-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
support filtering table definitions #39
Changes from 11 commits
7a64765
23ffe25
d1f8b90
fd15d43
a07e0b9
63390cd
7e1c4d6
e6ba312
16fe25b
53e002a
dab7671
32ff567
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,3 +133,82 @@ func TestTable_Build(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestTable_BuildWithDefinitionFilter(t *testing.T) { | ||
var ( | ||
definitionFilter = func(table rel.Table, def rel.TableDefinition) (bool, string) { | ||
_, ok := def.(rel.Key) | ||
// 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. | ||
if ok && table.Op == rel.SchemaAlter { | ||
return false, "adapter does not support adding keys when modifying tables" | ||
} | ||
|
||
return true, "" | ||
} | ||
tableBuilder = Table{ | ||
BufferFactory: BufferFactory{InlineValues: true, BoolTrueValue: "true", BoolFalseValue: "false", Quoter: Quote{IDPrefix: "`", IDSuffix: "`", IDSuffixEscapeChar: "`", ValueQuote: "'", ValueQuoteEscapeChar: "'"}}, | ||
ColumnMapper: sql.ColumnMapper, | ||
DefinitionFilter: definitionFilter, | ||
} | ||
) | ||
|
||
tests := []struct { | ||
result string | ||
table rel.Table | ||
}{ | ||
{ | ||
result: "CREATE TABLE `columns` (`bool` BOOL NOT NULL DEFAULT false, `int` INT(11) UNSIGNED, `bigint` BIGINT(20) UNSIGNED, `float` FLOAT(24) UNSIGNED, `decimal` DECIMAL(6,2) UNSIGNED, `string` VARCHAR(144) UNIQUE, `text` TEXT(1000), `date` DATE, `datetime` DATETIME DEFAULT '2020-01-01 01:00:00', `time` TIME, `blob` blob, PRIMARY KEY (`int`), FOREIGN KEY (`int`, `string`) REFERENCES `products` (`id`, `name`) ON DELETE CASCADE ON UPDATE CASCADE, UNIQUE `date_unique` (`date`)) Engine=InnoDB;", | ||
table: rel.Table{ | ||
Op: rel.SchemaCreate, | ||
Name: "columns", | ||
Definitions: []rel.TableDefinition{ | ||
rel.Column{Name: "bool", Type: rel.Bool, Required: true, Default: false}, | ||
rel.Column{Name: "int", Type: rel.Int, Limit: 11, Unsigned: true}, | ||
rel.Column{Name: "bigint", Type: rel.BigInt, Limit: 20, Unsigned: true}, | ||
rel.Column{Name: "float", Type: rel.Float, Precision: 24, Unsigned: true}, | ||
rel.Column{Name: "decimal", Type: rel.Decimal, Precision: 6, Scale: 2, Unsigned: true}, | ||
rel.Column{Name: "string", Type: rel.String, Limit: 144, Unique: true}, | ||
rel.Column{Name: "text", Type: rel.Text, Limit: 1000}, | ||
rel.Column{Name: "date", Type: rel.Date}, | ||
rel.Column{Name: "datetime", Type: rel.DateTime, Default: time.Date(2020, 1, 1, 1, 0, 0, 0, time.UTC)}, | ||
rel.Column{Name: "time", Type: rel.Time}, | ||
rel.Column{Name: "blob", Type: "blob"}, | ||
rel.Key{Columns: []string{"int"}, Type: rel.PrimaryKey}, | ||
rel.Key{Columns: []string{"int", "string"}, Type: rel.ForeignKey, Reference: rel.ForeignKeyReference{Table: "products", Columns: []string{"id", "name"}, OnDelete: "CASCADE", OnUpdate: "CASCADE"}}, | ||
rel.Key{Columns: []string{"date"}, Name: "date_unique", Type: rel.UniqueKey}, | ||
}, | ||
Options: "Engine=InnoDB", | ||
}, | ||
}, | ||
{ | ||
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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we just filter out silently like this? or do you think it's better to print some log? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to have some way to inform users. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be sufficient to output logs in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree, something like that should be sufficient There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @youpy, could you add the log as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I just added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought the log will be inside definitions functions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I misunderstood |
||
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}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
result: "", | ||
table: rel.Table{ | ||
Op: rel.SchemaAlter, | ||
Name: "transactions", | ||
Definitions: []rel.TableDefinition{ | ||
rel.Key{Op: rel.SchemaCreate, Columns: []string{"user_id"}, Type: rel.ForeignKey, Reference: rel.ForeignKeyReference{Table: "products", Columns: []string{"id", "name"}, OnDelete: "CASCADE", OnUpdate: "CASCADE"}}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.result, func(t *testing.T) { | ||
assert.Equal(t, test.result, tableBuilder.Build(test.table)) | ||
}) | ||
} | ||
} |
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.
won't it be simpler if we just use:
log.Printf("[REL] ....: %T", def)
?