Skip to content

Commit 68e7649

Browse files
committed
chore(api): Use uuidv7 as PK for provider_model_credentials and provider_credentials
1 parent 98473e9 commit 68e7649

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

api/migrations/versions/2025_08_09_1553-e8446f481c1e_add_provider_credential_pool_support.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
77
"""
88
from alembic import op
9+
from libs.uuid_utils import uuidv7
910
import models as models
1011
import sqlalchemy as sa
1112
from sqlalchemy.sql import table, column
12-
import uuid
1313

1414
# revision identifiers, used by Alembic.
1515
revision = 'e8446f481c1e'
@@ -21,7 +21,7 @@
2121
def upgrade():
2222
# Create provider_credentials table
2323
op.create_table('provider_credentials',
24-
sa.Column('id', models.types.StringUUID(), server_default=sa.text('uuid_generate_v4()'), nullable=False),
24+
sa.Column('id', models.types.StringUUID(), server_default=sa.text('uuidv7()'), nullable=False),
2525
sa.Column('tenant_id', models.types.StringUUID(), nullable=False),
2626
sa.Column('provider_name', sa.String(length=255), nullable=False),
2727
sa.Column('credential_name', sa.String(length=255), nullable=False),
@@ -63,7 +63,7 @@ def migrate_existing_providers_data():
6363
column('updated_at', sa.DateTime()),
6464
column('credential_id', models.types.StringUUID()),
6565
)
66-
66+
6767
provider_credential_table = table('provider_credentials',
6868
column('id', models.types.StringUUID()),
6969
column('tenant_id', models.types.StringUUID()),
@@ -79,15 +79,15 @@ def migrate_existing_providers_data():
7979

8080
# Query all existing providers data
8181
existing_providers = conn.execute(
82-
sa.select(providers_table.c.id, providers_table.c.tenant_id,
82+
sa.select(providers_table.c.id, providers_table.c.tenant_id,
8383
providers_table.c.provider_name, providers_table.c.encrypted_config,
8484
providers_table.c.created_at, providers_table.c.updated_at)
8585
.where(providers_table.c.encrypted_config.isnot(None))
8686
).fetchall()
87-
87+
8888
# Iterate through each provider and insert into provider_credentials
8989
for provider in existing_providers:
90-
credential_id = str(uuid.uuid4())
90+
credential_id = str(uuidv7())
9191
if not provider.encrypted_config or provider.encrypted_config.strip() == '':
9292
continue
9393

@@ -134,7 +134,7 @@ def downgrade():
134134

135135
def migrate_data_back_to_providers():
136136
"""Migrate data back from provider_credentials to providers table for downgrade"""
137-
137+
138138
# Define table structure for data manipulation
139139
providers_table = table('providers',
140140
column('id', models.types.StringUUID()),
@@ -143,7 +143,7 @@ def migrate_data_back_to_providers():
143143
column('encrypted_config', sa.Text()),
144144
column('credential_id', models.types.StringUUID()),
145145
)
146-
146+
147147
provider_credential_table = table('provider_credentials',
148148
column('id', models.types.StringUUID()),
149149
column('tenant_id', models.types.StringUUID()),
@@ -160,18 +160,18 @@ def migrate_data_back_to_providers():
160160
sa.select(providers_table.c.id, providers_table.c.credential_id)
161161
.where(providers_table.c.credential_id.isnot(None))
162162
).fetchall()
163-
163+
164164
# For each provider, get the credential data and update providers table
165165
for provider in providers_with_credentials:
166166
credential = conn.execute(
167167
sa.select(provider_credential_table.c.encrypted_config)
168168
.where(provider_credential_table.c.id == provider.credential_id)
169169
).fetchone()
170-
170+
171171
if credential:
172172
# Update providers table with encrypted_config from credential
173173
conn.execute(
174174
providers_table.update()
175175
.where(providers_table.c.id == provider.id)
176176
.values(encrypted_config=credential.encrypted_config)
177-
)
177+
)

api/migrations/versions/2025_08_13_1605-0e154742a5fa_add_provider_model_multi_credential.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
Create Date: 2025-08-13 16:05:42.657730
66
77
"""
8-
import uuid
98

109
from alembic import op
10+
from libs.uuid_utils import uuidv7
1111
import models as models
1212
import sqlalchemy as sa
1313
from sqlalchemy.sql import table, column
@@ -23,7 +23,7 @@
2323
def upgrade():
2424
# Create provider_model_credentials table
2525
op.create_table('provider_model_credentials',
26-
sa.Column('id', models.types.StringUUID(), server_default=sa.text('uuid_generate_v4()'), nullable=False),
26+
sa.Column('id', models.types.StringUUID(), server_default=sa.text('uuidv7()'), nullable=False),
2727
sa.Column('tenant_id', models.types.StringUUID(), nullable=False),
2828
sa.Column('provider_name', sa.String(length=255), nullable=False),
2929
sa.Column('model_name', sa.String(length=255), nullable=False),
@@ -71,7 +71,7 @@ def migrate_existing_provider_models_data():
7171
column('updated_at', sa.DateTime()),
7272
column('credential_id', models.types.StringUUID()),
7373
)
74-
74+
7575
provider_model_credentials_table = table('provider_model_credentials',
7676
column('id', models.types.StringUUID()),
7777
column('tenant_id', models.types.StringUUID()),
@@ -90,19 +90,19 @@ def migrate_existing_provider_models_data():
9090

9191
# Query all existing provider_models data with encrypted_config
9292
existing_provider_models = conn.execute(
93-
sa.select(provider_models_table.c.id, provider_models_table.c.tenant_id,
93+
sa.select(provider_models_table.c.id, provider_models_table.c.tenant_id,
9494
provider_models_table.c.provider_name, provider_models_table.c.model_name,
9595
provider_models_table.c.model_type, provider_models_table.c.encrypted_config,
9696
provider_models_table.c.created_at, provider_models_table.c.updated_at)
9797
.where(provider_models_table.c.encrypted_config.isnot(None))
9898
).fetchall()
99-
99+
100100
# Iterate through each provider_model and insert into provider_model_credentials
101101
for provider_model in existing_provider_models:
102102
if not provider_model.encrypted_config or provider_model.encrypted_config.strip() == '':
103103
continue
104104

105-
credential_id = str(uuid.uuid4())
105+
credential_id = str(uuidv7())
106106

107107
# Insert into provider_model_credentials table
108108
conn.execute(
@@ -148,14 +148,14 @@ def downgrade():
148148

149149
def migrate_data_back_to_provider_models():
150150
"""Migrate data back from provider_model_credentials to provider_models table for downgrade"""
151-
151+
152152
# Define table structure for data manipulation
153153
provider_models_table = table('provider_models',
154154
column('id', models.types.StringUUID()),
155155
column('encrypted_config', sa.Text()),
156156
column('credential_id', models.types.StringUUID()),
157157
)
158-
158+
159159
provider_model_credentials_table = table('provider_model_credentials',
160160
column('id', models.types.StringUUID()),
161161
column('encrypted_config', sa.Text()),
@@ -169,14 +169,14 @@ def migrate_data_back_to_provider_models():
169169
sa.select(provider_models_table.c.id, provider_models_table.c.credential_id)
170170
.where(provider_models_table.c.credential_id.isnot(None))
171171
).fetchall()
172-
172+
173173
# For each provider_model, get the credential data and update provider_models table
174174
for provider_model in provider_models_with_credentials:
175175
credential = conn.execute(
176176
sa.select(provider_model_credentials_table.c.encrypted_config)
177177
.where(provider_model_credentials_table.c.id == provider_model.credential_id)
178178
).fetchone()
179-
179+
180180
if credential:
181181
# Update provider_models table with encrypted_config from credential
182182
conn.execute(

api/models/provider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class ProviderCredential(Base):
274274
sa.Index("provider_credential_tenant_provider_idx", "tenant_id", "provider_name"),
275275
)
276276

277-
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuid_generate_v4()"))
277+
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuidv7()"))
278278
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
279279
provider_name: Mapped[str] = mapped_column(String(255), nullable=False)
280280
credential_name: Mapped[str] = mapped_column(String(255), nullable=False)
@@ -300,7 +300,7 @@ class ProviderModelCredential(Base):
300300
),
301301
)
302302

303-
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuid_generate_v4()"))
303+
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuidv7()"))
304304
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
305305
provider_name: Mapped[str] = mapped_column(String(255), nullable=False)
306306
model_name: Mapped[str] = mapped_column(String(255), nullable=False)

0 commit comments

Comments
 (0)