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

Online foreign key support #141

Merged
merged 1 commit into from
Jul 8, 2024
Merged

Online foreign key support #141

merged 1 commit into from
Jul 8, 2024

Conversation

bplunkett-stripe
Copy link
Collaborator

@bplunkett-stripe bplunkett-stripe commented Jul 7, 2024

Description

Online foreign key support via VALIDATE.

Unfortunately, it is not possible to add NOT VALID constraints to partitioned tables, so this strategy can only work when adding foreign keys to normal tables. It does work on partitions, so if a user really wanted to add foreign key constraints safely on a partitioned table, they could just add n foreign key constraints, one per partition, rather than just 1.

Motivation

Fixes #97

Testing

Tested via acceptance tests

@bplunkett-stripe bplunkett-stripe added the enhancement New feature or request label Jul 7, 2024
@@ -1970,23 +1986,77 @@ func (a *attachPartitionSQLVertexGenerator) GetDeleteDependencies(_ schema.Table
return nil, nil
}

func buildForeignKeyConstraintDiff(fsg *foreignKeyConstraintSQLVertexGenerator, addedTablesByName map[string]schema.Table, old, new schema.ForeignKeyConstraint) (foreignKeyConstraintDiff, bool, error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This aligns more with the new paradigm of sql vertex generators: the diff function should call into the sql vertex generator (if possible) to reduce duplicated logic.

Copy link
Collaborator

@alexaub-stripe alexaub-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Couple comments non-blocking

// buildChildrenByPartitionedIndexNameMap builds a map of indexes by their parent index name. This map will include
// all descendents, not just direct descendents. For example, if foobar idx has 5 children and each of those children
// has 5 children, the slice for foobar index wil contain 30 indexes (the 5 direct children and the 25 "grandchildren")
func buildChildrenByPartitionedIndexNameMap(indexes []schema.Index) map[string][]schema.Index {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: Could this function be a fair amount simpler with a findRoot(node, nodesByParent) helper? You avoid having to reason about popping/pushing onto currentNodes too.

  1. Build childrenByDirectParent map
  2. For each index, call findRoot(index, childrenByDirectParent)
  3. Add child to root entry in output map

Optionally you could memoize findRoot if you care about performance

@@ -1970,23 +1986,77 @@ func (a *attachPartitionSQLVertexGenerator) GetDeleteDependencies(_ schema.Table
return nil, nil
}

func buildForeignKeyConstraintDiff(fsg *foreignKeyConstraintSQLVertexGenerator, addedTablesByName map[string]schema.Table, old, new schema.ForeignKeyConstraint) (foreignKeyConstraintDiff, bool, error) {
if _, isOnNewTable := addedTablesByName[new.OwningTable.GetName()]; isOnNewTable {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does addedTablesByName include the case where the owning/referencing tables are themselves recreated? Or is it only net new tables?

@bplunkett-stripe bplunkett-stripe merged commit e5eafa8 into main Jul 8, 2024
9 checks passed
@bplunkett-stripe bplunkett-stripe deleted the bplunkett/online-fks branch July 8, 2024 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for online Foreign Keys via VALIDATE
2 participants