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 support for foreign key constraints in create_table operation #597

Merged
merged 56 commits into from
Jan 23, 2025

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Jan 16, 2025

This PR adds support for foreign_key constraints in table constraint definitions in create_table.

It includes all settings that are currently available in PostgreSQL 17. At the moment, this option is superior to other foreign key definitions in pgroll because it supports ON UPDATE and MATCH type settings. As I mentioned in earlier PRs, once this work is finished, I am unifying the constraint options in all operations.

Example constraint configuration:

{
  "name": "provider_fk",
  "type": "foreign_key",
  "columns": [
    "provider_id",
    "product_id"
  ],
  "deferrable": false,
  "references": {
    "table": "provider",
    "columns": [
      "id",
      "product_id"
    ],
    "on_delete": "SET DEFAULT",
    "on_delete_set_columns": ["id"],
    "on_update": "CASCADE",
    "match_type": "SIMPLE"
  }
}

@kvch kvch requested a review from andrew-farries January 21, 2025 18:33
@@ -40,6 +44,14 @@ func ExecuteTests(t *testing.T, tests TestCases, opts ...roll.Option) {
testSchema := testutils.TestSchema()

for _, tt := range tests {
if tt.minPgMajorVersion != 0 && os.Getenv("POSTGRES_VERSION") != "" {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to add this because setting a column list for ON DELETE actions was released in pg15.

@kvch kvch requested a review from andrew-farries January 22, 2025 15:06
@@ -40,6 +44,14 @@ func ExecuteTests(t *testing.T, tests TestCases, opts ...roll.Option) {
testSchema := testutils.TestSchema()

for _, tt := range tests {
if tt.minPgMajorVersion != 0 && os.Getenv("POSTGRES_VERSION") != "" {
pgMajorVersion := strings.Split(os.Getenv("POSTGRES_VERSION"), ".")[0]
version, _ := strconv.Atoi(pgMajorVersion)
Copy link
Collaborator

Choose a reason for hiding this comment

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

POSTGRES_VERSION is set to latest as part of the CI test matrix, ie not necessarily a Major.Minor string.

It's really a Postgres docker image tag, not strictly a postgres version number.

Maybe put this logic in a function and handle the case where it isn't Major.Minor release number.

@kvch kvch requested a review from andrew-farries January 22, 2025 16:31
@kvch kvch enabled auto-merge (squash) January 23, 2025 12:50
@kvch kvch merged commit b40621b into xataio:main Jan 23, 2025
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants