Quantcast
Channel: Liquibase Forums
Viewing all articles
Browse latest Browse all 2993

Re : How to create a "super"-table which will be always created infront of any table?

$
0
0
Hello Steve,

many thanks for you quick response.

I am using right now the XML format.

Before going on for me it is not clear what you mean with: 
"If you intend to use the XML changelog format and enforce that all changes to all database instances are made by developers/DBAs making additions to the XML changelog"
Sounds like you would recommend a different approach?

Back to the previous question.

Here our template table file:  000_DefaultTableTemplate.xml
  1. <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
  3.     xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
  4.         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.3.xsd
  5.         http://www.liquibase.org/xml/ns/dbchangelog-ext
  6.         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

  7.     <changeSet author="${table.author}" id="${changeset.name}_DefaultTable">
  8.         <createTable schemaName="${table.schema}" tableName="${table.name}">
  9.             <column name="id" type="int" remarks="surrogate key for the row" />
  10.             <column name="uuid" type="uuid" remarks="additional surrogate key for the row, which ensures also uniqueness over mulitple database instances"/>
  11.             <column name="rowCreatedUserId" type="${varchar50}" remarks="userId of the user who created this row"/>
  12.             <column name="rowCreated" type="timestamp" remarks="insert timestamp of this row, timestamp stores data in UTC" />
  13.             <column name="rowUpdatedUserId" type="${varchar50}" remarks="userId of the user who last updated this row"/>
  14.             <column name="rowUpdated" type="timestamp" remarks="last update timestamp of this row, timestamp stores data in UTC" />
  15.         </createTable>

  16.         <!-- mandatory not null constraints on default columns -->
  17.         <addNotNullConstraint columnName="id" columnDataType="int" schemaName="${table.schema}" tableName="${table.name}" />
  18.         <addNotNullConstraint columnName="uuid"  columnDataType="uuid" schemaName="${table.schema}" tableName="${table.name}" />
  19.         <!-- TODO: disabled until user management is available <addNotNullConstraint columnName="rowCreatedUserId" columnDataType="${varchar50}" schemaName="${table.schema}" tableName="${table.name}" /> -->
  20.         <addNotNullConstraint columnName="rowCreated" columnDataType="timestamp" schemaName="${table.schema}" tableName="${table.name}" />
  21.         <!-- TODO: disabled until user management is available<addNotNullConstraint columnName="rowUpdatedUserId" columnDataType="${varchar50}" schemaName="${table.schema}" tableName="${table.name}" /> -->
  22.         <addNotNullConstraint columnName="rowUpdated" columnDataType="timestamp" schemaName="${table.schema}" tableName="${table.name}" />

  23.         <!-- create primary key -->
  24.         <addPrimaryKey columnNames="id" constraintName="pk_${table.name}" schemaName="${table.schema}" tableName="${table.name}" />
  25.         <addAutoIncrement columnName="id" columnDataType="int" tableName="${table.name}" />

  26.         <!-- create unique index on uuid -->
  27.         <createIndex indexName="Idx${table.name}Uuid" unique="true" schemaName="${table.schema}" tableName="${table.name}" >
  28.             <column name="uuid" type="varchar(255)" />
  29.         </createIndex>
  30.     </changeSet>
  31. </databaseChangeLog>
and the other files including it:

Translation.xml
  1. <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
  3.     xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.3.xsd
  4.         http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

  5.     <!-- include the default properties -->
  6.     &propertiesAll;

  7.     <property name="table.schema" value="${schema}" />
  8.     <property name="table.name" value="Translations" />
  9.     <property name="table.author" value="hkais" />
  10.     <property name="changeset.number" value="001" />
  11.     <property name="changeset.operation" value="Create" />
  12.     <property name="changeset.name" value="${changeset.number}_${changeset.operation}${table.name}" />

  13.     <!-- create default table ${table.name} -->
  14.     <include file="000_DefaultTableTemplate.xml" relativeToChangelogFile="true" />

  15.     <changeSet author="${table.author}" id="${changeset.name}">
  16.         <addColumn schemaName="${schema}" tableName="${table.name}">
  17.             <column name="locale" type="${varchar5}" beforeColumn="rowCreatedUserId" remarks="translation locale" />
  18.         </addColumn>
  19.         <addNotNullConstraint columnName="locale" columnDataType="${varchar5}" schemaName="${table.schema}" tableName="${table.name}" />
  20. </changeSet>
The varchar properties are imported as xml entities at one place, so we have some defaults for the developers and to keep them same for the DBAs to administer their storages/tablespaces.
With the above changesets we have nearly our solution, but only nearly.
The issue now is, since we are using the property  ${changeset.name}_DefaultTable and we assumed this property gets renewed on every new changeset file like the Translation.xml we get a collision due to duplicateded ids of the changeset. So this lets me assume the properties are immutable, or I am doing something wrong?

We could debug in more depth in the IDE, but we would need some advice there to take a look.

best wishes

Viewing all articles
Browse latest Browse all 2993

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>