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

SQLite: Do not dump system tables #383

Merged
merged 2 commits into from
Feb 21, 2023
Merged

Conversation

docapotamus
Copy link
Contributor

@docapotamus docapotamus commented Feb 17, 2023

Pass the --nosys flag through to the SQLite .schema command. This will stop the sqlite command from writing out system tables sqlite_*.

This flag is documented:

sqlite> .help .schema
.schema ?PATTERN?        Show the CREATE statements matching PATTERN
   Options:
      --indent             Try to pretty-print the schema
      --nosys              Omit objects whose names start with "sqlite_"

I wasn't sure if this would constitute a breaking change so I didn't hide it behind a flag as the issue was marked as Bug, but I'm happy to add this if it is correct to.

This was also quite hard to test as the test suite passed with or without the --nosys flag. I'm presuming this is a change in SQLite but I couldn't find a reference to it. I added a test any way just to make sure it doesn't creap back in.

I also confirmed this behaviour on the command line.

Fixes #315

@amacneil
Copy link
Owner

Can you tell why the CI test is failing? It's not obvious to me.

@docapotamus
Copy link
Contributor Author

docapotamus commented Feb 18, 2023

I'm not to sure. Looks to be the same issue in the previous PR. I'll try take a look later today.

golang/go#49004 seems to hold the relevant information.

Opened #384 to address this.

@amacneil
Copy link
Owner

Rebase should fix it

Pass the `--nosys` flag through to the SQLite `.schema` command. This
will stop the `sqlite` command from writing out system tables
`sqlite_*`.
@@ -131,7 +131,7 @@ func (drv *Driver) schemaMigrationsDump(db *sql.DB) ([]byte, error) {
// DumpSchema returns the current database schema
func (drv *Driver) DumpSchema(db *sql.DB) ([]byte, error) {
path := ConnectionString(drv.databaseURL)
schema, err := dbutil.RunCommand("sqlite3", path, ".schema")
schema, err := dbutil.RunCommand("sqlite3", path, ".schema --nosys")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure these should be separate arguments right?

Suggested change
schema, err := dbutil.RunCommand("sqlite3", path, ".schema --nosys")
schema, err := dbutil.RunCommand("sqlite3", path, ".schema", "--nosys")

Are you sure this change did anything for you? Since you mentioned the test never failed, in which situations did you see those tables get created?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can recreate this using the steps in the issue just not in the unit test.

If I add the .schema --nosys as one string the sqlite_sequence does not get dumped to the schema. If I add it as a sepeate string I receive an error.

No change applied

j.doherty@test my_dbmate % DATABASE_URL=sqlite:db/db.sqlite ./dist/dbmate dump
Writing: ./db/schema.sql
j.doherty@test my_dbmate % cat ./db/schema.sql 
CREATE TABLE IF NOT EXISTS "schema_migrations" (version varchar(255) primary key);
CREATE TABLE t (
      id INTEGER PRIMARY KEY AUTOINCREMENT
);
CREATE TABLE sqlite_sequence(name,seq);
-- Dbmate schema migrations
INSERT INTO "schema_migrations" (version) VALUES
  ('20230220094634');

With my patch applied

j.doherty@test my_dbmate % DATABASE_URL=sqlite:db/db.sqlite ./dist/dbmate dump
Writing: ./db/schema.sql
j.doherty@test my_dbmate % cat ./db/schema.sql                                
CREATE TABLE IF NOT EXISTS "schema_migrations" (version varchar(255) primary key);
CREATE TABLE t (
      id INTEGER PRIMARY KEY AUTOINCREMENT
);
-- Dbmate schema migrations
INSERT INTO "schema_migrations" (version) VALUES
  ('20230220094634');

With the flag in a seperate arg

j.doherty@test my_dbmate % DATABASE_URL=sqlite:db/db.sqlite ./dist/dbmate dump
Error: sqlite3: Error: unknown option: -nosys
Use -help for a list of options.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. That table only shows up in the dump if you have an AUTOINCREMENT field. I added one to the test and it correctly fails now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot. Don't know why I didn't think to check the test migration.

@amacneil amacneil merged commit 477b443 into amacneil:main Feb 21, 2023
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.

Invalid Sqlite schema.sql when INTEGER PRIMARY KEY AUTOINCREMENT is used
2 participants