mirror of
https://github.com/ent/ent.git
synced 2026-05-01 23:20:53 +03:00
dialect/sql/schema: setrange on custom column name of pks (#333)
This commit is contained in:
@@ -45,19 +45,19 @@ func (d *SQLite) tableExist(ctx context.Context, tx dialect.Tx, name string) (bo
|
||||
// whenever a table that contains an AUTOINCREMENT column is created. However, it populates to it a rows (for tables)
|
||||
// only after the first insertion. Therefore, we check. If a record (for the given table) already exists in the "sqlite_sequence"
|
||||
// table, we updated it. Otherwise, we insert a new value.
|
||||
func (d *SQLite) setRange(ctx context.Context, tx dialect.Tx, name string, value int) error {
|
||||
func (d *SQLite) setRange(ctx context.Context, tx dialect.Tx, t *Table, value int) error {
|
||||
query, args := sql.Select().Count().
|
||||
From(sql.Table("sqlite_sequence")).
|
||||
Where(sql.EQ("name", name)).
|
||||
Where(sql.EQ("name", t.Name)).
|
||||
Query()
|
||||
exists, err := exist(ctx, tx, query, args...)
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
case exists:
|
||||
query, args = sql.Update("sqlite_sequence").Set("seq", value).Where(sql.EQ("name", name)).Query()
|
||||
query, args = sql.Update("sqlite_sequence").Set("seq", value).Where(sql.EQ("name", t.Name)).Query()
|
||||
default: // !exists
|
||||
query, args = sql.Insert("sqlite_sequence").Columns("name", "seq").Values(name, value).Query()
|
||||
query, args = sql.Insert("sqlite_sequence").Columns("name", "seq").Values(t.Name, value).Query()
|
||||
}
|
||||
return tx.Exec(ctx, query, args, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user