-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add foreign key constraint for network ID
- Loading branch information
Showing
8 changed files
with
100 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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
1 change: 1 addition & 0 deletions
1
internal/persistence/sql/migrations/sql/20220217152313000000_nid_fk.down.sql
This file contains 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 @@ | ||
ALTER TABLE keto_relation_tuples DROP CONSTRAINT keto_relation_tuples_nid_fk; |
29 changes: 29 additions & 0 deletions
29
internal/persistence/sql/migrations/sql/20220217152313000000_nid_fk.sqlite.down.sql
This file contains 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,29 @@ | ||
CREATE TABLE keto_relation_tuples_no_fk | ||
( | ||
shard_id UUID NOT NULL, | ||
nid UUID NOT NULL, | ||
namespace_id INTEGER NOT NULL, | ||
object VARCHAR(64) NOT NULL, | ||
relation VARCHAR(64) NOT NULL, | ||
subject_id VARCHAR(64) NULL, | ||
subject_set_namespace_id INTEGER NULL, | ||
subject_set_object VARCHAR(64) NULL, | ||
subject_set_relation VARCHAR(64) NULL, | ||
commit_time TIMESTAMP NOT NULL, | ||
|
||
PRIMARY KEY (shard_id, nid), | ||
|
||
-- enforce to have exactly one of subject_id or subject_set | ||
CONSTRAINT chk_keto_rt_subject_type CHECK | ||
((subject_id IS NULL AND | ||
subject_set_namespace_id IS NOT NULL AND subject_set_object IS NOT NULL AND subject_set_relation IS NOT NULL) | ||
OR | ||
(subject_id IS NOT NULL AND | ||
subject_set_namespace_id IS NULL AND subject_set_object IS NULL AND subject_set_relation IS NULL)) | ||
); | ||
|
||
INSERT INTO keto_relation_tuples_no_fk (shard_id, nid, namespace_id, object, relation, subject_id, subject_set_namespace_id, subject_set_object, subject_set_relation, commit_time) SELECT * FROM keto_relation_tuples; | ||
|
||
DROP TABLE keto_relation_tuples; | ||
|
||
ALTER TABLE keto_relation_tuples_no_fk RENAME TO keto_relation_tuples; |
35 changes: 35 additions & 0 deletions
35
internal/persistence/sql/migrations/sql/20220217152313000000_nid_fk.sqlite.up.sql
This file contains 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,35 @@ | ||
-- These tuples can't be retrieved over any API (and also not inserted by any API) because we always add a `WHERE nid = ?` clause to the queries. | ||
-- This DELETE statement is here to ensure that we don't run into problems where the `nid` column is not properly set. Should not happen if only the official API is used. | ||
DELETE FROM keto_relation_tuples WHERE nid NOT IN (SELECT nid FROM networks); | ||
|
||
CREATE TABLE keto_relation_tuples_with_fk | ||
( | ||
shard_id UUID NOT NULL, | ||
nid UUID NOT NULL, | ||
namespace_id INTEGER NOT NULL, | ||
object VARCHAR(64) NOT NULL, | ||
relation VARCHAR(64) NOT NULL, | ||
subject_id VARCHAR(64) NULL, | ||
subject_set_namespace_id INTEGER NULL, | ||
subject_set_object VARCHAR(64) NULL, | ||
subject_set_relation VARCHAR(64) NULL, | ||
commit_time TIMESTAMP NOT NULL, | ||
|
||
PRIMARY KEY (shard_id, nid), | ||
|
||
CONSTRAINT keto_relation_tuples_nid_fk FOREIGN KEY (nid) REFERENCES networks (nid), | ||
|
||
-- enforce to have exactly one of subject_id or subject_set | ||
CONSTRAINT chk_keto_rt_subject_type CHECK | ||
((subject_id IS NULL AND | ||
subject_set_namespace_id IS NOT NULL AND subject_set_object IS NOT NULL AND subject_set_relation IS NOT NULL) | ||
OR | ||
(subject_id IS NOT NULL AND | ||
subject_set_namespace_id IS NULL AND subject_set_object IS NULL AND subject_set_relation IS NULL)) | ||
); | ||
|
||
INSERT INTO keto_relation_tuples_with_fk (shard_id, nid, namespace_id, object, relation, subject_id, subject_set_namespace_id, subject_set_object, subject_set_relation, commit_time) SELECT * FROM keto_relation_tuples; | ||
|
||
DROP TABLE keto_relation_tuples; | ||
|
||
ALTER TABLE keto_relation_tuples_with_fk RENAME TO keto_relation_tuples; |
7 changes: 7 additions & 0 deletions
7
internal/persistence/sql/migrations/sql/20220217152313000000_nid_fk.up.sql
This file contains 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,7 @@ | ||
-- These tuples can't be retrieved over any API (and also not inserted by any API) because we always add a `WHERE nid = ?` clause to the queries. | ||
-- This DELETE statement is here to ensure that we don't run into problems where the `nid` column is not properly set. Should not happen if only the official API is used. | ||
DELETE FROM keto_relation_tuples WHERE nid NOT IN (SELECT nid FROM networks); | ||
|
||
ALTER TABLE keto_relation_tuples | ||
ADD CONSTRAINT keto_relation_tuples_nid_fk | ||
FOREIGN KEY (nid) REFERENCES networks (id); |
This file contains 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 @@ | ||
* |
This file contains 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 @@ | ||
# The fizz templates are frozen at this point. Add SQL migrations right in `./sql` |