Liquibase:如何在databaseChangeLog中指定schema名称
在XML格式databaseChangeLog中指定Schema的几种方式
1. 全局默认Schema
直接在<databaseChangeLog>根节点设置defaultSchemaName属性,后续所有变更集会自动使用该Schema:
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd" defaultSchemaName="schema_name"> <!-- 此变更集默认使用schema_name --> <changeSet author="quickstart-1" id="quickstart-2"> <createTable tableName="example_table"> <column name="id" type="INT"> <constraints primaryKey="true"/> </column> <column name="name" type="VARCHAR(50)"/> </createTable> </changeSet> </databaseChangeLog>
2. 单个变更集指定Schema
仅让特定变更集使用目标Schema,在<changeSet>节点添加schemaName属性:
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> <changeSet author="quickstart-1" id="quickstart-2" schemaName="schema_name"> <createTable tableName="example_table"> <column name="id" type="INT"> <constraints primaryKey="true"/> </column> <column name="name" type="VARCHAR(50)"/> </createTable> </changeSet> </databaseChangeLog>
3. 数据库对象级指定Schema
针对单个表、视图等对象,在对应标签中设置schemaName属性:
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> <changeSet author="quickstart-1" id="quickstart-2"> <createTable tableName="example_table" schemaName="schema_name"> <column name="id" type="INT"> <constraints primaryKey="true"/> </column> <column name="name" type="VARCHAR(50)"/> </createTable> </changeSet> </databaseChangeLog>
4. 原生SQL中显式指定Schema
使用<sql>标签执行语句时,直接在表名前加上Schema前缀:
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> <changeSet author="quickstart-1" id="quickstart-2"> <sql> CREATE TABLE schema_name.example_table (id INT PRIMARY KEY, name VARCHAR(50)); </sql> </changeSet> </databaseChangeLog>
注意:需确保执行变更的数据库用户拥有目标Schema的操作权限,比如你示例中创建Schema时指定的schema_name_user,要提前给它授予该Schema的读写权限。
内容的提问来源于stack exchange,提问作者m19v
相关产品推荐
相关产品推荐

