dialect/sql/schema: schema match from predicate

This commit is contained in:
Ariel Mashraki
2021-01-18 12:30:00 +02:00
committed by Ariel Mashraki
parent 7e904f0e1c
commit cc8da8fbf7
3 changed files with 38 additions and 28 deletions

View File

@@ -54,7 +54,7 @@ func (d *Postgres) tableExist(ctx context.Context, tx dialect.Tx, name string) (
query, args := sql.Dialect(dialect.Postgres).
Select(sql.Count("*")).From(sql.Table("tables").Schema("information_schema")).
Where(sql.And(
sql.EQ("table_schema", d.tableSchema()),
d.matchSchema(),
sql.EQ("table_name", name),
)).Query()
return exist(ctx, tx, query, args...)
@@ -65,7 +65,7 @@ func (d *Postgres) fkExist(ctx context.Context, tx dialect.Tx, name string) (boo
query, args := sql.Dialect(dialect.Postgres).
Select(sql.Count("*")).From(sql.Table("table_constraints").Schema("information_schema")).
Where(sql.And(
sql.EQ("table_schema", d.tableSchema()),
d.matchSchema(),
sql.EQ("constraint_type", "FOREIGN KEY"),
sql.EQ("constraint_name", name),
)).Query()
@@ -91,7 +91,7 @@ func (d *Postgres) table(ctx context.Context, tx dialect.Tx, name string) (*Tabl
Select("column_name", "data_type", "is_nullable", "column_default", "udt_name").
From(sql.Table("columns").Schema("information_schema")).
Where(sql.And(
sql.EQ("table_schema", d.tableSchema()),
d.matchSchema(),
sql.EQ("table_name", name),
)).Query()
if err := tx.Query(ctx, query, args, rows); err != nil {
@@ -173,8 +173,10 @@ ORDER BY index_name, seq_in_index;
// indexesQuery returns the query (and its placeholders) for getting table indexes.
func (d *Postgres) indexesQuery(table string) (string, []interface{}) {
expr, args := d.tableSchema().Query()
return fmt.Sprintf(indexesQuery, expr, table), args
if d.schema != "" {
return fmt.Sprintf(indexesQuery, "$1", table), []interface{}{d.schema}
}
return fmt.Sprintf(indexesQuery, "CURRENT_SCHEMA()", table), nil
}
func (d *Postgres) indexes(ctx context.Context, tx dialect.Tx, table string) (Indexes, error) {
@@ -405,7 +407,7 @@ func (d *Postgres) dropIndex(ctx context.Context, tx dialect.Tx, idx *Index, tab
query, args := sql.Dialect(dialect.Postgres).
Select(sql.Count("*")).From(sql.Table("table_constraints").Schema("information_schema")).
Where(sql.And(
sql.EQ("table_schema", d.tableSchema()),
d.matchSchema(),
sql.EQ("constraint_type", "UNIQUE"),
sql.EQ("constraint_name", name),
)).
@@ -444,12 +446,16 @@ func (d *Postgres) renameIndex(t *Table, old, new *Index) sql.Querier {
return sql.Dialect(dialect.Postgres).AlterIndex(old.realname).Rename(new.Name)
}
// tableSchema returns the query for getting the table schema.
func (d *Postgres) tableSchema() sql.Querier {
if d.schema != "" {
return sql.Expr("?", d.schema)
// matchSchema returns the predicate for matching table schema.
func (d *Postgres) matchSchema(columns ...string) *sql.Predicate {
column := "table_schema"
if len(columns) > 0 {
column = columns[0]
}
return sql.Raw("CURRENT_SCHEMA()")
if d.schema != "" {
return sql.EQ(column, d.schema)
}
return sql.EQ(column, sql.Raw("CURRENT_SCHEMA()"))
}
// tables returns the query for getting the in the schema.
@@ -457,7 +463,7 @@ func (d *Postgres) tables() sql.Querier {
return sql.Dialect(dialect.Postgres).
Select("table_name").
From(sql.Table("tables").Schema("information_schema")).
Where(sql.EQ("table_schema", d.tableSchema()))
Where(d.matchSchema())
}
// alterColumns returns the queries for applying the columns change-set.