Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion internal/queries/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ WITH identity_col_seq AS (
depend.refobjid = owner_attr.attrelid
AND depend.refobjsubid = owner_attr.attnum
WHERE owner_attr.attidentity != ''
),

dep_on_column AS (
SELECT
a.attrelid AS src_relid,
a.attnum AS src_attnum,
dep_a.attname AS tgt_name
FROM pg_catalog.pg_attribute AS a
INNER JOIN
pg_catalog.pg_depend AS dep
ON
a.attrelid = dep.objid
AND a.attnum = dep.objsubid
AND dep.classid = 'pg_class'::REGCLASS
INNER JOIN
pg_catalog.pg_attribute AS dep_a
ON dep.refobjid = dep_a.attrelid AND dep.refobjsubid = dep_a.attnum
)

SELECT
Expand All @@ -84,8 +101,15 @@ SELECT
COALESCE(collation_namespace.nspname, '')::TEXT AS collation_schema_name,
COALESCE(
pg_catalog.pg_get_expr(d.adbin, d.adrelid), ''
)::TEXT AS default_value,
)::TEXT AS attr_def,
a.attnotnull AS is_not_null,
(
SELECT ARRAY_AGG(dep_on_column.tgt_name)
FROM dep_on_column
WHERE
a.attrelid = dep_on_column.src_relid
AND a.attnum = dep_on_column.src_attnum
)::TEXT [] AS dep_on_column_names,
a.attlen AS column_size,
a.attidentity::TEXT AS identity_type,
identity_col_seq.seqstart AS start_value,
Expand All @@ -94,6 +118,7 @@ SELECT
identity_col_seq.seqmin AS min_value,
identity_col_seq.seqcache AS cache_size,
identity_col_seq.seqcycle AS is_cycle,
a.attgenerated = 's' AS is_generated,
pg_catalog.format_type(a.atttypid, a.atttypmod) AS column_type
FROM pg_catalog.pg_attribute AS a
LEFT JOIN
Expand Down
35 changes: 32 additions & 3 deletions internal/queries/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 29 additions & 5 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ func (s Schema) Normalize() Schema {
func normalizeTable(t Table) Table {
// Don't normalize columns order. their order is derived from the postgres catalogs
// (relevant to data packing)
var normColumns []Column
for _, c := range t.Columns {
c.DependsOnColumns = sortByKey(c.DependsOnColumns, func(s string) string {
return s
})
normColumns = append(normColumns, c)
}
t.Columns = normColumns

var normCheckConstraints []CheckConstraint
for _, checkConstraint := range sortSchemaObjectsByName(t.CheckConstraints) {
checkConstraint.DependsOnFunctions = sortSchemaObjectsByName(checkConstraint.DependsOnFunctions)
Expand All @@ -115,6 +124,7 @@ func normalizeTable(t Table) Table {
normPolicies = append(normPolicies, p)
}
t.Policies = normPolicies

return t
}

Expand Down Expand Up @@ -228,8 +238,13 @@ type (
// ''::text
// CURRENT_TIMESTAMP
// If empty, indicates that there is no default value.
Default string
IsNullable bool
Default string
// If the column is generated, this will be a SQL string representing the generated expression.
// If empty, indicates that there is no default value.
GeneratedExpr string
IsNullable bool
// DependsOnColumns is a list of (unescaped) column names that this column depends on.
DependsOnColumns []string
// Size is the number of bytes required to store the value.
// It is used for data-packing purposes
Size int
Expand Down Expand Up @@ -878,6 +893,13 @@ func (s *schemaFetcher) buildTable(
}
}

defaultExpr := column.AttrDef
generatedExpr := ""
if column.IsGenerated {
defaultExpr = ""
generatedExpr = column.AttrDef
}

columns = append(columns, Column{
Name: column.ColumnName,
Type: column.ColumnType,
Expand All @@ -888,9 +910,11 @@ func (s *schemaFetcher) buildTable(
// ''::text
// CURRENT_TIMESTAMP
// If empty, indicates that there is no default value.
Default: column.DefaultValue,
Size: int(column.ColumnSize),
Identity: identity,
Default: defaultExpr,
GeneratedExpr: generatedExpr,
DependsOnColumns: column.DepOnColumnNames,
Size: int(column.ColumnSize),
Identity: identity,
})
}

Expand Down
Loading
Loading