Skip to content

Commit

Permalink
Skip Alter ADD CONSTRAINT when changing the table schema (#39)
Browse files Browse the repository at this point in the history
* skip unsupported table definitions

* improve test

* add test

* cast only if `table.Op == rel.SchemaAlter`
  • Loading branch information
youpy authored Oct 12, 2022
1 parent 6477937 commit d45378c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/go-rel/rel v0.38.0
github.com/go-rel/sql v0.12.0
github.com/go-rel/sql v0.12.1-0.20221010124958-ac6e8b8d87f0
github.com/mattn/go-sqlite3 v1.14.15
github.com/stretchr/testify v1.8.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/go-rel/rel v0.38.0 h1:XooFDMrzHNaZSNvH1ZrEpcn/7TvPz37z1kA66N3Ahjo=
github.com/go-rel/rel v0.38.0/go.mod h1:Zq18pQqXZbDh2JBCo29jgt+y90nZWkUvI+W9Ls29ans=
github.com/go-rel/sql v0.12.0 h1:1iIm2JgUr854TjN2C2403A9nZKH1RwbMJp09SQC4HO8=
github.com/go-rel/sql v0.12.0/go.mod h1:Usxy37iCTA5aIqoJGekV4ATdCUOK5w2FiR00/VvvLJQ=
github.com/go-rel/sql v0.12.1-0.20221010124958-ac6e8b8d87f0 h1:C6KJcSVP+f8ZDgDgVJiA1/2OxkuViNB6z8TpDKaBMxI=
github.com/go-rel/sql v0.12.1-0.20221010124958-ac6e8b8d87f0/go.mod h1:LJHx6rhOHC4PIFFzqoR6RA7QYJ2jCOrW7spUmJq9Raw=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
Expand Down
18 changes: 17 additions & 1 deletion sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package sqlite3

import (
db "database/sql"
"log"
"strings"

"github.com/go-rel/rel"
Expand All @@ -34,7 +35,7 @@ func New(database *db.DB) rel.Adapter {
deleteBuilder = builder.Delete{BufferFactory: bufferFactory, Query: queryBuilder, Filter: filterBuilder}
ddlBufferFactory = builder.BufferFactory{InlineValues: true, BoolTrueValue: "1", BoolFalseValue: "0", Quoter: builder.Quote{IDPrefix: "\"", IDSuffix: "\"", IDSuffixEscapeChar: "\"", ValueQuote: "'", ValueQuoteEscapeChar: "'"}}
ddlQueryBuilder = builder.Query{BufferFactory: ddlBufferFactory, Filter: filterBuilder}
tableBuilder = builder.Table{BufferFactory: ddlBufferFactory, ColumnMapper: columnMapper}
tableBuilder = builder.Table{BufferFactory: ddlBufferFactory, ColumnMapper: columnMapper, DefinitionFilter: definitionFilter}
indexBuilder = builder.Index{BufferFactory: ddlBufferFactory, Query: ddlQueryBuilder, Filter: filterBuilder, SupportFilter: true}
)

Expand Down Expand Up @@ -119,3 +120,18 @@ func columnMapper(column *rel.Column) (string, int, int) {

return typ, m, n
}

func definitionFilter(table rel.Table, def rel.TableDefinition) bool {
if table.Op == rel.SchemaAlter {
// https://www.sqlite.org/omitted.html
// > Only the RENAME TABLE, ADD COLUMN, RENAME COLUMN, and DROP COLUMN variants of the ALTER TABLE command are supported.
_, ok := def.(rel.Key)
if ok {
log.Print("[REL] SQLite3 adapter does not support adding keys when modifying tables")

return false
}
}

return true
}
43 changes: 43 additions & 0 deletions sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/go-rel/rel"
"github.com/go-rel/sql"
"github.com/go-rel/sql/specs"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -123,3 +124,45 @@ func TestAdapter_Exec_error(t *testing.T) {
_, _, err = adapter.Exec(ctx, "error", nil)
assert.NotNil(t, err)
}

func TestAdapter_TableBuilder(t *testing.T) {
adapter, err := Open(dsn())
assert.Nil(t, err)
defer adapter.Close()

tests := []struct {
result string
table rel.Table
}{
{
result: `ALTER TABLE "columns" ADD COLUMN "verified" BOOL;ALTER TABLE "columns" RENAME COLUMN "string" TO "name";ALTER TABLE "columns" ;ALTER TABLE "columns" DROP COLUMN "blob";`,
table: rel.Table{
Op: rel.SchemaAlter,
Name: "columns",
Definitions: []rel.TableDefinition{
rel.Column{Name: "verified", Type: rel.Bool, Op: rel.SchemaCreate},
rel.Column{Name: "string", Rename: "name", Op: rel.SchemaRename},
rel.Column{Name: "bool", Type: rel.Int, Op: rel.SchemaAlter},
rel.Column{Name: "blob", Op: rel.SchemaDrop},

// unsupported and will be skipped
rel.Key{Op: rel.SchemaCreate, Columns: []string{"user_id"}, Type: rel.ForeignKey, Reference: rel.ForeignKeyReference{Table: "products", Columns: []string{"id", "name"}}},
},
},
},
{
result: `ALTER TABLE "table" RENAME TO "table1";`,
table: rel.Table{
Op: rel.SchemaRename,
Name: "table",
Rename: "table1",
},
},
}

for _, test := range tests {
t.Run(test.result, func(t *testing.T) {
assert.Equal(t, test.result, adapter.(*sql.SQL).TableBuilder.Build(test.table))
})
}
}

0 comments on commit d45378c

Please sign in to comment.