Skip to content

Commit

Permalink
Update dependency system
Browse files Browse the repository at this point in the history
  • Loading branch information
bplunkett-stripe committed Sep 30, 2024
1 parent a4d0db5 commit 7956651
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 162 deletions.
4 changes: 2 additions & 2 deletions pkg/diff/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func (p MigrationHazard) String() string {
type Statement struct {
DDL string
// Timeout is the statement_timeout to apply to this statement. If implementing your own plan executor, be sure to set
// the session-level statement_timeout to this value before executing the statement. A transaction-level statement_timeout
// the session-level statement_timeout to this value beforeSchemaObj executing the statement. A transaction-level statement_timeout
// will not work since building indexes concurrently cannot be done in a transaction
Timeout time.Duration
// LockTimeout is the lock_timeout to apply to this statement. If implementing your own plan executor, be sure to set
// the session-level lock_timeout to this value before executing the statement. A transaction-level lock_timeout
// the session-level lock_timeout to this value beforeSchemaObj executing the statement. A transaction-level lock_timeout
// will not work since building indexes concurrently cannot be done in a transaction
LockTimeout time.Duration
// The hazards this statement poses
Expand Down
4 changes: 2 additions & 2 deletions pkg/diff/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestPlan_InsertStatement(t *testing.T) {
},
},
{
name: "insert after first statement",
name: "insert afterSchemaObj first statement",
plan: diff.Plan{
Statements: []diff.Statement{
{DDL: "statement 1", Timeout: time.Second},
Expand All @@ -304,7 +304,7 @@ func TestPlan_InsertStatement(t *testing.T) {
},
},
{
name: "insert after last statement statement",
name: "insert afterSchemaObj last statement statement",
plan: diff.Plan{
Statements: []diff.Statement{
{DDL: "statement 1", Timeout: time.Second},
Expand Down
22 changes: 11 additions & 11 deletions pkg/diff/policy_sql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ var (
//
// If not done in this order, we may create an outtage for a user's queries where RLS rejects their queries because
// the policy allowing them hasn't been created yet. The same is true for disabling RLS, but in the reverse order. RLS
// must be disabled before policies are dropped.
// must be disabled beforeSchemaObj policies are dropped.
//
// Another quirk of policies: Policies on partitions must be dropped before the base table is altered, otherwise
// Another quirk of policies: Policies on partitions must be dropped beforeSchemaObj the base table is altered, otherwise
// the SQL could fail because, e.g., the policy references a column that no longer exists.

func enableRLSForTable(t schema.Table) Statement {
Expand Down Expand Up @@ -272,29 +272,29 @@ func buildPolicyVertexId(owningTable schema.SchemaQualifiedName, policyEscapedNa

func (psg *policySQLVertexGenerator) GetAddAlterDependencies(newPolicy, oldPolicy schema.Policy) ([]dependency, error) {
deps := []dependency{
mustRun(psg.GetSQLVertexId(newPolicy), diffTypeDelete).before(psg.GetSQLVertexId(newPolicy), diffTypeAddAlter),
mustRun(psg.GetSQLVertexId(newPolicy), diffTypeDelete).beforeSchemaObj(psg.GetSQLVertexId(newPolicy), diffTypeAddAlter),
}

newTargetColumns, err := getTargetColumns(newPolicy.Columns, psg.newSchemaColumnsByName)
if err != nil {
return nil, fmt.Errorf("getting target columns: %w", err)
}

// Run after the new columns are added/altered
// Run afterSchemaObj the new columns are added/altered
for _, tc := range newTargetColumns {
deps = append(deps, mustRun(psg.GetSQLVertexId(newPolicy), diffTypeAddAlter).after(buildColumnVertexId(tc.Name), diffTypeAddAlter))
deps = append(deps, mustRun(psg.GetSQLVertexId(newPolicy), diffTypeAddAlter).afterSchemaObj(buildColumnVertexId(tc.Name), diffTypeAddAlter))
}

if !cmp.Equal(oldPolicy, schema.Policy{}) {
// Run before the old columns are deleted (if they are deleted)
// Run beforeSchemaObj the old columns are deleted (if they are deleted)
oldTargetColumns, err := getTargetColumns(oldPolicy.Columns, psg.oldSchemaColumnsByName)
if err != nil {
return nil, fmt.Errorf("getting target columns: %w", err)
}
for _, tc := range oldTargetColumns {
// It only needs to run before the delete if the column is actually being deleted
// It only needs to run beforeSchemaObj the delete if the column is actually being deleted
if _, stillExists := psg.newSchemaColumnsByName[tc.GetName()]; !stillExists {
deps = append(deps, mustRun(psg.GetSQLVertexId(newPolicy), diffTypeAddAlter).before(buildColumnVertexId(tc.Name), diffTypeDelete))
deps = append(deps, mustRun(psg.GetSQLVertexId(newPolicy), diffTypeAddAlter).beforeSchemaObj(buildColumnVertexId(tc.Name), diffTypeDelete))
}
}
}
Expand All @@ -309,10 +309,10 @@ func (psg *policySQLVertexGenerator) GetDeleteDependencies(pol schema.Policy) ([
if err != nil {
return nil, fmt.Errorf("getting target columns: %w", err)
}
// The policy needs to be deleted before all the columns it references are deleted or add/altered
// The policy needs to be deleted beforeSchemaObj all the columns it references are deleted or add/altered
for _, c := range columns {
deps = append(deps, mustRun(psg.GetSQLVertexId(pol), diffTypeDelete).before(buildColumnVertexId(c.Name), diffTypeDelete))
deps = append(deps, mustRun(psg.GetSQLVertexId(pol), diffTypeDelete).before(buildColumnVertexId(c.Name), diffTypeAddAlter))
deps = append(deps, mustRun(psg.GetSQLVertexId(pol), diffTypeDelete).beforeSchemaObj(buildColumnVertexId(c.Name), diffTypeDelete))
deps = append(deps, mustRun(psg.GetSQLVertexId(pol), diffTypeDelete).beforeSchemaObj(buildColumnVertexId(c.Name), diffTypeAddAlter))
}

return deps, nil
Expand Down
Loading

0 comments on commit 7956651

Please sign in to comment.