Skip to content

Commit

Permalink
fix: Update unique_table_ownership constraint to allow owner per ty…
Browse files Browse the repository at this point in the history
…pe (#1403)
  • Loading branch information
baumandm committed Feb 1, 2024
1 parent 7a188c4 commit 8531df1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
@@ -0,0 +1,87 @@
"""allow unique_table_ownership per type
Revision ID: 299e24dcfd29
Revises: c00f08f16065
Create Date: 2024-01-31 14:39:13.601013
"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = "299e24dcfd29"
down_revision = "c00f08f16065"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
ownership_table = sa.Table(
"data_table_ownership", sa.MetaData(op.get_bind()), autoload=True
)

for constraint in ownership_table.foreign_key_constraints:
op.drop_constraint(constraint.name, "data_table_ownership", type_="foreignkey")

op.drop_constraint("unique_table_ownership", "data_table_ownership", type_="unique")

op.create_foreign_key(
"data_table_ownership_data_table_id_fk",
"data_table_ownership",
"data_table",
["data_table_id"],
["id"],
ondelete="CASCADE",
)
op.create_foreign_key(
"data_table_ownership_uid_fk",
"data_table_ownership",
"user",
["uid"],
["id"],
ondelete="CASCADE",
)
op.create_unique_constraint(
"unique_table_ownership",
"data_table_ownership",
["data_table_id", "uid", "type"],
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
ownership_table = sa.Table(
"data_table_ownership", sa.MetaData(op.get_bind()), autoload=True
)

for constraint in ownership_table.foreign_key_constraints:
op.drop_constraint(constraint.name, "data_table_ownership", type_="foreignkey")

op.drop_constraint("unique_table_ownership", "data_table_ownership", type_="unique")

op.create_foreign_key(
"data_table_ownership_data_table_id_fk",
"data_table_ownership",
"data_table",
["data_table_id"],
["id"],
ondelete="CASCADE",
)
op.create_foreign_key(
"data_table_ownership_uid_fk",
"data_table_ownership",
"user",
["uid"],
["id"],
ondelete="CASCADE",
)
op.create_unique_constraint(
"unique_table_ownership",
"data_table_ownership",
["data_table_id", "uid"],
)
# ### end Alembic commands ###
4 changes: 3 additions & 1 deletion querybook/server/models/metastore.py
Expand Up @@ -324,7 +324,9 @@ def to_dict(self, include_table=False):
class DataTableOwnership(Base):
__tablename__ = "data_table_ownership"
__table_args__ = (
sql.UniqueConstraint("data_table_id", "uid", name="unique_table_ownership"),
sql.UniqueConstraint(
"data_table_id", "uid", "type", name="unique_table_ownership"
),
)

id = sql.Column(sql.Integer, primary_key=True)
Expand Down

0 comments on commit 8531df1

Please sign in to comment.