fix: Resolve issue with cleaning acl table
This commit is contained in:
parent
1643b13c95
commit
9fc4199958
1 changed files with 43 additions and 29 deletions
|
|
@ -144,44 +144,58 @@ def _create_data_permission(conn, user_id, data_id, permission_name):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_column(inspector, table, name, schema=None):
|
||||||
|
for col in inspector.get_columns(table, schema=schema):
|
||||||
|
if col["name"] == name:
|
||||||
|
return col
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def upgrade() -> None:
|
def upgrade() -> None:
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
|
insp = sa.inspect(conn)
|
||||||
|
|
||||||
# Recreate ACLs table with default permissions set to datasets instead of documents
|
dataset_id_column = _get_column(insp, "acls", "dataset_id")
|
||||||
op.drop_table("acls")
|
if not dataset_id_column:
|
||||||
|
# Recreate ACLs table with default permissions set to datasets instead of documents
|
||||||
|
op.drop_table("acls")
|
||||||
|
|
||||||
acls_table = op.create_table(
|
acls_table = op.create_table(
|
||||||
"acls",
|
"acls",
|
||||||
sa.Column("id", UUID, primary_key=True, default=uuid4),
|
sa.Column("id", UUID, primary_key=True, default=uuid4),
|
||||||
sa.Column(
|
sa.Column(
|
||||||
"created_at", sa.DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
"created_at", sa.DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
||||||
),
|
),
|
||||||
sa.Column(
|
sa.Column(
|
||||||
"updated_at", sa.DateTime(timezone=True), onupdate=lambda: datetime.now(timezone.utc)
|
"updated_at",
|
||||||
),
|
sa.DateTime(timezone=True),
|
||||||
sa.Column("principal_id", UUID, sa.ForeignKey("principals.id")),
|
onupdate=lambda: datetime.now(timezone.utc),
|
||||||
sa.Column("permission_id", UUID, sa.ForeignKey("permissions.id")),
|
),
|
||||||
sa.Column("dataset_id", UUID, sa.ForeignKey("datasets.id", ondelete="CASCADE")),
|
sa.Column("principal_id", UUID, sa.ForeignKey("principals.id")),
|
||||||
)
|
sa.Column("permission_id", UUID, sa.ForeignKey("permissions.id")),
|
||||||
|
sa.Column("dataset_id", UUID, sa.ForeignKey("datasets.id", ondelete="CASCADE")),
|
||||||
|
)
|
||||||
|
|
||||||
# Note: We can't use any Cognee model info to gather data (as it can change) in database so we must use our own table
|
# Note: We can't use any Cognee model info to gather data (as it can change) in database so we must use our own table
|
||||||
# definition or load what is in the database
|
# definition or load what is in the database
|
||||||
dataset_table = _define_dataset_table()
|
dataset_table = _define_dataset_table()
|
||||||
datasets = conn.execute(sa.select(dataset_table)).fetchall()
|
datasets = conn.execute(sa.select(dataset_table)).fetchall()
|
||||||
|
|
||||||
if not datasets:
|
if not datasets:
|
||||||
return
|
return
|
||||||
|
|
||||||
acl_list = []
|
acl_list = []
|
||||||
|
|
||||||
for dataset in datasets:
|
for dataset in datasets:
|
||||||
acl_list.append(_create_dataset_permission(conn, dataset.owner_id, dataset.id, "read"))
|
acl_list.append(_create_dataset_permission(conn, dataset.owner_id, dataset.id, "read"))
|
||||||
acl_list.append(_create_dataset_permission(conn, dataset.owner_id, dataset.id, "write"))
|
acl_list.append(_create_dataset_permission(conn, dataset.owner_id, dataset.id, "write"))
|
||||||
acl_list.append(_create_dataset_permission(conn, dataset.owner_id, dataset.id, "share"))
|
acl_list.append(_create_dataset_permission(conn, dataset.owner_id, dataset.id, "share"))
|
||||||
acl_list.append(_create_dataset_permission(conn, dataset.owner_id, dataset.id, "delete"))
|
acl_list.append(
|
||||||
|
_create_dataset_permission(conn, dataset.owner_id, dataset.id, "delete")
|
||||||
|
)
|
||||||
|
|
||||||
if acl_list:
|
if acl_list:
|
||||||
op.bulk_insert(acls_table, acl_list)
|
op.bulk_insert(acls_table, acl_list)
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue