mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
dialect/sql/schema: comment changes (#415)
This commit is contained in:
@@ -145,8 +145,8 @@ func (m *Migrate) create(ctx context.Context, tx dialect.Tx, tables ...*Table) e
|
|||||||
if err := tx.Exec(ctx, query, args, nil); err != nil {
|
if err := tx.Exec(ctx, query, args, nil); err != nil {
|
||||||
return fmt.Errorf("create table %q: %v", t.Name, err)
|
return fmt.Errorf("create table %q: %v", t.Name, err)
|
||||||
}
|
}
|
||||||
// if global unique identifier is enabled and it's not a relation table,
|
// If global unique identifier is enabled and it's not
|
||||||
// allocate a range for the table pk.
|
// a relation table, allocate a range for the table pk.
|
||||||
if m.universalID && len(t.PrimaryKey) == 1 {
|
if m.universalID && len(t.PrimaryKey) == 1 {
|
||||||
if err := m.allocPKRange(ctx, tx, t); err != nil {
|
if err := m.allocPKRange(ctx, tx, t); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -161,7 +161,7 @@ func (m *Migrate) create(ctx context.Context, tx dialect.Tx, tables ...*Table) e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// create foreign keys after tables were created/altered,
|
// Create foreign keys after tables were created/altered,
|
||||||
// because circular foreign-key constraints are possible.
|
// because circular foreign-key constraints are possible.
|
||||||
for _, t := range tables {
|
for _, t := range tables {
|
||||||
if len(t.ForeignKeys) == 0 {
|
if len(t.ForeignKeys) == 0 {
|
||||||
@@ -277,16 +277,16 @@ func (m *Migrate) changeSet(curr, new *Table) (*changes, error) {
|
|||||||
return nil, fmt.Errorf("cannot change primary key for table: %q", curr.Name)
|
return nil, fmt.Errorf("cannot change primary key for table: %q", curr.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// add or modify columns.
|
// Add or modify columns.
|
||||||
for _, c1 := range new.Columns {
|
for _, c1 := range new.Columns {
|
||||||
// ignore primary keys.
|
// Ignore primary keys.
|
||||||
if c1.PrimaryKey() {
|
if c1.PrimaryKey() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch c2, ok := curr.column(c1.Name); {
|
switch c2, ok := curr.column(c1.Name); {
|
||||||
case !ok:
|
case !ok:
|
||||||
change.column.add = append(change.column.add, c1)
|
change.column.add = append(change.column.add, c1)
|
||||||
// modify a non-unique column to unique.
|
// Modify a non-unique column to unique.
|
||||||
case c1.Unique && !c2.Unique:
|
case c1.Unique && !c2.Unique:
|
||||||
change.index.add.append(&Index{
|
change.index.add.append(&Index{
|
||||||
Name: c1.Name,
|
Name: c1.Name,
|
||||||
@@ -294,28 +294,28 @@ func (m *Migrate) changeSet(curr, new *Table) (*changes, error) {
|
|||||||
Columns: []*Column{c1},
|
Columns: []*Column{c1},
|
||||||
columns: []string{c1.Name},
|
columns: []string{c1.Name},
|
||||||
})
|
})
|
||||||
// modify a unique column to non-unique.
|
// Modify a unique column to non-unique.
|
||||||
case !c1.Unique && c2.Unique:
|
case !c1.Unique && c2.Unique:
|
||||||
idx, ok := curr.index(c2.Name)
|
idx, ok := curr.index(c2.Name)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing index to drop for column %q", c2.Name)
|
return nil, fmt.Errorf("missing index to drop for column %q", c2.Name)
|
||||||
}
|
}
|
||||||
change.index.drop.append(idx)
|
change.index.drop.append(idx)
|
||||||
// extending column types.
|
// Extending column types.
|
||||||
case m.cType(c1) != m.cType(c2):
|
case m.cType(c1) != m.cType(c2):
|
||||||
if !c2.ConvertibleTo(c1) {
|
if !c2.ConvertibleTo(c1) {
|
||||||
return nil, fmt.Errorf("changing column type for %q is invalid (%s != %s)", c1.Name, m.cType(c1), m.cType(c2))
|
return nil, fmt.Errorf("changing column type for %q is invalid (%s != %s)", c1.Name, m.cType(c1), m.cType(c2))
|
||||||
}
|
}
|
||||||
fallthrough
|
fallthrough
|
||||||
// change nullability of a column.
|
// Change nullability of a column.
|
||||||
case c1.Nullable != c2.Nullable:
|
case c1.Nullable != c2.Nullable:
|
||||||
change.column.modify = append(change.column.modify, c1)
|
change.column.modify = append(change.column.modify, c1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// drop columns.
|
// Drop columns.
|
||||||
for _, c1 := range curr.Columns {
|
for _, c1 := range curr.Columns {
|
||||||
// if a column was dropped, multi-columns indexes that are associated with this column will
|
// If a column was dropped, multi-columns indexes that are associated with this column will
|
||||||
// no longer behave the same. Therefore, these indexes should be dropped too. There's no need
|
// no longer behave the same. Therefore, these indexes should be dropped too. There's no need
|
||||||
// to do it explicitly (here), because entc will remove them from the schema specification,
|
// to do it explicitly (here), because entc will remove them from the schema specification,
|
||||||
// and they will be dropped in the block below.
|
// and they will be dropped in the block below.
|
||||||
@@ -324,19 +324,19 @@ func (m *Migrate) changeSet(curr, new *Table) (*changes, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add or modify indexes.
|
// Add or modify indexes.
|
||||||
for _, idx1 := range new.Indexes {
|
for _, idx1 := range new.Indexes {
|
||||||
switch idx2, ok := curr.index(idx1.Name); {
|
switch idx2, ok := curr.index(idx1.Name); {
|
||||||
case !ok:
|
case !ok:
|
||||||
change.index.add.append(idx1)
|
change.index.add.append(idx1)
|
||||||
// changing index cardinality require drop and create.
|
// Changing index cardinality require drop and create.
|
||||||
case idx1.Unique != idx2.Unique:
|
case idx1.Unique != idx2.Unique:
|
||||||
change.index.drop.append(idx2)
|
change.index.drop.append(idx2)
|
||||||
change.index.add.append(idx1)
|
change.index.add.append(idx1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// drop indexes.
|
// Drop indexes.
|
||||||
for _, idx1 := range curr.Indexes {
|
for _, idx1 := range curr.Indexes {
|
||||||
_, ok1 := new.fk(idx1.Name)
|
_, ok1 := new.fk(idx1.Name)
|
||||||
_, ok2 := new.index(idx1.Name)
|
_, ok2 := new.index(idx1.Name)
|
||||||
@@ -464,7 +464,7 @@ func (m *Migrate) types(ctx context.Context, tx dialect.Tx) error {
|
|||||||
|
|
||||||
func (m *Migrate) allocPKRange(ctx context.Context, tx dialect.Tx, t *Table) error {
|
func (m *Migrate) allocPKRange(ctx context.Context, tx dialect.Tx, t *Table) error {
|
||||||
id := indexOf(m.typeRanges, t.Name)
|
id := indexOf(m.typeRanges, t.Name)
|
||||||
// if the table re-created, re-use its range from
|
// If the table re-created, re-use its range from
|
||||||
// the past. otherwise, allocate a new id-range.
|
// the past. otherwise, allocate a new id-range.
|
||||||
if id == -1 {
|
if id == -1 {
|
||||||
if len(m.typeRanges) > MaxTypes {
|
if len(m.typeRanges) > MaxTypes {
|
||||||
@@ -478,7 +478,7 @@ func (m *Migrate) allocPKRange(ctx context.Context, tx dialect.Tx, t *Table) err
|
|||||||
id = len(m.typeRanges)
|
id = len(m.typeRanges)
|
||||||
m.typeRanges = append(m.typeRanges, t.Name)
|
m.typeRanges = append(m.typeRanges, t.Name)
|
||||||
}
|
}
|
||||||
// set the id offset for table.
|
// Set the id offset for table.
|
||||||
return m.setRange(ctx, tx, t, id<<32)
|
return m.setRange(ctx, tx, t, id<<32)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func (d *MySQL) table(ctx context.Context, tx dialect.Tx, name string) (*Table,
|
|||||||
if err := tx.Query(ctx, query, args, rows); err != nil {
|
if err := tx.Query(ctx, query, args, rows); err != nil {
|
||||||
return nil, fmt.Errorf("mysql: reading table description %v", err)
|
return nil, fmt.Errorf("mysql: reading table description %v", err)
|
||||||
}
|
}
|
||||||
// call Close in cases of failures (Close is idempotent).
|
// Call Close in cases of failures (Close is idempotent).
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
t := NewTable(name)
|
t := NewTable(name)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
@@ -82,7 +82,7 @@ func (d *MySQL) table(ctx context.Context, tx dialect.Tx, name string) (*Table,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// add and link indexes to table columns.
|
// Add and link indexes to table columns.
|
||||||
for _, idx := range indexes {
|
for _, idx := range indexes {
|
||||||
t.AddIndex(idx.Name, idx.Unique, idx.columns)
|
t.AddIndex(idx.Name, idx.Unique, idx.columns)
|
||||||
}
|
}
|
||||||
@@ -124,7 +124,7 @@ func (d *MySQL) verifyRange(ctx context.Context, tx dialect.Tx, t *Table, expect
|
|||||||
if err := tx.Query(ctx, query, args, rows); err != nil {
|
if err := tx.Query(ctx, query, args, rows); err != nil {
|
||||||
return fmt.Errorf("mysql: query auto_increment %v", err)
|
return fmt.Errorf("mysql: query auto_increment %v", err)
|
||||||
}
|
}
|
||||||
// call Close in cases of failures (Close is idempotent).
|
// Call Close in cases of failures (Close is idempotent).
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
actual := &sql.NullInt64{}
|
actual := &sql.NullInt64{}
|
||||||
if err := sql.ScanOne(rows, actual); err != nil {
|
if err := sql.ScanOne(rows, actual); err != nil {
|
||||||
@@ -151,7 +151,7 @@ func (d *MySQL) tBuilder(t *Table) *sql.TableBuilder {
|
|||||||
for _, pk := range t.PrimaryKey {
|
for _, pk := range t.PrimaryKey {
|
||||||
b.PrimaryKey(pk.Name)
|
b.PrimaryKey(pk.Name)
|
||||||
}
|
}
|
||||||
// default charset / collation on MySQL table.
|
// Default charset / collation on MySQL table.
|
||||||
// columns can be override using the Charset / Collate fields.
|
// columns can be override using the Charset / Collate fields.
|
||||||
b.Charset("utf8mb4").Collate("utf8mb4_bin")
|
b.Charset("utf8mb4").Collate("utf8mb4_bin")
|
||||||
return b
|
return b
|
||||||
@@ -212,7 +212,7 @@ func (d *MySQL) cType(c *Column) (t string) {
|
|||||||
t = "double"
|
t = "double"
|
||||||
case field.TypeTime:
|
case field.TypeTime:
|
||||||
t = "timestamp"
|
t = "timestamp"
|
||||||
// in MySQL timestamp columns are `NOT NULL by default, and assigning NULL
|
// In MySQL, timestamp columns are `NOT NULL by default, and assigning NULL
|
||||||
// assigns the current_timestamp(). We avoid this if not set otherwise.
|
// assigns the current_timestamp(). We avoid this if not set otherwise.
|
||||||
c.Nullable = true
|
c.Nullable = true
|
||||||
case field.TypeEnum:
|
case field.TypeEnum:
|
||||||
@@ -416,7 +416,7 @@ func (d *MySQL) scanIndexes(rows *sql.Rows) (Indexes, error) {
|
|||||||
if err := rows.Scan(&name, &column, &nonuniq, &seqindex); err != nil {
|
if err := rows.Scan(&name, &column, &nonuniq, &seqindex); err != nil {
|
||||||
return nil, fmt.Errorf("scanning index description: %v", err)
|
return nil, fmt.Errorf("scanning index description: %v", err)
|
||||||
}
|
}
|
||||||
// ignore primary keys.
|
// Ignore primary keys.
|
||||||
if name == "PRIMARY" {
|
if name == "PRIMARY" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ func (d *Postgres) table(ctx context.Context, tx dialect.Tx, name string) (*Tabl
|
|||||||
if err := tx.Query(ctx, query, args, rows); err != nil {
|
if err := tx.Query(ctx, query, args, rows); err != nil {
|
||||||
return nil, fmt.Errorf("postgres: reading table description %v", err)
|
return nil, fmt.Errorf("postgres: reading table description %v", err)
|
||||||
}
|
}
|
||||||
// call `Close` in cases of failures (`Close` is idempotent).
|
// Call `Close` in cases of failures (`Close` is idempotent).
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
t := NewTable(name)
|
t := NewTable(name)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
|
|||||||
@@ -234,7 +234,7 @@ func (c *Column) defaultValue(b *sql.ColumnBuilder) {
|
|||||||
case bool:
|
case bool:
|
||||||
attr += strconv.FormatBool(v)
|
attr += strconv.FormatBool(v)
|
||||||
case string:
|
case string:
|
||||||
// escape single quote by replacing each with 2.
|
// Escape single quote by replacing each with 2.
|
||||||
attr += fmt.Sprintf("'%s'", strings.Replace(v, "'", "''", -1))
|
attr += fmt.Sprintf("'%s'", strings.Replace(v, "'", "''", -1))
|
||||||
default:
|
default:
|
||||||
attr += fmt.Sprint(v)
|
attr += fmt.Sprint(v)
|
||||||
|
|||||||
Reference in New Issue
Block a user