"""shares: one grant per note and person, and only the permissions the app knows Revision ID: 0034 Revises: 0033 Create Date: 2026-10-07 Nothing wrote a share until #5174, so the table never needed these. Now the Share dialog does: - A unique index on (resource_type, resource_id, shared_with_user_id) where a user is the target, so sharing a note with someone twice updates the one grant rather than stacking two (the same for a group, ahead of step 15). - A check that `permission` is `view` or `edit`. - An index on `shared_with_user_id` for "Shared with me". ## Downgrade Drops the indexes and the check. The rows stay. """ from alembic import op revision = "0034" down_revision = "0033" branch_labels = None depends_on = None def upgrade() -> None: op.create_index( "uq_shares_resource_user", "shares", ["resource_type", "resource_id", "shared_with_user_id"], unique=True, postgresql_where="shared_with_user_id IS NOT NULL", ) op.create_index( "uq_shares_resource_group", "shares", ["resource_type", "resource_id", "shared_with_group_id"], unique=True, postgresql_where="shared_with_group_id IS NOT NULL", ) op.create_index("ix_shares_user", "shares", ["shared_with_user_id"]) op.create_check_constraint("ck_shares_permission", "shares", "permission IN ('view', 'edit')") def downgrade() -> None: op.drop_constraint("ck_shares_permission", "shares", type_="check") op.drop_index("ix_shares_user", table_name="shares") op.drop_index("uq_shares_resource_group", table_name="shares") op.drop_index("uq_shares_resource_user", table_name="shares")