Skip to content

feat: support disabling db auto migration#1727

Merged
looplj merged 2 commits into
looplj:unstablefrom
wangYX657211334:feature/disable_schema_init
Jun 2, 2026
Merged

feat: support disabling db auto migration#1727
looplj merged 2 commits into
looplj:unstablefrom
wangYX657211334:feature/disable_schema_init

Conversation

@wangYX657211334

Copy link
Copy Markdown
Contributor

需求背景
部分国产化或分布式数据库对标准 MySQL/PostgreSQL 的 DML(数据操纵)支持较好,但在 DDL(数据定义/表结构变更)方面的兼容性相对较弱(如不支持某些物理约束)。

主要修改
增加了禁用系统自动初始化数据 Schema 的功能选项(默认是启用的,不改变原始逻辑)。

业务收益
解耦了代码与底层的建表逻辑,使得 AxonHub 能够更加顺滑、方便地部署在各类国产化数据库上(用户可采用离线 SQL 脚本等方式手动完成建表)。

@greptile-apps

greptile-apps Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a disable_auto_migration configuration flag (default false) that lets operators skip the automatic Ent schema creation and data migration steps on startup — useful for databases that have limited DDL compatibility, where the user will apply schema scripts manually.

  • Adds DisableAutoMigration bool to db.Config, registers a Viper default, documents it in config.example.yml, and gates the Schema.Create + datamigrate.Run block on !cfg.DisableAutoMigration in ent.go.
  • The change is fully backward-compatible: the default is false, so all existing deployments continue to auto-migrate on startup without any configuration change.

Confidence Score: 4/5

Safe 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

Filename Overview
internal/server/db/ent.go Wraps both the DDL Schema.Create and the DML datamigrate step in a single !cfg.DisableAutoMigration guard; the data-migration concern flagged in the review thread is still unaddressed here.
internal/server/db/config.go Adds DisableAutoMigration bool field with consistent conf, yaml, and json struct tags — straightforward and correct.
conf/conf.go Registers db.disable_auto_migration default (false) with Viper, preserving backward-compatible behavior.
config.example.yml Documents the new option with the correct env-var name; comment is accurate and follows the style of adjacent entries.

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
Loading

Reviews (2): Last reviewed commit: "fix: update config `disable_schema_init`..." | Re-trigger Greptile

Comment thread internal/server/db/ent.go Outdated
Comment on lines 64 to 81
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)
}
}

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.

P1 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.

@looplj

looplj commented May 28, 2026

Copy link
Copy Markdown
Owner

帮忙改一下配置名, disable_auto_migration

@wangYX657211334 wangYX657211334 changed the title feat: support disabling db schema initialization feat: support disabling db auto migration Jun 2, 2026
@wangYX657211334

Copy link
Copy Markdown
Contributor Author

已修改

@looplj looplj merged commit 23b062c into looplj:unstable Jun 2, 2026
4 checks passed
junjiangao pushed a commit to junjiangao/axonhub that referenced this pull request Jun 5, 2026
* feat: support disabling db schema initialization

* fix: update config `disable_schema_init` to `disable_auto_migration`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants