mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
dialect/sql/schemma: fail if column type is invalid (#486)
This commit is contained in:
@@ -279,6 +279,8 @@ func (m *Migrate) changeSet(curr, new *Table) (*changes, error) {
|
||||
switch c2, ok := curr.column(c1.Name); {
|
||||
case !ok:
|
||||
change.column.add = append(change.column.add, c1)
|
||||
case !c2.Type.Valid():
|
||||
return nil, fmt.Errorf("invalid type %q for column %q", c2.typ, c2.Name)
|
||||
// Modify a non-unique column to unique.
|
||||
case c1.Unique && !c2.Unique:
|
||||
change.index.add.append(&Index{
|
||||
|
||||
@@ -239,6 +239,8 @@ func TestMySQL_Create(t *testing.T) {
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
{Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{dialect.MySQL: "datetime"}, Nullable: true},
|
||||
{Name: "updated_at", Type: field.TypeTime, SchemaType: map[string]string{dialect.MySQL: "datetime"}, Nullable: true},
|
||||
{Name: "deleted_at", Type: field.TypeTime, Nullable: true},
|
||||
},
|
||||
PrimaryKey: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
@@ -252,11 +254,15 @@ func TestMySQL_Create(t *testing.T) {
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"column_name", "column_type", "is_nullable", "column_key", "column_default", "extra", "character_set_name", "collation_name"}).
|
||||
AddRow("id", "bigint(20)", "NO", "PRI", "NULL", "auto_increment", "", "").
|
||||
AddRow("created_at", "datetime", "NO", "YES", "NULL", "", "", ""))
|
||||
AddRow("created_at", "datetime", "NO", "YES", "NULL", "", "", "").
|
||||
AddRow("updated_at", "timestamp", "NO", "YES", "NULL", "", "", "").
|
||||
AddRow("deleted_at", "datetime", "NO", "YES", "NULL", "", "", ""))
|
||||
mock.ExpectQuery(escape("SELECT `index_name`, `column_name`, `non_unique`, `seq_in_index` FROM INFORMATION_SCHEMA.STATISTICS WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ? ORDER BY `index_name`, `seq_in_index`")).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "non_unique", "seq_in_index"}).
|
||||
AddRow("PRIMARY", "id", "0", "1"))
|
||||
mock.ExpectExec(escape("ALTER TABLE `users` MODIFY COLUMN `updated_at` datetime NULL, MODIFY COLUMN `deleted_at` timestamp NULL")).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
},
|
||||
|
||||
@@ -136,7 +136,9 @@ func TestPostgres_Create(t *testing.T) {
|
||||
{Name: "uuid", Type: field.TypeUUID, Nullable: true},
|
||||
{Name: "text", Type: field.TypeString, Nullable: true, Size: math.MaxInt32},
|
||||
{Name: "age", Type: field.TypeInt},
|
||||
{Name: "date", Type: field.TypeTime, SchemaType: map[string]string{dialect.Postgres: "date"}},
|
||||
{Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{dialect.Postgres: "date"}},
|
||||
{Name: "updated_at", Type: field.TypeTime, SchemaType: map[string]string{dialect.MySQL: "date"}, Nullable: true},
|
||||
{Name: "deleted_at", Type: field.TypeTime, Nullable: true},
|
||||
},
|
||||
PrimaryKey: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
@@ -152,12 +154,14 @@ func TestPostgres_Create(t *testing.T) {
|
||||
AddRow("id", "bigint", "NO", "NULL").
|
||||
AddRow("name", "character varying", "YES", "NULL").
|
||||
AddRow("uuid", "uuid", "YES", "NULL").
|
||||
AddRow("date", "date", "NO", "CURRENT_DATE").
|
||||
AddRow("created_at", "date", "NO", "CURRENT_DATE").
|
||||
AddRow("updated_at", "timestamp", "YES", "NULL").
|
||||
AddRow("deleted_at", "date", "YES", "NULL").
|
||||
AddRow("text", "text", "YES", "NULL"))
|
||||
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
|
||||
AddRow("users_pkey", "id", "t", "t", 0))
|
||||
mock.ExpectExec(escape(`ALTER TABLE "users" ADD COLUMN "age" bigint NOT NULL`)).
|
||||
mock.ExpectExec(escape(`ALTER TABLE "users" ADD COLUMN "age" bigint NOT NULL, ALTER COLUMN "updated_at" TYPE timestamp with time zone, ALTER COLUMN "updated_at" DROP NOT NULL, ALTER COLUMN "deleted_at" TYPE timestamp with time zone, ALTER COLUMN "deleted_at" DROP NOT NULL`)).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user