Schema: fix more missing cascades
This commit is contained in:
parent
3112dd7ad1
commit
e3759a2e5c
|
@ -0,0 +1,37 @@
|
||||||
|
"""Fix missing cascades
|
||||||
|
|
||||||
|
Revision ID: 8bf166ebda01
|
||||||
|
Revises: dd0274654d4e
|
||||||
|
Create Date: 2022-11-03 13:23:36.572482
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '8bf166ebda01'
|
||||||
|
down_revision = 'dd0274654d4e'
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
cascades = [
|
||||||
|
("user", "pgpkey", "pgp_key_id", "SET NULL"),
|
||||||
|
("user_webhook_subscription", "oauthtoken", "token_id", "CASCADE"),
|
||||||
|
("user_webhook_delivery", "user_webhook_subscription", "subscription_id", "CASCADE"),
|
||||||
|
]
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
for (table, relation, col, do) in cascades:
|
||||||
|
op.execute(f"""
|
||||||
|
ALTER TABLE "{table}" DROP CONSTRAINT IF EXISTS {table}_{col}_fkey;
|
||||||
|
ALTER TABLE "{table}" ADD CONSTRAINT {table}_{col}_fkey
|
||||||
|
FOREIGN KEY ({col})
|
||||||
|
REFERENCES "{relation}"(id) ON DELETE {do};
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
for (table, relation, col, do) in tables:
|
||||||
|
op.execute(f"""
|
||||||
|
ALTER TABLE "{table}" DROP CONSTRAINT IF EXISTS {table}_{col}_fkey;
|
||||||
|
ALTER TABLE "{table}" ADD CONSTRAINT {table}_{col}_fkey FOREIGN KEY ({col}) REFERENCES "{relation}"(id);
|
||||||
|
""")
|
|
@ -79,7 +79,7 @@ CREATE TABLE pgpkey (
|
||||||
|
|
||||||
ALTER TABLE "user"
|
ALTER TABLE "user"
|
||||||
ADD CONSTRAINT user_pgp_key_id_fkey
|
ADD CONSTRAINT user_pgp_key_id_fkey
|
||||||
FOREIGN KEY (pgp_key_id) REFERENCES pgpkey(id);
|
FOREIGN KEY (pgp_key_id) REFERENCES pgpkey(id) ON DELETE SET NULL;
|
||||||
|
|
||||||
CREATE TABLE sshkey (
|
CREATE TABLE sshkey (
|
||||||
id serial PRIMARY KEY,
|
id serial PRIMARY KEY,
|
||||||
|
@ -242,7 +242,7 @@ CREATE TABLE user_webhook_subscription (
|
||||||
url character varying(2048) NOT NULL,
|
url character varying(2048) NOT NULL,
|
||||||
events character varying NOT NULL,
|
events character varying NOT NULL,
|
||||||
user_id integer REFERENCES "user"(id) ON DELETE CASCADE,
|
user_id integer REFERENCES "user"(id) ON DELETE CASCADE,
|
||||||
token_id integer REFERENCES oauthtoken(id)
|
token_id integer REFERENCES oauthtoken(id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE user_webhook_delivery (
|
CREATE TABLE user_webhook_delivery (
|
||||||
|
@ -256,7 +256,7 @@ CREATE TABLE user_webhook_delivery (
|
||||||
response character varying(16384),
|
response character varying(16384),
|
||||||
response_status integer NOT NULL,
|
response_status integer NOT NULL,
|
||||||
response_headers character varying(16384),
|
response_headers character varying(16384),
|
||||||
subscription_id integer REFERENCES user_webhook_subscription(id)
|
subscription_id integer REFERENCES user_webhook_subscription(id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE webhook_subscription (
|
CREATE TABLE webhook_subscription (
|
||||||
|
|
Loading…
Reference in New Issue