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

✨Personalized resources: remove projects to projects nodes table 🗃️ #4384

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"""remove projects_to_projects_nodes

Revision ID: 71ea254837b0
Revises: c8f072c72adc
Create Date: 2023-06-19 13:52:39.161616+00:00

"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "71ea254837b0"
down_revision = "c8f072c72adc"
branch_labels = None
depends_on = None


# TRIGGERS -----------------
drop_projects_to_projects_nodes_deleted_trigger = sa.DDL(
"DROP TRIGGER IF EXISTS entry_deleted on projects;"
)
projects_to_projects_nodes_deleted_trigger = sa.DDL(
"""
DROP TRIGGER IF EXISTS entry_deleted on projects;
CREATE TRIGGER entry_deleted
AFTER DELETE ON projects
FOR EACH ROW
EXECUTE FUNCTION delete_orphaned_project_nodes();
"""
)

# PROCEDURES -------------------
drop_delete_orphaned_project_nodes_procedure = sa.DDL(
"DROP FUNCTION delete_orphaned_project_nodes();"
)
delete_orphaned_project_nodes_procedure = sa.DDL(
"""
CREATE OR REPLACE FUNCTION delete_orphaned_project_nodes()
RETURNS TRIGGER AS $$
BEGIN
DELETE FROM projects_nodes
WHERE NOT EXISTS (
SELECT 1 FROM projects_to_projects_nodes
WHERE projects_to_projects_nodes.node_id = projects_nodes.node_id
);
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
"""
)


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("projects_to_projects_nodes")
op.add_column(
"projects_nodes", sa.Column("project_uuid", sa.String(), nullable=False)
)
# custom
op.drop_constraint("projects_nodes_pkey", "projects_nodes", type_="primary")
op.create_primary_key(
"projects_nodes_pkey",
"projects_nodes",
["project_uuid", "node_id"],
)
op.create_foreign_key(
"fk_projects_to_projects_nodes_to_projects_uuid",
"projects_nodes",
"projects",
["project_uuid"],
["uuid"],
onupdate="CASCADE",
ondelete="CASCADE",
)
# ### end Alembic commands ###

# custom
op.execute(drop_projects_to_projects_nodes_deleted_trigger)
op.execute(drop_delete_orphaned_project_nodes_procedure)


def downgrade():
# custom
op.drop_constraint("projects_nodes_pkey", "projects_nodes", type_="primary")
op.create_primary_key("projects_nodes_pkey", "projects_nodes", ["node_id"])
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(
"fk_projects_to_projects_nodes_to_projects_uuid",
"projects_nodes",
type_="foreignkey",
)
op.drop_column("projects_nodes", "project_uuid")
op.create_table(
"projects_to_projects_nodes",
sa.Column("project_uuid", sa.VARCHAR(), autoincrement=False, nullable=True),
sa.Column("node_id", sa.VARCHAR(), autoincrement=False, nullable=True),
sa.Column(
"created",
postgresql.TIMESTAMP(timezone=True),
server_default=sa.text("now()"),
autoincrement=False,
nullable=False,
),
sa.Column(
"modified",
postgresql.TIMESTAMP(timezone=True),
server_default=sa.text("now()"),
autoincrement=False,
nullable=False,
),
sa.ForeignKeyConstraint(
["node_id"],
["projects_nodes.node_id"],
name="fk_projects_to_projects_nodes_to_projects_nodes_node_id",
onupdate="CASCADE",
ondelete="CASCADE",
),
sa.ForeignKeyConstraint(
["project_uuid"],
["projects.uuid"],
name="fk_projects_to_projects_nodes_to_projects_uuid",
onupdate="CASCADE",
ondelete="CASCADE",
),
sa.UniqueConstraint(
"project_uuid",
"node_id",
name="projects_to_projects_nodes_project_uuid_node_id_key",
),
)
# ### end Alembic commands ###

# custom
op.execute(delete_orphaned_project_nodes_procedure)
op.execute(projects_to_projects_nodes_deleted_trigger)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import sqlalchemy as sa
from sqlalchemy import event
from sqlalchemy.dialects.postgresql import JSONB

from ._common import (
Expand All @@ -19,11 +18,24 @@
projects_nodes = sa.Table(
"projects_nodes",
metadata,
sa.Column(
"project_uuid",
sa.String,
sa.ForeignKey(
projects.c.uuid,
onupdate="CASCADE",
ondelete="CASCADE",
name="fk_projects_to_projects_nodes_to_projects_uuid",
),
nullable=False,
primary_key=False,
doc="The project unique identifier",
),
sa.Column(
"node_id",
sa.String,
nullable=False,
primary_key=True,
primary_key=False,
doc="The node unique identifier",
),
sa.Column(
Expand All @@ -36,42 +48,8 @@
# TIME STAMPS ----
column_created_datetime(timezone=True),
column_modified_datetime(timezone=True),
sa.PrimaryKeyConstraint("project_uuid", "node_id"),
)


register_modified_datetime_auto_update_trigger(projects_nodes)


# TRIGGERS -----------------
projects_to_projects_nodes_deleted_trigger = sa.DDL(
"""
DROP TRIGGER IF EXISTS entry_deleted on projects;
CREATE TRIGGER entry_deleted
AFTER DELETE ON projects
FOR EACH ROW
EXECUTE FUNCTION delete_orphaned_project_nodes();
"""
)

# PROCEDURES -------------------
delete_orphaned_project_nodes_procedure = sa.DDL(
"""
CREATE OR REPLACE FUNCTION delete_orphaned_project_nodes()
RETURNS TRIGGER AS $$
BEGIN
DELETE FROM projects_nodes
WHERE NOT EXISTS (
SELECT 1 FROM projects_to_projects_nodes
WHERE projects_to_projects_nodes.node_id = projects_nodes.node_id
);
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
"""
)

# REGISTER THEM PROCEDURES/TRIGGERS


event.listen(projects_nodes, "after_create", delete_orphaned_project_nodes_procedure)
event.listen(projects, "after_create", projects_to_projects_nodes_deleted_trigger)

This file was deleted.

Loading