dialect/sql/schema: make Field.Unique and Index.Unique equal (#2004)

This will allow moving from:

	field.String(c).Unique()

To:

	index.Field(c).Unique()

And vice versa
This commit is contained in:
Ariel Mashraki
2021-10-03 16:20:28 +03:00
committed by GitHub
parent 842fc9df1c
commit 14cba0abb6
17 changed files with 687 additions and 31 deletions

View File

@@ -342,17 +342,28 @@ func (m *Migrate) changeSet(curr, new *Table) (*changes, error) {
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{
Name: c1.Name,
Unique: true,
Columns: []*Column{c1},
columns: []string{c1.Name},
})
// Make sure the table does not have unique index for this column
// before adding it to the changeset, because there are 2 ways to
// configure uniqueness on ent.Field (using the Unique modifier or
// adding rule on the Indexes option).
if idx, ok := curr.index(c1.Name); !ok || !idx.Unique {
change.index.add.append(&Index{
Name: c1.Name,
Unique: true,
Columns: []*Column{c1},
columns: []string{c1.Name},
})
}
// Modify a unique column to non-unique.
case !c1.Unique && c2.Unique:
// If the uniqueness was defined on the Indexes option,
// or was moved from the Unique modifier to the Indexes.
if idx, ok := new.index(c1.Name); ok && idx.Unique {
continue
}
idx, ok := curr.index(c2.Name)
if !ok {
return nil, fmt.Errorf("missing index to drop for column %q", c2.Name)
return nil, fmt.Errorf("missing index to drop for unique column %q", c2.Name)
}
change.index.drop.append(idx)
// Extending column types.

View File

@@ -617,6 +617,7 @@ func TestPostgres_Create(t *testing.T) {
c2 := []*Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "score", Type: field.TypeInt},
{Name: "email", Type: field.TypeString},
}
return []*Table{
{
@@ -633,7 +634,9 @@ func TestPostgres_Create(t *testing.T) {
Columns: c2,
PrimaryKey: c2[0:1],
Indexes: Indexes{
{Name: "equipment_score", Columns: c2[1:]},
{Name: "equipment_score", Columns: c2[1:2]},
// Index should not be changed.
{Name: "equipment_email", Unique: true, Columns: c2[2:]},
},
},
}
@@ -667,11 +670,13 @@ func TestPostgres_Create(t *testing.T) {
WithArgs("equipment").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name", "numeric_precision", "numeric_scale"}).
AddRow("id", "bigint", "NO", "NULL", "int8", nil, nil).
AddRow("score", "bigint", "NO", "NULL", "int8", nil, nil))
AddRow("score", "bigint", "NO", "NULL", "int8", nil, nil).
AddRow("email", "character varying", "YES", "NULL", "varchar", nil, nil))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "CURRENT_SCHEMA()", "equipment"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0).
AddRow("equipment_score", "score", "f", "f", 0))
AddRow("equipment_score", "score", "f", "f", 0).
AddRow("equipment_email", "email", "f", "t", 0))
mock.ExpectCommit()
},
},