Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/SQLite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -930,13 +930,15 @@ end
SQLite.drop!(db, table; ifexists::Bool=false)

drop the SQLite table `table` from the database `db`; `ifexists=true` will prevent an error being thrown if `table` doesn't exist

The database file keeps its size. Run `VACUUM` outside a transaction to reclaim the
space; it can renumber the rowids of tables without an `INTEGER PRIMARY KEY`.
"""
function drop!(db::DB, table::AbstractString; ifexists::Bool = false)
exists = ifexists ? "IF EXISTS" : ""
transaction(db) do
execute(db, "DROP TABLE $exists $(esc_id(table))")
end
execute(db, "VACUUM")
end

"""
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,22 @@ end
end

@testset "load! / drop!" begin
@testset "drop! keeps other rowids and works in a transaction" begin
db = SQLite.DB()
DBInterface.execute(db, "CREATE TABLE keep (v TEXT)")
DBInterface.execute(db, "INSERT INTO keep VALUES ('a'), ('b'), ('c')")
DBInterface.execute(db, "DELETE FROM keep WHERE v = 'a'")
DBInterface.execute(db, "CREATE TABLE gone (x INT)")
SQLite.drop!(db, "gone")
@test columntable(DBInterface.execute(db, "SELECT rowid, v FROM keep")) ==
(rowid = [2, 3], v = ["b", "c"])

DBInterface.execute(db, "CREATE TABLE gone_in_tx (x INT)")
DBInterface.transaction(() -> SQLite.drop!(db, "gone_in_tx"), db)
@test [t.name for t in SQLite.tables(db)] == ["keep"]
close(db)
end

@testset "ANALYZE quotes table and index names" begin
db = SQLite.DB()
@test SQLite.load!((a = [1, 1, 2],), db, "my table"; analyze = true) == "my table"
Expand Down
Loading