-
Notifications
You must be signed in to change notification settings - Fork 252
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
Conversation
Can you tell why the CI test is failing? It's not obvious to me. |
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. |
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_*`.
665ed81
to
c820cad
Compare
@@ -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") |
There was a problem hiding this comment.
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?
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Pass the
--nosys
flag through to the SQLite.schema
command. This will stop thesqlite
command from writing out system tablessqlite_*
.This flag is documented:
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