-
Notifications
You must be signed in to change notification settings - Fork 161
Base path db constaint #7952
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mdellweg
wants to merge
2
commits into
pulp:main
Choose a base branch
from
mdellweg:base_path_db_constaint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Base path db constaint #7952
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
pulpcore/app/migrations/0157_distribution_base_path_constraint.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # Generated by Django 5.2.17 on 2026-08-11 10:34 | ||
|
|
||
| from django.db import migrations | ||
|
|
||
|
|
||
| ADD_TRIGGER = r""" | ||
| CREATE OR REPLACE FUNCTION path_prefixes("path" text) | ||
| RETURNS text[] AS $$ | ||
| DECLARE | ||
| segment text; | ||
| segments text[] := string_to_array(path, '/'); | ||
| prefix text := ''; | ||
| prefixes text[]; | ||
| BEGIN | ||
| -- Return directory prefixes of a path. | ||
| -- 'a/b/c' -> {'a', 'a/b'} | ||
| prefix := segments[1]; | ||
| FOREACH segment IN ARRAY segments[2:] | ||
| LOOP | ||
| prefixes := prefixes || prefix; | ||
| prefix := prefix || '/' || segment; | ||
| END LOOP; | ||
| RETURN prefixes; | ||
| END; | ||
| $$ LANGUAGE plpgsql | ||
| IMMUTABLE | ||
| RETURNS NULL ON NULL INPUT | ||
| PARALLEL SAFE; | ||
|
|
||
| CREATE OR REPLACE FUNCTION "check_core_distribution_base_path_prefix_free" () | ||
| RETURNS TRIGGER AS $$ | ||
| DECLARE | ||
| base_path_slash text := new.base_path || '/'; | ||
| BEGIN | ||
| -- Check that no base_path is a prefix of another. | ||
| -- The normalization of "/" ensures that a simple string comparison actually suffice. | ||
| -- WARNING: This check alone does not ensure uniqueness. | ||
|
|
||
| -- ^@ seems to only use an index when the indexed expression is on the left (pg18). | ||
| PERFORM 1 FROM "core_distribution" | ||
| WHERE | ||
| "pulp_id" != new."pulp_id" | ||
| AND | ||
| "pulp_domain_id" = new."pulp_domain_id" | ||
| AND | ||
| "base_path" ^@ base_path_slash | ||
| LIMIT 1; | ||
| IF FOUND THEN | ||
| RAISE EXCEPTION '"%" is the prefix of an existing base_path.', new."base_path" | ||
| USING ERRCODE = 'exclusion_violation'; | ||
| END IF; | ||
|
|
||
| -- This variant however uses the existing uniqueness index. | ||
| PERFORM 1 FROM "core_distribution" | ||
| WHERE | ||
| "pulp_id" != new."pulp_id" | ||
| AND | ||
| "pulp_domain_id" = new."pulp_domain_id" | ||
| AND | ||
| "base_path" = ANY(path_prefixes(new."base_path")) | ||
| LIMIT 1; | ||
| IF FOUND THEN | ||
| RAISE EXCEPTION '"%" is prefixed by an existing base_path.', new."base_path" | ||
| USING ERRCODE = 'exclusion_violation'; | ||
| END IF; | ||
|
|
||
| RETURN new; | ||
| END | ||
| $$ LANGUAGE plpgsql; | ||
|
|
||
| CREATE CONSTRAINT TRIGGER "core_distribution_insert_base_path_overlap_constraint" | ||
| AFTER INSERT | ||
| ON "core_distribution" | ||
| DEFERRABLE INITIALLY DEFERRED | ||
| FOR EACH ROW | ||
| WHEN (new."base_path" IS NOT NULL) | ||
| EXECUTE FUNCTION "check_core_distribution_base_path_prefix_free" (); | ||
|
|
||
| CREATE CONSTRAINT TRIGGER "core_distribution_update_base_path_overlap_constraint" | ||
| AFTER UPDATE | ||
| ON "core_distribution" | ||
| DEFERRABLE INITIALLY DEFERRED | ||
| FOR EACH ROW | ||
| WHEN (new."base_path" IS NOT NULL AND new."base_path" != old."base_path") | ||
| EXECUTE FUNCTION "check_core_distribution_base_path_prefix_free" (); | ||
| """ | ||
|
|
||
| REMOVE_TRIGGER = r""" | ||
| DROP TRIGGER IF EXISTS "core_distribution_update_base_path_overlap_constraint" ON "core_distribution"; | ||
| DROP TRIGGER IF EXISTS "core_distribution_insert_base_path_overlap_constraint" ON "core_distribution"; | ||
| DROP FUNCTION IF EXISTS "check_core_distribution_base_path_prefix_free"; | ||
| DROP FUNCTION IF EXISTS "path_prefixes"; | ||
| """ | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('core', '0156_alter_contentartifact_relative_path_and_more'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunSQL(sql=ADD_TRIGGER, reverse_sql=REMOVE_TRIGGER, elidable=False), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import pytest | ||
| from django.db import connection | ||
| from django.db.utils import IntegrityError | ||
|
|
||
| from pulpcore.app.models import Distribution | ||
|
|
||
| # "SET CONSTRAINTS ALL IMMEDIATE" is automatically called by the fixtures teardown. | ||
| # We only need to do it manually when we need the exception during the test. | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| class TestDistributionBasePathConstraint: | ||
| def test_must_be_unique(self): | ||
| Distribution(name="0", base_path="a").save() | ||
| with pytest.raises(IntegrityError, match="unique constraint"): | ||
| Distribution(name="1", base_path="a").save() | ||
|
|
||
| def test_can_share_a_prefix_with_another_base_path(self): | ||
| Distribution(name="0", base_path="a/a").save() | ||
| Distribution(name="1", base_path="a/b").save() | ||
|
|
||
| def test_cannot_be_the_prefix_of_another_base_path(self): | ||
| Distribution(name="0", base_path="a/a").save() | ||
| Distribution(name="1", base_path="a").save() | ||
| with pytest.raises(IntegrityError, match="prefix"): | ||
| with connection.cursor() as cursor: | ||
| cursor.execute("SET CONSTRAINTS ALL IMMEDIATE") | ||
|
|
||
| def test_cannot_contain_another_base_path_as_prefix(self): | ||
| Distribution(name="0", base_path="a").save() | ||
| Distribution(name="1", base_path="a/a").save() | ||
| with pytest.raises(IntegrityError, match="prefix"): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we set |
||
| with connection.cursor() as cursor: | ||
| cursor.execute("SET CONSTRAINTS ALL IMMEDIATE") | ||
|
|
||
| def test_prefixes_are_checke_at_slash_boundaries(self): | ||
| Distribution(name="0", base_path="abc").save() | ||
| Distribution(name="1", base_path="ab").save() | ||
| Distribution(name="2", base_path="abcd").save() | ||
| Distribution(name="3", base_path="abcde/a").save() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
馃憤 Nice removal!