Schema: fix more missing cascades

This commit is contained in:
Drew DeVault 2022-11-03 13:26:23 +01:00
parent 3112dd7ad1
commit e3759a2e5c
2 changed files with 40 additions and 3 deletions

View File

@ -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);
""")

View File

@ -79,7 +79,7 @@ CREATE TABLE pgpkey (
ALTER TABLE "user"
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 (
id serial PRIMARY KEY,
@ -242,7 +242,7 @@ CREATE TABLE user_webhook_subscription (
url character varying(2048) NOT NULL,
events character varying NOT NULL,
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 (
@ -256,7 +256,7 @@ CREATE TABLE user_webhook_delivery (
response character varying(16384),
response_status integer NOT NULL,
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 (