Skip to content

Commit

Permalink
fix: Support Spanner ROW DELETION POLICY (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Jun 6, 2024
2 parents 032d596 + 0ffe6c8 commit 834c015
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/show/spanner/show_create_all_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ type informationSchemaTable struct {
TableName string `db:"TABLE_NAME"`
}

const (
querySelectTableOptionRowDeletionPolicy = `SELECT TABLE_NAME, ROW_DELETION_POLICY_EXPRESSION FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '' AND TABLE_NAME = ? AND ROW_DELETION_POLICY_EXPRESSION != '';`
)

type informationSchemaTableOptionRowDeletionPolicy struct {
TableName string `db:"TABLE_NAME"`
RowDeletionPolicyExpression string `db:"ROW_DELETION_POLICY_EXPRESSION"`
}

const (
queryShowCreateAllTables = `SELECT TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, SPANNER_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? ORDER BY TABLE_NAME, ORDINAL_POSITION;`
)
Expand Down Expand Up @@ -235,6 +244,19 @@ func ShowCreateAllTables(ctx context.Context, db sqlQueryerContext, opts ...Show
d += ")"
}

tableOptionRowDeletionPolicy := make([]*informationSchemaTableOptionRowDeletionPolicy, 0)
if err := dbz.QueryContext(ctx, &tableOptionRowDeletionPolicy, querySelectTableOptionRowDeletionPolicy, tbl.TableName); err != nil {
return "", apperr.Errorf("dbz.QueryContext: %w", err)
}

if len(tableOptionRowDeletionPolicy) > 0 {
d += ",\nROW DELETION POLICY ("
for _, opt := range tableOptionRowDeletionPolicy {
d += opt.RowDeletionPolicyExpression
}
d += ")"
}

// append table
query += d + ";\n"

Expand Down

0 comments on commit 834c015

Please sign in to comment.