Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite3: fix errors when Drop()ing a SQLite DB containing internal tables #1172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hofnarrr
Copy link

@hofnarrr hofnarrr commented Sep 20, 2024

In some cases SQLite creates new tables for internal use. The names of these internal schema objects always begin with sqlite_ and the objects are not to be touched by applications.

Example case:

If column declaration uses AUTOINCREMENT keyword, a table sqlite_sequence is created to track the unique ids. This table 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 leading to errors when trying to DROP it later.

This branch adds:

  • A filter to the query resolving tables to be dropped. Filter removes tables starting with sqlite_ from the query results.
  • Test migrations reproducing the issues

Fixes #1138

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.
@hofnarrr hofnarrr force-pushed the hofnarrr/sqlite3/fix-drop-undroppable-tables branch from 206e2d0 to e8788d9 Compare September 20, 2024 19:03
Running `ANALYZE` causes SQLite to create internal undroppable tables.
This is useful during `migrate.Drop()` testing.
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
@hofnarrr hofnarrr force-pushed the hofnarrr/sqlite3/fix-drop-undroppable-tables branch from e8788d9 to edd80e2 Compare September 20, 2024 19:25
@hofnarrr hofnarrr changed the title sqlite3: fix error when Drop()ing a SQLite DB which uses AUTOINCREMENT columns sqlite3: fix errors when Drop()ing a SQLite DB containing internal tables Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sqlite3 Drop() attempts to drop internal tables which might fail f.e. for sqlite_sequence
1 participant