Skip to content

feat(api): introduce model-type migration script - #36520

Merged
wylswz merged 14 commits into
langgenius:mainfrom
QuantumGhost:feat/model-type-migration-script
May 27, 2026
Merged

wylswz merged 14 commits into
langgenius:mainfrom
QuantumGhost:feat/model-type-migration-script

Conversation

@QuantumGhost

@QuantumGhost QuantumGhost commented May 22, 2026 •

Copy link
Copy Markdown
Member

AI disclosure: This PR was primarily drafted with Codex using GPT-5.4. I have reviewed and edited the final diff, and I am responsible for the content.

This PR adopts a relatively verbose style intentionally, so that the script can be reviewed carefully and any issues can be spotted easily.

Please review this PR with extra care: it modifies production credential records. Any script errors could cause data loss or a major outage.

Summary

This migration normalizes legacy provider-related model_type values to the canonical enum values:

  • text-generation -> llm
  • embeddings -> text-embedding
  • reranking -> rerank

The main goal is not just to update enum values in place, but to do so safely when a legacy row and a canonical row would collapse onto the same logical key after normalization.

How conflicts are handled

For each table, the migration groups rows by the normalized business key (that is, the key after mapping legacy model_type values to their canonical value). If multiple rows end up in the same logical group:

  • the migration keeps a single winner row
  • deletes the loser rows
  • updates the winner’s model_type to the canonical value if needed

The winner is chosen deterministically as the most recently updated row (updated_at), with id as the tiebreaker.

This is how the migration avoids creating unique-key or logical-key conflicts during canonicalization.

Table-specific rules

provider_models

Rows are grouped by the logical key behind unique_provider_model_name:

  • (tenant_id, provider_name, model_name, model_type)

If legacy and canonical rows collide after normalization, they are merged into one winner row.

tenant_default_models

Rows are grouped by the logical key behind unique_tenant_default_model_type:

  • (tenant_id, model_type)

Again, legacy/canonical duplicates are merged before final canonicalization.

provider_model_settings

This table does not enforce the key at the DB level, but the migration treats the following as the logical uniqueness boundary:

  • (tenant_id, provider_name, model_name, model_type)

So duplicates are merged the same way as the indexed tables.

provider_model_credentials

This table also uses a logical uniqueness rule rather than relying on a DB unique index:

  • (tenant_id, provider_name, model_name, credential_name, model_type)

When duplicates are merged, the migration also rewrites references from loser credentials to the winner credential in:

  • provider_models
  • load_balancing_model_configs

Only after references are rewritten are loser credential rows removed.

load_balancing_model_configs

This table is handled differently.

For normal rows, canonicalization is mostly row-by-row.

For name="__inherit__" rows, the migration first performs a pre-deduplication step by normalized business key:

  • (tenant_id, provider_name, model_name, model_type)

This is necessary because a legacy __inherit__ row and a canonical __inherit__ row could otherwise collide after the model_type update. The migration resolves that duplicate first, then canonicalizes remaining rows.

Operational behavior

  • The command is dry-run by default
  • --apply is required to persist changes
  • It supports scoping by table, tenant, and canonical model type
  • It runs in small transactions and skips lock-conflicted groups/rows instead of blocking the whole migration
  • Use the --output flag to capture a complete log of migration changes and execution context for debugging or recovery.
  • This migration is idempotent. It is safe to run multiple times, and it is also safe to rerun after a partial or interrupted execution.

Because this migration updates and deletes a large volume of data, we strongly recommend that you:

  1. test and validate the migration in a staging environment before applying it to production;
  2. preserve the output logs for a period of time after execution so that any migration-related issues can be diagnosed and resolved.

Continuation of #36427.

Closes #36426 #36129 #36386

Screenshots

Before After
... ...

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and cd web && pnpm exec vp staged (frontend) to appease the lint gods

@QuantumGhost QuantumGhost self-assigned this May 22, 2026
@github-actions

github-actions Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-05-26 16:04:32.206371986 +0000
+++ /tmp/pyrefly_pr.txt	2026-05-26 16:04:22.491274000 +0000
@@ -592,6 +592,14 @@
    --> tasks/regenerate_summary_index_task.py:277:60
 ERROR Class member `AsyncWorkflowCFSPlanScheduler.plan` overrides parent class `CFSPlanScheduler` in an inconsistent manner [bad-override-mutable-attribute]
   --> tasks/workflow_cfs_scheduler/cfs_scheduler.py:19:5
+ERROR Object of class `FromClause` has no attribute `metadata` [missing-attribute]
+  --> tests/helpers/legacy_model_type_migration.py:64:16
+ERROR Object of class `FromClause` has no attribute `metadata` [missing-attribute]
+  --> tests/helpers/legacy_model_type_migration.py:80:16
+ERROR Argument `dict[_KeyType, Any]` is not assignable to parameter `object` with type `dict[str, object]` in function `list.append` [bad-argument-type]
+   --> tests/helpers/legacy_model_type_migration.py:138:23
+ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]
+   --> tests/helpers/legacy_model_type_migration.py:182:13
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `SQLCoreOperations[TenantStatus] | TenantStatus` in function `models.account.Tenant.__init__` [bad-argument-type]
   --> tests/integration_tests/controllers/openapi/conftest.py:35:43
 ERROR Argument `Literal['owner']` is not assignable to parameter `role` with type `SQLCoreOperations[TenantAccountRole] | TenantAccountRole` in function `models.account.TenantAccountJoin.__init__` [bad-argument-type]
@@ -1976,6 +1984,30 @@
   --> tests/unit_tests/commands/test_clean_expired_messages.py:94:9
 ERROR Expected a callable, got `None` [not-callable]
    --> tests/unit_tests/commands/test_clean_expired_messages.py:176:9
+ERROR Generator function should return `Generator` [bad-return]
+  --> tests/unit_tests/commands/test_legacy_model_type_migration.py:36:38
+ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]
+   --> tests/unit_tests/commands/test_legacy_model_type_migration.py:204:13
+ERROR Object of class `object` has no attribute `callback` [missing-attribute]
+   --> tests/unit_tests/commands/test_legacy_model_type_migration.py:383:5
+ERROR Object of class `object` has no attribute `callback` [missing-attribute]
+   --> tests/unit_tests/commands/test_legacy_model_type_migration.py:444:5
+ERROR Object of class `TextIOBase` has no attribute `name` [missing-attribute]
+   --> tests/unit_tests/commands/test_legacy_model_type_migration.py:457:17
+ERROR Class `object` has no class attribute `__table__` [missing-attribute]
+   --> tests/unit_tests/commands/test_legacy_model_type_migration.py:541:48
+ERROR Class `object` has no class attribute `__table__` [missing-attribute]
+   --> tests/unit_tests/commands/test_legacy_model_type_migration.py:620:48
+ERROR Class `object` has no class attribute `__table__` [missing-attribute]
+   --> tests/unit_tests/commands/test_legacy_model_type_migration.py:723:48
+ERROR Class `object` has no class attribute `__table__` [missing-attribute]
+   --> tests/unit_tests/commands/test_legacy_model_type_migration.py:801:48
+ERROR Class `object` has no class attribute `__table__` [missing-attribute]
+   --> tests/unit_tests/commands/test_legacy_model_type_migration.py:856:42
+ERROR Argument `object` is not assignable to parameter `orig` with type `BaseException` in function `sqlalchemy.exc.DBAPIError.__init__` [bad-argument-type]
+    --> tests/unit_tests/commands/test_legacy_model_type_migration.py:1226:44
+ERROR Argument `SimpleNamespace` is not assignable to parameter `orig` with type `BaseException` in function `sqlalchemy.exc.DBAPIError.__init__` [bad-argument-type]
+    --> tests/unit_tests/commands/test_legacy_model_type_migration.py:1250:57
 ERROR Expected a callable, got `None` [not-callable]
   --> tests/unit_tests/commands/test_reset_encrypt_key_pair.py:18:9
 ERROR Object of class `ModuleType` has no attribute `upgrade` [missing-attribute]

@github-actions

github-actions Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 45.69% 45.80% +0.11%
Strict coverage 45.21% 45.32% +0.11%
Typed symbols 24,247 24,388 +141
Untyped symbols 29,125 29,168 +43
Modules 2724 2731 +7

@QuantumGhost
QuantumGhost force-pushed the feat/model-type-migration-script branch from 8d0ad9d to 1a25acc Compare May 26, 2026 01:25
@QuantumGhost
QuantumGhost marked this pull request as ready for review May 26, 2026 01:26
@QuantumGhost
QuantumGhost requested a review from a team May 26, 2026 01:26
@QuantumGhost
QuantumGhost requested a review from laipz8200 as a code owner May 26, 2026 01:26
@dosubot dosubot Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. docs::upgrade-guide-required require dedicated upgrade guide in realease note labels May 26, 2026
@wylswz
wylswz requested a review from GareArc May 26, 2026 02:33
wylswz
wylswz previously approved these changes May 26, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 26, 2026
GareArc
GareArc previously approved these changes May 26, 2026
@GareArc
GareArc enabled auto-merge May 26, 2026 04:13
wylswz
wylswz previously approved these changes May 26, 2026
@QuantumGhost
QuantumGhost marked this pull request as draft May 26, 2026 06:26
@QuantumGhost
QuantumGhost force-pushed the feat/model-type-migration-script branch from fbdfee2 to ee8d69c Compare May 26, 2026 06:26
hjlarry
hjlarry previously approved these changes May 26, 2026

@hjlarry hjlarry left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@QuantumGhost
QuantumGhost marked this pull request as ready for review May 26, 2026 07:26
@QuantumGhost
QuantumGhost force-pushed the feat/model-type-migration-script branch from 6ee5a35 to 1a9fdef Compare May 26, 2026 16:03
@wylswz
wylswz enabled auto-merge May 27, 2026 02:09
@wylswz
wylswz added this pull request to the merge queue May 27, 2026
Merged via the queue into langgenius:main with commit 5c5a6e8 May 27, 2026
42 checks passed
Ssazurov pushed a commit to Ssazurov/dify that referenced this pull request Aug 8, 2026
@QuantumGhost QuantumGhost mentioned this pull request Sep 1, 2026
6 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs::upgrade-guide-required require dedicated upgrade guide in realease note lgtm This PR has been approved by a maintainer size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Legacy model_type values break custom model credential management

4 participants