-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c26e97
commit 484268d
Showing
12 changed files
with
216 additions
and
83 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
"""add-catalog | ||
Revision ID: 4e40358ad25c | ||
Revises: d9ea6ea6708f | ||
Create Date: 2022-07-19 15:03:34.512545 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "4e40358ad25c" | ||
down_revision = "d9ea6ea6708f" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"catalog", | ||
sa.Column("organization_siret", sa.CHAR(length=14), nullable=False), | ||
sa.Column( | ||
"created_at", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.text("clock_timestamp()"), | ||
nullable=False, | ||
), | ||
sa.ForeignKeyConstraint( | ||
["organization_siret"], ["organization.siret"], ondelete="CASCADE" | ||
), | ||
sa.PrimaryKeyConstraint("organization_siret"), | ||
) | ||
|
||
# Create initial organization. | ||
op.execute( | ||
"INSERT INTO organization (siret, name) " | ||
"VALUES ('00000000000000', 'Organisation par défaut');" | ||
) | ||
|
||
# Create its catalog. | ||
op.execute("INSERT INTO catalog (organization_siret) VALUES ('00000000000000');") | ||
|
||
# Add all users to the initial organization. | ||
op.add_column("user", sa.Column("organization_siret", sa.CHAR(14))) | ||
op.execute("UPDATE \"user\" SET organization_siret = '00000000000000';") | ||
op.alter_column("user", "organization_siret", nullable=False) | ||
op.create_foreign_key( | ||
None, | ||
"user", | ||
"organization", | ||
["organization_siret"], | ||
["siret"], | ||
ondelete="CASCADE", | ||
) | ||
|
||
# Add all catalog records to the initial catalog. | ||
op.add_column("catalog_record", sa.Column("organization_siret", sa.CHAR(14))) | ||
op.execute("UPDATE catalog_record SET organization_siret = '00000000000000';") | ||
op.alter_column("catalog_record", "organization_siret", nullable=False) | ||
op.create_foreign_key( | ||
None, | ||
"catalog_record", | ||
"catalog", | ||
["organization_siret"], | ||
["organization_siret"], | ||
ondelete="CASCADE", | ||
) | ||
|
||
# Swap the 1-1 relationship between CatalogRecord and Dataset, as the CASCADE | ||
# must be on the Dataset side: when a catalog record is removed, the dataset must | ||
# be dropped too. | ||
op.drop_constraint( | ||
"catalog_record_dataset_id_fkey", "catalog_record", type_="foreignkey" | ||
) | ||
op.add_column( | ||
"dataset", | ||
sa.Column( | ||
"catalog_record_id", | ||
postgresql.UUID(as_uuid=True), | ||
), | ||
) | ||
op.execute( | ||
"UPDATE dataset SET catalog_record_id = catalog_record.id " | ||
"FROM dataset AS d JOIN catalog_record ON d.id = catalog_record.dataset_id;" | ||
) | ||
op.alter_column("dataset", "catalog_record_id", nullable=False) | ||
op.drop_column("catalog_record", "dataset_id") | ||
op.create_foreign_key( | ||
None, | ||
"dataset", | ||
"catalog_record", | ||
["catalog_record_id"], | ||
["id"], | ||
ondelete="CASCADE", | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_constraint(None, "user", type_="foreignkey") | ||
op.drop_column("user", "organization_siret") | ||
op.drop_constraint(None, "catalog_record", type_="foreignkey") | ||
op.drop_column("catalog_record", "organization_siret") | ||
op.drop_table("catalog") |
This file was deleted.
Oops, something went wrong.
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.