Liquibase with multiple schemas

2019-05-01 01:40发布

问题:

I have a software wich uses multiple schemas. I made db.changelog-master.xml to maintain different releases and db.changelog-S16.xml to current sprint (it's currently sprint 16 going on). db.changelog-S16.xml is looking like this:

<?xml version="1.0" encoding="UTF-8"?> 
<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-3.1.xsd">

  <include file="my_schema_1.xml" relativeToChangelogFile="true" /> 
</databaseChangeLog>

Problem here is, that I want to make one changelog which could update all the schemas in one but I have failed to find a way to hardcode the schemaname in the changelog. Can it be obtained to the changelog via generateChangelog somehow (so the schemanames would be in my_schema_1.xml), or can I type it to the changelog? What I am looking for would be like this:

 <?xml version="1.0" encoding="UTF-8"?> 
<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-3.1.xsd">

    <TAG WHERE I COULD INSERT SCHEMANAME>
  <include file="my_schema_1.xml" relativeToChangelogFile="true" /> 
      <TAG WHERE I COULD INSERT SCHEMANAME>
    <include file="my_schema_2.xml" relativeToChangelogFile="true" />
       etc...
</databaseChangeLog>

Because now it is only possibly to change schema in the update clause when running the liquibase from the commandline. This would be handy too when I am making customer databases to their servers. I could just make customer_master.xml and hardcode their schemanames in it and then run it to their server from the scratch. So far only place I have found to insert schema name is when you make the commandline call. That would mean that I would have to update each schema differently or make some powershell script to do them for me...

回答1:

Most change tags have a "schemaName" attribute that you can use to specify the schema. For example, <createTable tableName="person" schemaName="customer">

If your schemas are always named the same, you can hard code them directly into the change tags. If they change from deploy to deply, you probably want to use changelog parameters so you can use <createTable tableName="person" schemaName="${primarySchema}"> and then specify what primarySchema is equal to at runtime.

You will need to specify the schemaName on each changeSet, otherwise it will use the default schemaName which cannot be changed part way through the script.



标签: liquibase