Skip to content

Commit f8d17b1

Browse files
committed
refactor: move global data connectors to the _global namespace
Closes #830.
1 parent 4d27856 commit f8d17b1

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""Add global namespace
2+
3+
Revision ID: 47e51c42e391
4+
Revises: dcb9648c3c15
5+
Create Date: 2025-05-14 07:20:22.778969
6+
7+
"""
8+
9+
from alembic import op
10+
from dataclasses import dataclass
11+
import sqlalchemy as sa
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision = "47e51c42e391"
16+
down_revision = "dcb9648c3c15"
17+
branch_labels = None
18+
depends_on = None
19+
20+
21+
def upgrade() -> None:
22+
connection = op.get_bind()
23+
with connection.begin_nested() as tx:
24+
op.execute(sa.text("LOCK TABLE common.namespaces IN EXCLUSIVE MODE"))
25+
op.drop_constraint(
26+
"either_group_id_or_user_id_is_set",
27+
"namespaces",
28+
schema="common",
29+
type_="check",
30+
)
31+
op.create_check_constraint(
32+
"either_group_id_or_user_id_is_set",
33+
"namespaces",
34+
"(user_id IS NOT NULL) OR (group_id IS NOT NULL) OR (slug = '_global')",
35+
schema="common",
36+
)
37+
38+
insert_global_namespace_stmt = (
39+
sa.insert(
40+
sa.table(
41+
"namespaces",
42+
sa.column("id", type_=sa.VARCHAR),
43+
sa.column("slug", type_=sa.VARCHAR),
44+
schema="common",
45+
)
46+
)
47+
.values(id=sa.text("generate_ulid()"), slug="_global")
48+
.returning(sa.column("id", type_=sa.VARCHAR))
49+
)
50+
namespace_id = connection.execute(insert_global_namespace_stmt).scalar()
51+
if not namespace_id:
52+
raise RuntimeError("Failed to insert the _global namespace")
53+
54+
print(f"namespace_id={namespace_id}")
55+
56+
op.execute(sa.text("LOCK TABLE storage.data_connectors IN EXCLUSIVE MODE"))
57+
select_global_data_connectors_stmt = (
58+
sa.select(sa.column("id", type_=sa.VARCHAR), sa.column("global_slug", type_=sa.VARCHAR))
59+
.select_from(sa.table("data_connectors", schema="storage"))
60+
.where(sa.column("global_slug", type_=sa.VARCHAR).is_not(sa.null()))
61+
)
62+
data_connectors = connection.execute(select_global_data_connectors_stmt).scalars().all()
63+
print(f"data_connectors={data_connectors}")
64+
65+
for dc_id, dc_global_slug in data_connectors:
66+
insert_entity_slug_stmt = (
67+
sa.insert(
68+
sa.table(
69+
"entity_slugs",
70+
sa.column("id", type_=sa.VARCHAR),
71+
sa.column("slug", type_=sa.VARCHAR),
72+
sa.column("data_connector_id", type_=sa.VARCHAR),
73+
sa.column("namespace_id", type_=sa.VARCHAR),
74+
schema="common",
75+
)
76+
)
77+
.values(
78+
id=sa.text("generate_ulid()"),
79+
slug=dc_global_slug,
80+
data_connector_id=dc_id,
81+
namespace_id=namespace_id,
82+
)
83+
.returning(sa.column("id", type_=sa.VARCHAR))
84+
)
85+
slug_id = connection.execute(insert_entity_slug_stmt).scalar()
86+
if not slug_id:
87+
raise RuntimeError(f"Failed to insert the entity slug for data connector '{dc_id}'")
88+
89+
tx.commit()
90+
91+
92+
def downgrade() -> None:
93+
connection = op.get_bind()
94+
with connection.begin_nested() as tx:
95+
op.execute(sa.text("LOCK TABLE common.namespaces IN EXCLUSIVE MODE"))
96+
op.drop_constraint(
97+
"either_group_id_or_user_id_is_set",
98+
"namespaces",
99+
schema="common",
100+
type_="check",
101+
)
102+
op.create_check_constraint(
103+
"either_group_id_or_user_id_is_set",
104+
"namespaces",
105+
"(user_id IS NULL) <> (group_id IS NULL)",
106+
schema="common",
107+
)
108+
tx.commit()
109+
# CheckConstraint("(user_id IS NULL) <> (group_id IS NULL)", name="either_group_id_or_user_id_is_set"),

components/renku_data_services/namespace/orm.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ class NamespaceORM(BaseORM):
5555

5656
__tablename__ = "namespaces"
5757
__table_args__ = (
58-
CheckConstraint("(user_id IS NULL) <> (group_id IS NULL)", name="either_group_id_or_user_id_is_set"),
58+
# CheckConstraint("(user_id IS NULL) <> (group_id IS NULL)", name="either_group_id_or_user_id_is_set"),
59+
CheckConstraint(
60+
"(user_id IS NOT NULL) OR (group_id IS NOT NULL) OR (slug = '_global')",
61+
name="either_group_id_or_user_id_is_set",
62+
),
5963
)
6064

6165
id: Mapped[ULID] = mapped_column("id", ULIDType, primary_key=True, default_factory=lambda: str(ULID()), init=False)

0 commit comments

Comments
 (0)