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

Avoid dumping sqlite system tables into schema #735

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions dialect_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,16 @@ func (m *sqlite) FizzTranslator() fizz.Translator {
}

func (m *sqlite) DumpSchema(w io.Writer) error {
cmd := exec.Command("sqlite3", m.Details().Database, ".schema")
cmd := exec.Command("sqlite3", m.Details().Database, ".schema --nosys")
mattbnz marked this conversation as resolved.
Show resolved Hide resolved
return genericDumpSchema(m.Details(), cmd, w)
}

func (m *sqlite) LoadSchema(r io.Reader) error {
cmd := exec.Command("sqlite3", m.ConnectionDetails.Database)
cmd.Stderr = os.Stderr
in, err := cmd.StdinPipe()
if err != nil {
return err
return fmt.Errorf("could not open stdin to SQLite database %s: %w", m.ConnectionDetails.Database, err)
}
go func() {
defer in.Close()
Expand All @@ -246,12 +247,12 @@ func (m *sqlite) LoadSchema(r io.Reader) error {
log(logging.SQL, strings.Join(cmd.Args, " "))
err = cmd.Start()
if err != nil {
return err
return fmt.Errorf("could not load schema to SQLite database %s: %w", m.ConnectionDetails.Database, err)
}

err = cmd.Wait()
if err != nil {
return err
return fmt.Errorf("command failure loading schema to SQLite database %s: %w", m.ConnectionDetails.Database, err)
}

log(logging.Info, "loaded schema for %s", m.Details().Database)
Expand Down
20 changes: 20 additions & 0 deletions dialect_sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package pop

import (
"bytes"
"fmt"
"path/filepath"
"testing"
Expand Down Expand Up @@ -196,3 +197,22 @@ func TestSqlite_NewDriver(t *testing.T) {
_, err := newSQLiteDriver()
require.NoError(t, err)
}

func TestSqlite_NoDumpSysTables(t *testing.T) {
r := require.New(t)

dir := t.TempDir()
cd := &ConnectionDetails{Dialect: "sqlite", Database: filepath.Join(dir, "testdb.sqlite")}
c, err := NewConnection(cd)
r.NoError(err)
r.NoError(c.Open())

r.NoError(c.RawQuery("CREATE TABLE aitest (id integer primary key autoincrement);").Exec())


schema := new(bytes.Buffer)
r.NoError(c.Dialect.DumpSchema(schema))

r.Contains(schema.String(), "CREATE TABLE aitest")
r.NotContains(schema.String(), "CREATE TABLE sqlite_sequence")
}