Optimize ClickHouse schema migration strategy #48
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
Schema migrations are currently handled by running all SQL files sequentially on every app startup in
ClickHouseConfig.initSchema(). Each new column or table change requires:ALTER TABLE ... ADD COLUMN IF NOT EXISTSfile (e.g.05-oidc-auto-signup.sql)SCHEMA_FILESarray inClickHouseConfig.javaAbstractClickHouseIT.javaclickhouse/init/for Docker entrypoint +src/main/resources/clickhouse/for classpath)This works but has scaling issues:
CREATE TABLE IF NOT EXISTSandALTER TABLE ADD COLUMN IF NOT EXISTS— unnecessary overhead as the schema growsSuggested Improvements
schema_migrations(version String, applied_at DateTime64)) and skip already-applied filesclickhouse/*.sql) instead of a hardcoded arrayclickhouse/init/Docker copies or generate them from the classpath resources at build timeThe key improvements from this issue have been implemented in
ClickHouseConfig.java:PathMatchingResourcePatternResolverwithclasspath:clickhouse/*.sql— no hardcoded file list.sqlfiles are picked up automatically by filename sortThe
schema_migrationstracking table was not added, but all migrations useIF NOT EXISTSpatterns so re-running is idempotent with negligible overhead. Closing as sufficiently addressed.