Skip to content

Commit

Permalink
db: only strict schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Feb 17, 2024
1 parent ef91bb4 commit 3a93ac5
Show file tree
Hide file tree
Showing 250 changed files with 6,721 additions and 13,733 deletions.
1 change: 0 additions & 1 deletion cmd/chai/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func NewApp() *cli.App {
app.EnableBashCompletion = true

app.Commands = []*cli.Command{
NewInsertCommand(),
NewVersionCommand(),
NewDumpCommand(),
NewRestoreCommand(),
Expand Down
127 changes: 0 additions & 127 deletions cmd/chai/commands/insert.go

This file was deleted.

32 changes: 27 additions & 5 deletions cmd/chai/dbutil/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dbutil
import (
"fmt"
"io"
"strings"

"github.com/chaisql/chai"
"go.uber.org/multierr"
Expand Down Expand Up @@ -57,14 +58,35 @@ func dumpTable(tx *chai.Tx, w io.Writer, query, tableName string) error {
defer res.Close()

// Inserts statements.
insert := fmt.Sprintf("INSERT INTO %s VALUES", tableName)
return res.Iterate(func(r *chai.Row) error {
data, err := r.MarshalJSON()
cols, err := r.Columns()
if err != nil {
return err
}

if _, err := fmt.Fprintf(w, "%s %s;\n", insert, string(data)); err != nil {
m := make(map[string]interface{}, len(cols))
err = r.MapScan(m)
if err != nil {
return err
}

var sb strings.Builder

for i, c := range cols {
if i > 0 {
sb.WriteString(", ")
}

v := m[c]
if v == nil {
sb.WriteString("NULL")
continue
}

fmt.Fprintf(&sb, "%v", v)
}

if _, err := fmt.Fprintf(w, "INSERT INTO %s VALUES (%s);\n", tableName, sb.String()); err != nil {
return err
}

Expand Down Expand Up @@ -105,8 +127,8 @@ func dumpSchema(tx *chai.Tx, w io.Writer, query string, tableName string) error
// Indexes statements.
res, err := tx.Query(`
SELECT sql FROM __chai_catalog WHERE
type = 'index' AND owner.table_name = ? OR
type = 'sequence' AND owner IS NULL
type = 'index' AND owner_table_name = ? OR
type = 'sequence' AND owner_table_name IS NULL
`, tableName)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmd/chai/dbutil/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestDump(t *testing.T) {
writeToBuf("\n")
}

q := fmt.Sprintf("CREATE TABLE %s (a INTEGER, b ANY, c ANY, ...);", table)
q := fmt.Sprintf("CREATE TABLE %s (a INTEGER, b INTEGER, c INTEGER);", table)
err = db.Exec(q)
assert.NoError(t, err)
writeToBuf(q + "\n")
Expand All @@ -68,17 +68,17 @@ func TestDump(t *testing.T) {
assert.NoError(t, err)
writeToBuf(q + "\n")

q = fmt.Sprintf(`INSERT INTO %s VALUES {"a": %d, "b": %d, "c": %d};`, table, 1, 2, 3)
q = fmt.Sprintf(`INSERT INTO %s VALUES (%d, %d, %d);`, table, 1, 2, 3)
err = db.Exec(q)
assert.NoError(t, err)
writeToBuf(q + "\n")

q = fmt.Sprintf(`INSERT INTO %s VALUES {"a": %d, "b": %d, "c": %d};`, table, 2, 2, 2)
q = fmt.Sprintf(`INSERT INTO %s VALUES (%d, %d, %d);`, table, 2, 2, 2)
err = db.Exec(q)
assert.NoError(t, err)
writeToBuf(q + "\n")

q = fmt.Sprintf(`INSERT INTO %s VALUES {"a": %d, "b": %d, "c": %d};`, table, 3, 2, 1)
q = fmt.Sprintf(`INSERT INTO %s VALUES (%d, %d, %d);`, table, 3, 2, 1)
err = db.Exec(q)
assert.NoError(t, err)
writeToBuf(q + "\n")
Expand Down
2 changes: 1 addition & 1 deletion cmd/chai/dbutil/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestExecSQL(t *testing.T) {

var got bytes.Buffer
err = ExecSQL(context.Background(), db, strings.NewReader(`
CREATE TABLE test(a, ...);
CREATE TABLE test(a INT, b INT);
CREATE INDEX idx_a ON test (a);
INSERT INTO test (a, b) VALUES (1, 2), (2, 2), (3, 2);
SELECT * FROM test;
Expand Down
111 changes: 0 additions & 111 deletions cmd/chai/dbutil/insert.go

This file was deleted.

Loading

0 comments on commit 3a93ac5

Please sign in to comment.