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

Add foreign key support #53

Merged
merged 5 commits into from
Aug 10, 2023
Merged

Conversation

bplunkett-stripe
Copy link
Collaborator

@bplunkett-stripe bplunkett-stripe commented Aug 7, 2023

Description

Add foreign key support

Motivation

#49

Testing

Tested with acceptance tests

if !con.IsInheritable {
sb.WriteString(" NO INHERIT")
}

if !con.IsValid {
sb.WriteString(" NOT VALID")
} else if !csg.isNewTable {
hazards = append(hazards, MigrationHazard{
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We were missing this hazard previously

@bplunkett-stripe bplunkett-stripe added the enhancement New feature or request label Aug 7, 2023
@bplunkett-stripe bplunkett-stripe force-pushed the bplunkett/foreign-key-support branch from a1242f2 to 1475640 Compare August 8, 2023 00:50
alexaub-stripe
alexaub-stripe previously approved these changes Aug 10, 2023
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.

Great! Really cool to see what plugging in a new feature like this is like today.

WHERE
constraint_namespace.nspname = 'public'
AND pg_constraint.contype = 'f'
AND pg_constraint.conislocal;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this imply that fk constraints for partitioned tables will be handled only at the parent table level?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. The foreign key will trickle down automatically for us. We don't need to do any complicated stuff like we do for indexes where we attach the foreign key

@@ -42,150 +42,6 @@ var (
}

schemaMigrationPlanTestCases = []schemaMigrationPlanTestCase{
{
name: "No-op",
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the purpose of removing these test cases?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The schema migration plan cases test "how" the migration is done, not just the migration's corectness. The latter are tested by the acceptance tests.

Since there are many "no-op" acceptance tests that assert the generated plan is empty, we don't need this test case. I'm trying to minimize the number of these test cases, since it's tricky to maintain the before and after schemas.

Comment on lines +462 to +463
deletedTablesByName: deletedTablesByName,
addedTablesByName: addedTablesByName,
Copy link
Collaborator

Choose a reason for hiding this comment

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

double checking: Will this include recreated tables?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes! We validate this works in the test cases that change a table from partitioned to unpartitioned and vice-versa.

if !strings.HasSuffix(diff.old.ConstraintDef, " NOT VALID") {
return nil, fmt.Errorf("expected the old constraint def to be suffixed with NOT VALID: %q", diff.old.ConstraintDef)
}
diff.old.ConstraintDef = strings.TrimSuffix(diff.old.ConstraintDef, " NOT VALID")
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Is this just so the cmp.Equal on 1482 works? Might make sense to just do that inline on 1482

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 is a really good question. This is a pattern of the Alter functions that I'm switching too.

The Alter functions need to keep track of what they can successfully alter and what they can't. If they can't successfully resolve a diff, then ideally they return an error to the user.

I found the easiest way to do this is:
For each alterable difference,

  • generate a statement to alter the difference
  • update a copy of old to include the difference
    Finally, compare old to new. If they are equal, then there were no unresolvable diffs.

With the case of invalid to valid, we need both update the old to be valid and the constraint def in order for the equality check to pass. The strings.HasSuffix is more of a confidence check that the constraint def follows the expected format.

Copy link
Collaborator

Choose a reason for hiding this comment

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

SGTM if you want to do this for consistency with that pattern 👍

pkg/diff/sql_generator.go Outdated Show resolved Hide resolved
Co-authored-by: alexaub-stripe <130488060+alexaub-stripe@users.noreply.github.com>
@bplunkett-stripe bplunkett-stripe merged commit 702737f into main Aug 10, 2023
@bplunkett-stripe bplunkett-stripe deleted the bplunkett/foreign-key-support branch August 10, 2023 16:43
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.

2 participants