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

@@ -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)
}