Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres - handle datatypes created in schemas not found in jobmappings #3164

Merged
merged 1 commit into from
Jan 18, 2025
Merged
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
2 changes: 1 addition & 1 deletion backend/gen/go/db/dbschemas/postgresql/system.sql.go

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

2 changes: 1 addition & 1 deletion backend/pkg/dbschemas/sql/postgresql/queries/system.sql
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ WITH custom_types AS (
JOIN
pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE
n.nspname = sqlc.arg('schema')
n.nspname NOT IN('pg_catalog', 'pg_toast', 'information_schema')
AND t.typtype IN ('d', 'e', 'c')
),
table_columns AS (
Expand Down
37 changes: 37 additions & 0 deletions backend/pkg/sqlmanager/postgres/postgres-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,15 @@ func (p *PostgresManager) GetSchemaInitStatements(
return nil
})

datatypes := &sqlmanager_shared.SchemaTableDataTypeResponse{}
dataTypeStmts := []string{}
errgrp.Go(func() error {
datatypeCfg, err := p.GetSchemaTableDataTypes(errctx, tables)
if err != nil {
return fmt.Errorf("unable to retrieve postgres schema table data types: %w", err)
}
dataTypeStmts = datatypeCfg.GetStatements()
datatypes = datatypeCfg
return nil
})

Expand Down Expand Up @@ -578,6 +580,9 @@ func (p *PostgresManager) GetSchemaInitStatements(
return nil, err
}

additionalSchemaStmts := getSchemaCreationStatementsFromDataTypes(tables, datatypes)
schemaStmts = append(schemaStmts, additionalSchemaStmts...)

return []*sqlmanager_shared.InitSchemaStatements{
{Label: SchemasLabel, Statements: schemaStmts},
{Label: "data types", Statements: dataTypeStmts},
Expand All @@ -589,6 +594,38 @@ func (p *PostgresManager) GetSchemaInitStatements(
}, nil
}

// Finds any schemas referenced in datatypes that don't exist in tables and returns the statements to create them
func getSchemaCreationStatementsFromDataTypes(tables []*sqlmanager_shared.SchemaTable, datatypes *sqlmanager_shared.SchemaTableDataTypeResponse) []string {
schemaStmts := []string{}
schemaSet := map[string]struct{}{}
for _, table := range tables {
schemaSet[table.Schema] = struct{}{}
}

// Check each datatype schema against the table schemas
for _, composite := range datatypes.Composites {
if _, exists := schemaSet[composite.Schema]; !exists {
schemaStmts = append(schemaStmts, fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %q;", composite.Schema))
schemaSet[composite.Schema] = struct{}{}
}
}

for _, enum := range datatypes.Enums {
if _, exists := schemaSet[enum.Schema]; !exists {
schemaStmts = append(schemaStmts, fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %q;", enum.Schema))
schemaSet[enum.Schema] = struct{}{}
}
}

for _, domain := range datatypes.Domains {
if _, exists := schemaSet[domain.Schema]; !exists {
schemaStmts = append(schemaStmts, fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %q;", domain.Schema))
schemaSet[domain.Schema] = struct{}{}
}
}
return schemaStmts
}

func wrapPgIdempotentIndex(
schema,
constraintname,
Expand Down
Loading