Skip to content

Commit

Permalink
updates pg
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Nov 21, 2024
1 parent b20528b commit 737f7ae
Show file tree
Hide file tree
Showing 47 changed files with 256 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from ..constants import DECIMAL_PLACES


class ReferentialAction:
# SEE https://docs.sqlalchemy.org/en/20/orm/cascades.html
CASCADE = "CASCADE"
SET_NULL = "SET NULL"
SET_DEFAULT = "SET DEFAULT"
RESTRICT = "RESTRICT"
NO_ACTION = "NO ACTION"
class RefActions:
"""Referential actions for `ON UPDATE`, `ON DELETE`"""

# SEE https://docs.sqlalchemy.org/en/20/core/constraints.html#on-update-on-delete
CASCADE: Final[str] = "CASCADE"
SET_NULL: Final[str] = "SET NULL"
SET_DEFAULT: Final[str] = "SET DEFAULT"
RESTRICT: Final[str] = "RESTRICT"
NO_ACTION: Final[str] = "NO ACTION"


def column_created_datetime(*, timezone: bool = True) -> sa.Column:
Expand Down Expand Up @@ -43,8 +45,8 @@ def column_created_by_user(
sa.Integer,
sa.ForeignKey(
users_table.c.id,
onupdate=ReferentialAction.CASCADE,
ondelete=ReferentialAction.SET_NULL,
onupdate=RefActions.CASCADE,
ondelete=RefActions.SET_NULL,
),
nullable=not required,
doc="Who created this row at `created`",
Expand All @@ -59,8 +61,8 @@ def column_modified_by_user(
sa.Integer,
sa.ForeignKey(
users_table.c.id,
onupdate=ReferentialAction.CASCADE,
ondelete=ReferentialAction.SET_NULL,
onupdate=RefActions.CASCADE,
ondelete=RefActions.SET_NULL,
),
nullable=not required,
doc="Who modified this row at `modified`",
Expand All @@ -83,8 +85,8 @@ def column_trashed_by_user(resource_name: str, users_table: sa.Table) -> sa.Colu
sa.BigInteger,
sa.ForeignKey(
users_table.c.id,
onupdate=ReferentialAction.CASCADE,
ondelete=ReferentialAction.SET_NULL,
onupdate=RefActions.CASCADE,
ondelete=RefActions.SET_NULL,
name=f"fk_{resource_name}_trashed_by_user_id",
),
nullable=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sqlalchemy as sa
from sqlalchemy.sql import func

from ._common import RefActions
from .base import metadata
from .users import users

Expand All @@ -35,7 +36,7 @@
sa.Column(
"user_id",
sa.BigInteger(),
sa.ForeignKey(users.c.id, ondelete="CASCADE"),
sa.ForeignKey(users.c.id, ondelete=RefActions.CASCADE),
nullable=False,
doc="Identified user",
),
Expand All @@ -44,8 +45,8 @@
sa.String,
sa.ForeignKey(
"products.name",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
name="fk_api_keys_product_name",
),
nullable=False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func

from ._common import RefActions
from .base import metadata

group_classifiers = sa.Table(
Expand All @@ -32,8 +33,8 @@
sa.ForeignKey(
"groups.gid",
name="fk_group_classifiers_gid_to_groups_gid",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
),
unique=True, # Every Group can ONLY have one set of classifiers
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlalchemy as sa
from sqlalchemy.sql import expression, func

from ._common import RefActions
from .base import metadata
from .clusters import clusters
from .groups import groups
Expand All @@ -14,8 +15,8 @@
sa.ForeignKey(
clusters.c.id,
name="fk_cluster_to_groups_id_clusters",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
),
doc="Cluster unique ID",
),
Expand All @@ -25,8 +26,8 @@
sa.ForeignKey(
groups.c.gid,
name="fk_cluster_to_groups_gid_groups",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
),
doc="Group unique IDentifier",
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func

from ._common import RefActions
from .base import metadata


Expand Down Expand Up @@ -36,8 +37,8 @@ class ClusterType(Enum):
sa.ForeignKey(
"groups.gid",
name="fk_clusters_gid_groups",
onupdate="CASCADE",
ondelete="RESTRICT",
onupdate=RefActions.CASCADE,
ondelete=RefActions.RESTRICT,
),
nullable=False,
doc="Identifier of the group that owns this cluster",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func

from ._common import RefActions
from .base import metadata
from .comp_pipeline import StateType

Expand All @@ -26,8 +27,8 @@
sa.ForeignKey(
"projects.uuid",
name="fk_comp_runs_project_uuid_projects",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
),
nullable=False,
doc="The project uuid with which the run entry is associated",
Expand All @@ -38,8 +39,8 @@
sa.ForeignKey(
"users.id",
name="fk_comp_runs_user_id_users",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
),
nullable=False,
doc="The user id with which the run entry is associated",
Expand All @@ -50,8 +51,8 @@
sa.ForeignKey(
"clusters.id",
name="fk_comp_runs_cluster_id_clusters",
onupdate="CASCADE",
ondelete="SET NULL",
onupdate=RefActions.CASCADE,
ondelete=RefActions.SET_NULL,
),
nullable=True,
doc="The cluster id on which the run entry is associated, if NULL or 0 uses the default",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import sqlalchemy as sa

from ._common import RefActions
from .base import metadata
from .users import users

Expand Down Expand Up @@ -57,6 +58,9 @@ class ConfirmationAction(enum.Enum):
# constraints ----------------
sa.PrimaryKeyConstraint("code", name="confirmation_code"),
sa.ForeignKeyConstraint(
["user_id"], [users.c.id], name="user_confirmation_fkey", ondelete="CASCADE"
["user_id"],
[users.c.id],
name="user_confirmation_fkey",
ondelete=RefActions.CASCADE,
),
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sqlalchemy as sa
from sqlalchemy.sql import expression

from ._common import column_created_datetime, column_modified_datetime
from ._common import RefActions, column_created_datetime, column_modified_datetime
from .base import metadata
from .workspaces import workspaces

Expand Down Expand Up @@ -35,8 +35,8 @@
sa.String,
sa.ForeignKey(
"products.name",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
name="fk_new_folders_to_products_name",
),
nullable=False,
Expand All @@ -46,8 +46,8 @@
sa.BigInteger,
sa.ForeignKey(
"users.id",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
name="fk_folders_to_user_id",
),
nullable=True,
Expand All @@ -57,8 +57,8 @@
sa.BigInteger,
sa.ForeignKey(
workspaces.c.workspace_id,
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
name="fk_folders_to_workspace_id",
),
nullable=True,
Expand All @@ -69,7 +69,7 @@
sa.ForeignKey(
"groups.gid",
name="fk_new_folders_to_groups_gid",
ondelete="SET NULL",
ondelete=RefActions.SET_NULL,
),
nullable=True,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func

from ._common import RefActions
from .base import metadata


Expand Down Expand Up @@ -86,8 +87,8 @@ class GroupType(enum.Enum):
sa.ForeignKey(
"users.id",
name="fk_user_to_groups_id_users",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
),
doc="User unique IDentifier",
),
Expand All @@ -97,8 +98,8 @@ class GroupType(enum.Enum):
sa.ForeignKey(
"groups.gid",
name="fk_user_to_groups_gid_groups",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
),
doc="Group unique IDentifier",
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import sqlalchemy as sa

from ._common import column_created_datetime, column_modified_datetime
from ._common import RefActions, column_created_datetime, column_modified_datetime
from .base import metadata

#
# groups_extra_properties: Maps internet access permissions to groups
#
groups_extra_properties = sa.Table(
"groups_extra_properties",
# groups_extra_properties: Maps internet access permissions to groups
metadata,
sa.Column(
"group_id",
sa.BigInteger,
sa.ForeignKey(
"groups.gid",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
name="fk_groups_extra_properties_to_group_group_id",
),
nullable=False,
Expand All @@ -26,8 +24,8 @@
sa.VARCHAR,
sa.ForeignKey(
"products.name",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
name="fk_groups_extra_properties_to_products_name",
),
nullable=False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from ._common import (
NUMERIC_KWARGS,
RefActions,
column_created_datetime,
column_modified_datetime,
register_modified_datetime_auto_update_trigger,
Expand Down Expand Up @@ -49,8 +50,8 @@
sa.ForeignKey(
payments_methods.c.payment_method_id,
name="fk_payments_autorecharge_primary_payment_method_id",
onupdate="CASCADE",
ondelete="CASCADE",
onupdate=RefActions.CASCADE,
ondelete=RefActions.CASCADE,
),
nullable=False,
unique=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TypedDict,
)

from ._common import RefActions
from .base import metadata
from .groups import groups
from .jinja2_templates import jinja2_templates
Expand Down Expand Up @@ -199,8 +200,8 @@ class ProductLoginSettingsDict(TypedDict, total=False):
sa.ForeignKey(
jinja2_templates.c.name,
name="fk_jinja2_templates_name",
ondelete="SET NULL",
onupdate="CASCADE",
ondelete=RefActions.SET_NULL,
onupdate=RefActions.CASCADE,
),
nullable=True,
doc="Custom jinja2 template for registration email",
Expand Down Expand Up @@ -238,8 +239,8 @@ class ProductLoginSettingsDict(TypedDict, total=False):
sa.ForeignKey(
groups.c.gid,
name="fk_products_group_id",
ondelete="SET NULL",
onupdate="CASCADE",
ondelete=RefActions.SET_NULL,
onupdate=RefActions.CASCADE,
),
unique=True,
nullable=True,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sqlalchemy as sa

from ._common import NUMERIC_KWARGS
from ._common import NUMERIC_KWARGS, RefActions
from .base import metadata
from .products import products

Expand All @@ -20,8 +20,8 @@
sa.ForeignKey(
products.c.name,
name="fk_products_prices_product_name",
ondelete="RESTRICT",
onupdate="CASCADE",
ondelete=RefActions.RESTRICT,
onupdate=RefActions.CASCADE,
),
nullable=False,
doc="Product name",
Expand Down
Loading

0 comments on commit 737f7ae

Please sign in to comment.