From 4f3c203a738dbb694fbf048a0f6f52513579ae1d Mon Sep 17 00:00:00 2001 From: hofnarr Date: Fri, 20 Sep 2024 20:30:18 +0300 Subject: [PATCH 1/3] sqlite3/examples: add migration with table w/ AUTOINCREMENT Declaring column as `AUTOINCREMENT` causes SQLite to create a new table `sqlite_sequence` under the hood, which cannot be dropped. This is useful in `migration.Drop()` tests. --- .../examples/migrations/55_autoincrement_table.down.sql | 1 + .../sqlite3/examples/migrations/55_autoincrement_table.up.sql | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 database/sqlite3/examples/migrations/55_autoincrement_table.down.sql create mode 100644 database/sqlite3/examples/migrations/55_autoincrement_table.up.sql diff --git a/database/sqlite3/examples/migrations/55_autoincrement_table.down.sql b/database/sqlite3/examples/migrations/55_autoincrement_table.down.sql new file mode 100644 index 000000000..35107d7e5 --- /dev/null +++ b/database/sqlite3/examples/migrations/55_autoincrement_table.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS autoincr; diff --git a/database/sqlite3/examples/migrations/55_autoincrement_table.up.sql b/database/sqlite3/examples/migrations/55_autoincrement_table.up.sql new file mode 100644 index 000000000..67b8ee28f --- /dev/null +++ b/database/sqlite3/examples/migrations/55_autoincrement_table.up.sql @@ -0,0 +1,3 @@ +CREATE TABLE IF NOT EXISTS autoincr ( + id INTEGER PRIMARY KEY AUTOINCREMENT +); From 2668b50d8da2df89b94649b956c3fabfc62ac248 Mon Sep 17 00:00:00 2001 From: hofnarr Date: Fri, 20 Sep 2024 22:22:02 +0300 Subject: [PATCH 2/3] sqlite3/examples: add migration running ANALYZE Running `ANALYZE` causes SQLite to create internal undroppable tables. This is useful during `migrate.Drop()` testing. --- database/sqlite3/examples/migrations/66_analyze_table.down.sql | 1 + database/sqlite3/examples/migrations/66_analyze_table.up.sql | 1 + 2 files changed, 2 insertions(+) create mode 100644 database/sqlite3/examples/migrations/66_analyze_table.down.sql create mode 100644 database/sqlite3/examples/migrations/66_analyze_table.up.sql diff --git a/database/sqlite3/examples/migrations/66_analyze_table.down.sql b/database/sqlite3/examples/migrations/66_analyze_table.down.sql new file mode 100644 index 000000000..1fcd659ca --- /dev/null +++ b/database/sqlite3/examples/migrations/66_analyze_table.down.sql @@ -0,0 +1 @@ +-- noop diff --git a/database/sqlite3/examples/migrations/66_analyze_table.up.sql b/database/sqlite3/examples/migrations/66_analyze_table.up.sql new file mode 100644 index 000000000..04bdfb4a6 --- /dev/null +++ b/database/sqlite3/examples/migrations/66_analyze_table.up.sql @@ -0,0 +1 @@ +ANALYZE pets; From edd80e23e55345080040f0d8c54a7bb85f9d0b6a Mon Sep 17 00:00:00 2001 From: hofnarr Date: Fri, 20 Sep 2024 19:04:09 +0300 Subject: [PATCH 3/3] sqlite3: fix error when dropping db with system tables 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 --- database/sqlite3/sqlite3.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/database/sqlite3/sqlite3.go b/database/sqlite3/sqlite3.go index 56bb23338..8bd70c952 100644 --- a/database/sqlite3/sqlite3.go +++ b/database/sqlite3/sqlite3.go @@ -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)}