Database Refactoring

Sample Changelog File

This is a sample db.changelog.xml file used by the migrator.  Some things to notice include:

  • You can have multiple commands within a change set
  • You can have have duplicate “id” tags because the unique identifier is the “id” tag AND the “author” tag AND the file name and location
  • You can include other files
  • You can use the pre-made tags like “createTable” or “insert”, or use can use raw SQL
  • You can use the “runAlways” and “runOnChange” changelog attributes to control when a changelog will run
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
  xmlns=
        "http://www.sundog.net/xml/ns/dbchangelog/1.0"
  xmlns:xsi=
        "http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation=
        "http://www.sundog.net/xml/ns/dbchangelog/1.0 
         http://www.sundog.net/xml/ns/dbchangelog/dbchangelog-1.0.xsd">

    <changeSet id="1" author="bob">
        <createTable tableName="department">
            <column name="id" type="varchar(50)">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="active" type="number(1)" defaultValue="1"/>
        </createTable>
    </changeSet>    
    <changeSet id="2" author="bob">
        <createTable tableName="employee">
            <column name="id" type="int">
                <constraints nullable="false" primaryKey="true"/>
            </column>
            <column name="name" type="varchar(255)"/>
            <column name="salary" type="float" defaultValue="0"/>
            <column name="department_id">
                <constraints foreignKeyName="fk_employee_department" 
                          references="department(id)" 
                          initiallyDeffered="true" 
                          nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet id="2" author="alice">
        <createTable tableName="location">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <sql>alter table employee add location_id 
                      constraint fk_employee_department 
                      references location(id)</sql>
    </changeSet>
    <changeSet id="3" author="alice">
	<insert tableName="location">
            <column name="id" value="1"/>
            <column name="name" value="North Branch"/>
        </insert>
	<insert tableName="location">
            <column name="id" value="2"/>
            <column name="name" value="South Branch"/>
        </insert>
	<insert tableName="location">
            <column name="id" value="3"/>
            <column name="name" value="Headquarters"/>
        </insert>
    </changeSet>
    <changeSet id="5" author="bob" runAlways="true">
        <sql>update NewsItems set content=DATE_FORMAT(now(), '%W %M %Y %H:%i:%s') where id=2</sql>
    </changeSet>

    <include file="com/example/productdata/db.changelog.xml"/>
</databaseChangeLog>