diff --git a/go.mod b/go.mod index f810960..52ebd92 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 13df482..ce23c62 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/sqlite3.go b/sqlite3.go index fb8ddaa..9d44f96 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -14,6 +14,7 @@ package sqlite3 import ( db "database/sql" + "log" "strings" "github.com/go-rel/rel" @@ -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} ) @@ -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 +} diff --git a/sqlite3_test.go b/sqlite3_test.go index 653971c..d1c7ea0 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -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" @@ -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)) + }) + } +}