dialect/sql/schema: setrange on custom column name of pks (#333)

This commit is contained in:
Ariel Mashraki
2020-02-09 09:41:26 +02:00
committed by GitHub
parent 48d33fde9d
commit 26440c2bc9
12 changed files with 60 additions and 26 deletions

View File

@@ -62,11 +62,15 @@ func (d *Postgres) fkExist(ctx context.Context, tx dialect.Tx, name string) (boo
}
// setRange sets restart the identity column to the given offset. Used by the universal-id option.
func (d *Postgres) setRange(ctx context.Context, tx dialect.Tx, name string, value int) error {
func (d *Postgres) setRange(ctx context.Context, tx dialect.Tx, t *Table, value int) error {
if value == 0 {
value = 1 // RESTART value cannot be < 1.
}
return tx.Exec(ctx, fmt.Sprintf("ALTER TABLE %s ALTER COLUMN id RESTART WITH %d", name, value), []interface{}{}, nil)
pk := "id"
if len(t.PrimaryKey) == 1 {
pk = t.PrimaryKey[0].Name
}
return tx.Exec(ctx, fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s RESTART WITH %d", t.Name, pk, value), []interface{}{}, nil)
}
// table loads the current table description from the database.