如何管理Properties文件适配多Schema/数据库应用并实现单命令批量更新
配置Liquibase多Schema批量更新的properties文件方案
你的应用包含多个独立Schema,每个对应不同的数据库账号密码,要通过单条命令完成所有Schema的变更更新,基于你给出的目录结构,下面是具体配置和操作方法:
1. 配置liquibase.properties文件
在根目录的liquibase.properties里,为每个Schema单独配置一套数据库连接和变更集参数,用不同前缀区分,示例如下:
# Schema1 相关配置 schema1.url=jdbc:mysql://localhost:3306/schema1_db schema1.username=user1 schema1.password=pass1 schema1.changeLogFile=Schema1-folder/changelog.xml # Schema2 相关配置 schema2.url=jdbc:mysql://localhost:3306/schema2_db schema2.username=user2 schema2.password=pass2 schema2.changeLogFile=Schema2-folder/changelog.xml # Schema3 相关配置 schema3.url=jdbc:mysql://localhost:3306/schema3_db schema3.username=user3 schema3.password=pass3 schema3.changeLogFile=Schema3-folder/changelog.xml
2. 编写批量执行脚本
Liquibase本身没有直接的多Schema批量执行命令,所以需要写个脚本(Windows用.bat,Linux/macOS用.sh)遍历所有Schema配置,逐个执行更新命令。
Linux/macOS 脚本(命名为update-all.sh)
#!/bin/bash # 读取配置并执行Schema1的更新 liquibase --defaultsFile=liquibase.properties --url=$(grep 'schema1.url' liquibase.properties | cut -d'=' -f2) --username=$(grep 'schema1.username' liquibase.properties | cut -d'=' -f2) --password=$(grep 'schema1.password' liquibase.properties | cut -d'=' -f2) --changeLogFile=$(grep 'schema1.changeLogFile' liquibase.properties | cut -d'=' -f2) update # 执行Schema2的更新 liquibase --defaultsFile=liquibase.properties --url=$(grep 'schema2.url' liquibase.properties | cut -d'=' -f2) --username=$(grep 'schema2.username' liquibase.properties | cut -d'=' -f2) --password=$(grep 'schema2.password' liquibase.properties | cut -d'=' -f2) --changeLogFile=$(grep 'schema2.changeLogFile' liquibase.properties | cut -d'=' -f2) update # 执行Schema3的更新 liquibase --defaultsFile=liquibase.properties --url=$(grep 'schema3.url' liquibase.properties | cut -d'=' -f2) --username=$(grep 'schema3.username' liquibase.properties | cut -d'=' -f2) --password=$(grep 'schema3.password' liquibase.properties | cut -d'=' -f2) --changeLogFile=$(grep 'schema3.changeLogFile' liquibase.properties | cut -d'=' -f2) update
Windows 脚本(命名为update-all.bat)
@echo off # 读取Schema1配置并执行更新 for /f "tokens=2 delims==" %%a in ('findstr "schema1.url" liquibase.properties') do set schema1_url=%%a for /f "tokens=2 delims==" %%a in ('findstr "schema1.username" liquibase.properties') do set schema1_user=%%a for /f "tokens=2 delims==" %%a in ('findstr "schema1.password" liquibase.properties') do set schema1_pass=%%a for /f "tokens=2 delims==" %%a in ('findstr "schema1.changeLogFile" liquibase.properties') do set schema1_changelog=%%a liquibase --defaultsFile=liquibase.properties --url=%schema1_url% --username=%schema1_user% --password=%schema1_pass% --changeLogFile=%schema1_changelog% update # 读取Schema2配置并执行更新 for /f "tokens=2 delims==" %%a in ('findstr "schema2.url" liquibase.properties') do set schema2_url=%%a for /f "tokens=2 delims==" %%a in ('findstr "schema2.username" liquibase.properties') do set schema2_user=%%a for /f "tokens=2 delims==" %%a in ('findstr "schema2.password" liquibase.properties') do set schema2_pass=%%a for /f "tokens=2 delims==" %%a in ('findstr "schema2.changeLogFile" liquibase.properties') do set schema2_changelog=%%a liquibase --defaultsFile=liquibase.properties --url=%schema2_url% --username=%schema2_user% --password=%schema2_pass% --changeLogFile=%schema2_changelog% update # 读取Schema3配置并执行更新 for /f "tokens=2 delims==" %%a in ('findstr "schema3.url" liquibase.properties') do set schema3_url=%%a for /f "tokens=2 delims==" %%a in ('findstr "schema3.username" liquibase.properties') do set schema3_user=%%a for /f "tokens=2 delims==" %%a in ('findstr "schema3.password" liquibase.properties') do set schema3_pass=%%a for /f "tokens=2 delims==" %%a in ('findstr "schema3.changeLogFile" liquibase.properties') do set schema3_changelog=%%a liquibase --defaultsFile=liquibase.properties --url=%schema3_url% --username=%schema3_user% --password=%schema3_pass% --changeLogFile=%schema3_changelog% update
3. 执行批量更新
- Linux/macOS:先给脚本加执行权限
chmod +x update-all.sh,然后运行./update-all.sh - Windows:直接双击
update-all.bat,或者在命令行里执行update-all.bat
这样就能通过一条脚本命令完成所有Schema的变更更新了。后续如果新增Schema,只需要在liquibase.properties里添加对应前缀的配置,再在脚本里加一条对应的执行命令就行。
内容的提问来源于stack exchange,提问作者Workonphp
相关产品推荐
相关产品推荐

