-
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server/customer: add a Customer.user_id foreign key instead of having…
… UserCustomer association table Must have been really tired when I did this initially...
- Loading branch information
1 parent
4bf0212
commit 317a4aa
Showing
15 changed files
with
178 additions
and
157 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
server/migrations/versions/2025-01-03-1443_add_customer_user_id_and_remove_.py
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,112 @@ | ||
"""Add Customer.user_id and remove UserCustomer | ||
Revision ID: c996df1d397f | ||
Revises: 81faf775fce0 | ||
Create Date: 2025-01-03 14:43:30.632086 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# Polar Custom Imports | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "c996df1d397f" | ||
down_revision = "81faf775fce0" | ||
branch_labels: tuple[str] | None = None | ||
depends_on: tuple[str] | None = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column("customers", sa.Column("user_id", sa.Uuid(), nullable=True)) | ||
op.create_foreign_key( | ||
op.f("customers_user_id_fkey"), | ||
"customers", | ||
"users", | ||
["user_id"], | ||
["id"], | ||
ondelete="set null", | ||
) | ||
|
||
op.execute( | ||
""" | ||
UPDATE customers | ||
SET user_id = user_customers.user_id | ||
FROM user_customers | ||
WHERE customers.id = user_customers.customer_id | ||
""" | ||
) | ||
|
||
op.drop_index("ix_user_customers_created_at", table_name="user_customers") | ||
op.drop_index("ix_user_customers_deleted_at", table_name="user_customers") | ||
op.drop_index("ix_user_customers_modified_at", table_name="user_customers") | ||
op.drop_table("user_customers") | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"user_customers", | ||
sa.Column("user_id", sa.UUID(), autoincrement=False, nullable=False), | ||
sa.Column("customer_id", sa.UUID(), autoincrement=False, nullable=False), | ||
sa.Column("id", sa.UUID(), autoincrement=False, nullable=False), | ||
sa.Column( | ||
"created_at", | ||
postgresql.TIMESTAMP(timezone=True), | ||
autoincrement=False, | ||
nullable=False, | ||
), | ||
sa.Column( | ||
"modified_at", | ||
postgresql.TIMESTAMP(timezone=True), | ||
autoincrement=False, | ||
nullable=True, | ||
), | ||
sa.Column( | ||
"deleted_at", | ||
postgresql.TIMESTAMP(timezone=True), | ||
autoincrement=False, | ||
nullable=True, | ||
), | ||
sa.ForeignKeyConstraint( | ||
["customer_id"], | ||
["customers.id"], | ||
name="user_customers_customer_id_fkey", | ||
ondelete="CASCADE", | ||
), | ||
sa.ForeignKeyConstraint( | ||
["user_id"], | ||
["users.id"], | ||
name="user_customers_user_id_fkey", | ||
ondelete="CASCADE", | ||
), | ||
sa.PrimaryKeyConstraint("id", name="user_customers_pkey"), | ||
sa.UniqueConstraint("customer_id", name="user_customers_customer_id_key"), | ||
) | ||
op.create_index( | ||
"ix_user_customers_modified_at", "user_customers", ["modified_at"], unique=False | ||
) | ||
op.create_index( | ||
"ix_user_customers_deleted_at", "user_customers", ["deleted_at"], unique=False | ||
) | ||
op.create_index( | ||
"ix_user_customers_created_at", "user_customers", ["created_at"], unique=False | ||
) | ||
|
||
op.execute( | ||
""" | ||
INSERT INTO user_customers (user_id, customer_id, created_at) | ||
SELECT user_id, id, created_at | ||
FROM customers | ||
""" | ||
) | ||
|
||
op.drop_constraint(op.f("customers_user_id_fkey"), "customers", type_="foreignkey") | ||
op.drop_column("customers", "user_id") | ||
|
||
# ### end Alembic commands ### |
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
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
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
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
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
Oops, something went wrong.