Skip to content

Commit

Permalink
sqlite3: fix error when dropping db with system tables
Browse files Browse the repository at this point in the history
In some cases SQLite creates new tables for internal use. The names
of these internal schema objects[0] always begin with `sqlite_` and are
not to be touched by applications.

Example case: if column declaration uses `AUTOINCREMENT` keyword, a new
column is created into table `sqlite_sequence` which can not be dropped.
Currently, `sqlite3.Drop()` resolves tables to be dropped by querying
table `sqlite_master` for all tables in the database. However, this query
also returns aforementioned `sqlite_sequence` table.

This commit adds a filter to the query resolving tables to be dropped.
Filter removes tables starting with `sqlite_` from the query results.

[0]: https://www.sqlite.org/fileformat2.html#intschema
  • Loading branch information
hofnarrr committed Sep 20, 2024
1 parent 2668b50 commit edd80e2
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion database/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ func (m *Sqlite) Close() error {
}

func (m *Sqlite) Drop() (err error) {
query := `SELECT name FROM sqlite_master WHERE type = 'table';`
query := `
SELECT name FROM sqlite_master
WHERE type = 'table'
AND name NOT LIKE 'sqlite_%';`

tables, err := m.db.Query(query)
if err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
Expand Down

0 comments on commit edd80e2

Please sign in to comment.