feat: support disabling db auto migration#1727
Conversation
Greptile SummaryThis PR adds a
Confidence Score: 4/5Safe to merge for users who never set disable_auto_migration: true; those who do enable the flag will have both DDL and DML steps skipped — including data migrations that seed required rows — which can leave the database in an incomplete state. The flag's guard in ent.go covers Schema.Create (DDL) and datamigrate.Run (DML) as a single unit. The DML migrations (v0.3.0 creates the default project and fixes user-role timestamps; v0.4.0 creates the primary data-storage record) contain logic that the application depends on at runtime. An operator who enables the flag to bypass DDL will also bypass these DML steps without any runtime indication, potentially leaving the system without required seed records. internal/server/db/ent.go — the guard that controls both DDL and DML in a single block. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[NewEntClient called] --> B[Open master DB]
B --> C{ReadReplica DSN set?}
C -- Yes --> D[Open replica DB\nCreate routerDriver]
C -- No --> E[Use master driver]
D --> F[ent.NewClient]
E --> F
F --> G{cfg.DisableAutoMigration?}
G -- false\n(default) --> H[client.Schema.Create\nDDL: create/migrate tables]
H --> I[datamigrate.Run\nDML: seed rows, transform data]
I --> J[Return client]
G -- true --> J
J --> K[Application starts]
style G fill:#f9c74f,stroke:#f3722c
style H fill:#90be6d
style I fill:#90be6d
Reviews (2): Last reviewed commit: "fix: update config `disable_schema_init`..." | Re-trigger Greptile |
| if !cfg.DisableSchemaInit { | ||
| err = client.Schema.Create( | ||
| context.Background(), | ||
| migrate.WithGlobalUniqueID(false), | ||
| migrate.WithForeignKeys(false), | ||
| migrate.WithDropIndex(true), | ||
| migrate.WithDropColumn(true), | ||
| schema.WithHooks(schemahook.V0_3_0), | ||
| ) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| migrator := datamigrate.NewMigrator(client) | ||
| if err := migrator.Run(context.Background()); err != nil { | ||
| panic(err) | ||
| migrator := datamigrate.NewMigrator(client) | ||
| if err := migrator.Run(context.Background()); err != nil { | ||
| panic(err) | ||
| } | ||
| } |
There was a problem hiding this comment.
Data migrations silently skipped alongside schema init
Both the DDL schema creation and the datamigrate step are wrapped in the same guard. When a user sets disable_schema_init: true to handle DDL manually, the data migrations (which typically contain DML-only steps like seeding required rows or transforming existing data) are also bypassed. A user who manually applies the schema SQL scripts will never see a warning or error about the skipped data migrations, and the database can end up in an inconsistent state that is hard to diagnose. Consider either splitting the flag (e.g., disable_schema_ddl_init) to allow data migrations to still run, or at least logging a prominent warning when this flag is enabled so operators know they are responsible for running data migrations as well.
|
帮忙改一下配置名, disable_auto_migration |
|
已修改 |
* feat: support disabling db schema initialization * fix: update config `disable_schema_init` to `disable_auto_migration`
需求背景
部分国产化或分布式数据库对标准 MySQL/PostgreSQL 的 DML(数据操纵)支持较好,但在 DDL(数据定义/表结构变更)方面的兼容性相对较弱(如不支持某些物理约束)。
主要修改
增加了禁用系统自动初始化数据 Schema 的功能选项(默认是启用的,不改变原始逻辑)。
业务收益
解耦了代码与底层的建表逻辑,使得 AxonHub 能够更加顺滑、方便地部署在各类国产化数据库上(用户可采用离线 SQL 脚本等方式手动完成建表)。