<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.3 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.3.xsd">
    <preConditions>
        <dbms type="mysql"/>
        <sqlCheck expectedResult="1">select 1</sqlCheck>
    </preConditions>

    <changeSet id="1" author="nvoxland">
        <comment>
            You can add comments to changeSets.
            They can even be multiple lines if you would like.
            They aren't used to compute the changeSet MD5Sum, so you can update them whenever you want without causing
            problems.
        </comment>
        <createTable tableName="person">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="firstname" type="varchar(50)"/>
            <column name="lastname" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet id="2" author="nvoxland">
        <comment>Add a username column so we can use "person" for authentication</comment>
        <addColumn tableName="person">
            <column name="usernae" type="varchar(8)"/>
        </addColumn>
    </changeSet>
    <changeSet id="3" author="nvoxland">
        <comment>Fix misspelled "username" column</comment>
        <renameColumn tableName="person" oldColumnName="usernae" newColumnName="username" columnDataType="varchar(8)"/>
    </changeSet>
    <changeSet id="5" author="nvoxland" context="test">
        <insert tableName="person">
            <column name="firstname" value="John"/>
            <column name="lastname" value="Doe"/>
            <column name="username" value="jdoe"/>
        </insert>
        <insert tableName="person">
            <column name="firstname" value="Jane"/>
            <column name="lastname" value="Doe"/>
            <column name="username" value="janedoe"/>
        </insert>
        <insert tableName="person">
            <column name="firstname" value="Bob"/>
            <column name="lastname" value="Johnson"/>
            <column name="username" value="bjohnson"/>
        </insert>
    </changeSet>
    <changeSet id="6" author="nvoxland">
        <comment>Don't keep username in the person table</comment>
        <dropColumn tableName="person" columnName="username"/>
    </changeSet>
    <changeSet id="7" author="nvoxland">
        <createTable tableName="employee">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet id="7" author="bjohnson" context="test">
        <insert tableName="employee">
            <column name="name" value="ACME Corp"/>
        </insert>
        <insert tableName="employee">
            <column name="name" value="Widgets Inc."/>
        </insert>
    </changeSet>
    <changeSet id="7a" author="nvoxland">
        <addColumn tableName="employee">
            <column name="company_id" type="int">
                <constraints nullable="true" foreignKeyName="fk_employee_company" references="company(id)"/>
            </column>
        </addColumn>
    </changeSet>
    <changeSet id="8" author="bjohnson">
        <dropNotNullConstraint tableName="employee" columnName="name" columnDataType="varchar(50)"/>
    </changeSet>
    <changeSet id="8.1" author="bjohnson">
        <comment>I guess name needs to be not-null</comment>
        <addNotNullConstraint tableName='employee' columnName="name" defaultNullValue="UNKNOWN"
                              columnDataType="varchar(50)"/>
    </changeSet>
    <changeSet id="9" author="nvoxland">
        <renameTable oldTableName="employee" newTableName="company"/>
    </changeSet>
    <changeSet id="10" author="nvoxland">
        <createTable tableName="testtable">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="value" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="person_id" type="int">
                <constraints nullable="false" foreignKeyName="fk_test_person" references="person(id)"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet id="11" author="nvoxland">
        <dropTable tableName="testtable"/>
    </changeSet>

    <changeSet id="12" author="nvoxland">
        <createIndex indexName="idx_company_name" tableName="company">
            <column name="name"/>
        </createIndex>
        <createIndex indexName="idx_person_lastname" tableName="person">
            <column name="lastname"/>
        </createIndex>
    </changeSet>

    <changeSet id="13" author="nvoxland">
        <dropIndex indexName="idx_person_lastname" tableName="person"/>
    </changeSet>

    <changeSet id="14" author="nvoxland">
        <createTable tableName="liquibaseRunInfo">
            <column name="timesRan" type="int"/>
        </createTable>
        <insert tableName="liquibaseRunInfo">
            <column name="timesRan" valueNumeric="1"/>
        </insert>
    </changeSet>

    <changeSet id="15" author="nvoxland">
        <sql>update liquibaseRunInfo set timesRan=timesRan+1</sql>
    </changeSet>

    <changeSet id="16" author="nvoxland">
        <createView viewName="personView">
            select firstname, lastname from person
        </createView>
    </changeSet>

    <changeSet id="18" author="nvoxland">
        <dropView viewName="personView"/>
    </changeSet>

    <changeSet id="19" author="nvoxland">
        <mergeColumns
                tableName="person"
                column1Name="firstname"
                joinString=" "
                column2Name="lastname"
                finalColumnName="fullname"
                finalColumnType="varchar(100)"/>
    </changeSet>

    <changeSet id="20" author="nvoxland">
        <createView viewName="personView">
            select id, fullname from person
        </createView>
    </changeSet>

    <changeSet id="21" author="nvoxland">
        <renameView oldViewName="personView" newViewName="v_person"/>
    </changeSet>

    <changeSet id="22" author="nvoxland">
        <addColumn tableName="person">
            <column name="employer_id" type="int"/>
        </addColumn>
    </changeSet>

    <changeSet id="23" author="nvoxland">
        <addForeignKeyConstraint
                baseTableName="person" baseColumnNames="employer_id"
                constraintName="fk_person_employer"
                referencedTableName="company" referencedColumnNames="id"
                deleteCascade="true"/>
    </changeSet>

    <changeSet id="24" author="nvoxland">
        <dropForeignKeyConstraint baseTableName="person" constraintName="fk_person_employer"/>
    </changeSet>

    <changeSet id="25" author="nvoxland">
        <createTable tableName="address">
            <column name="id" type="int"/>
            <column name="line1" type="varchar(255)"/>
            <column name="line2" type="varchar(255)"/>
            <column name="city" type="varchar(255)"/>
            <column name="state" type="char(2)"/>
            <column name="postalcode" type="varchar(15)"/>
        </createTable>
    </changeSet>

    <changeSet id="25.1" author="nvoxland">
        <addNotNullConstraint tableName="address" columnName="id" columnDataType="int"/>
    </changeSet>

    <changeSet id="25.2" author="nvoxland">
        <addPrimaryKey tableName="address" columnNames="id" constraintName="pk_address"/>
    </changeSet>

    <changeSet id="25.3" author="nvoxland">
        <addAutoIncrement tableName="address" columnName="id" columnDataType="int"/>
    </changeSet>

    <changeSet id="26" author="nvoxland">
        <insert tableName="address">
            <column name="line1" value="123 4th St"/>
            <column name="line2" value="Suite 432"/>
            <column name="city" value="New York"/>
            <column name="state" value="NY"/>
            <column name="postalcode" value="01235"/>
        </insert>
        <insert tableName="address">
            <column name="line1" value="6123 64th St"/>
            <column name="city" value="New York"/>
            <column name="state" value="NY"/>
            <column name="postalcode" value="01235"/>
        </insert>
        <insert tableName="address">
            <column name="line1" value="One LiquiBase Way"/>
            <column name="city" value="Fargo"/>
            <column name="state" value="ND"/>
            <column name="postalcode" value="58103"/>
        </insert>
        <insert tableName="address">
            <column name="line1" value="123 Main Ave"/>
            <column name="city" value="City With No State"/>
            <column name="postalcode" value="00000"/>
        </insert>
    </changeSet>

    <changeSet id="27" author="nvoxland">
        <addLookupTable
                existingTableName="address" existingColumnName="state"
                newTableName="state" newColumnName="id" newColumnDataType="char(2)"/>
    </changeSet>

    <changeSet id="28" author="nvoxland">
        <addDefaultValue tableName="address" columnName="line2" defaultValue="N/A"/>
    </changeSet>

    <changeSet id="29" author="nvoxland">
        <dropDefaultValue tableName="address" columnName="line2"/>
    </changeSet>

    <changeSet id="30" author="nvoxland">
        <createTable tableName="pkTest">
            <column name="id" type="int"/>
            <column name="value" type="varchar(50)"/>
        </createTable>
    </changeSet>

    <changeSet id="31" author="nvoxland">
        <addPrimaryKey tableName="pkTest" columnNames="id" constraintName="pk_pktest"/>
    </changeSet>

    <changeSet id="32" author="nvoxland">
        <dropPrimaryKey tableName="pkTest" constraintName="pk_pktest"/>
    </changeSet>

    <changeSet id="33" author="nvoxland">
        <addUniqueConstraint tableName="address" columnNames="line1, line2" constraintName="uq_address_line1line2"/>
    </changeSet>

    <changeSet id="34" author="nvoxland">
        <dropUniqueConstraint tableName="address" constraintName="uq_address_line1line2"/>
    </changeSet>


    <changeSet id="35" author="nvoxland">
        <createTable tableName="tableToRollback">
            <column name="id" type="int"/>
        </createTable>
    </changeSet>

    <!--<changeSet id="50" author="nvoxland">-->
        <!--<modifyColumn tableName="address">-->
            <!--<column name="postalcode" type="varchar(20)"/>-->
        <!--</modifyColumn>-->
    <!--</changeSet>-->

    <changeSet id="51" author="nvoxland">
        <createTable tableName="nulldefaulttest">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="firstname" type="varchar(50)"/>
            <column name="test" type="bigint" defaultValue="null"/>
        </createTable>
    </changeSet>

    <changeSet id="51b" author="nvoxland">
        <addColumn tableName="nulldefaulttest">
            <column name="foreign_id" type="bigint" defaultValue="null"/>
        </addColumn>
    </changeSet>

    <changeSet id="52" author="pkeeble">
        <sqlFile path="changelogs/mysql/complete/sample.sql" stripComments="true"/>
    </changeSet>

    <include file="changelogs/mysql/complete/included.changelog.xml"/>

    <include file="changelogs/mysql/complete/renamed.changelog.xml"/>

    <include file="changelogs/common/common.tests.changelog.xml" />

    <include file="changelogs/common/autoincrement.tests.changelog.xml" />

    <include file="changelogs/common/schema.tests.changelog.xml" />

    <changeSet id="54" author="nvoxland">
        <addColumn tableName="address">
            <column name="newcol" type="varchar(255)"/>
        </addColumn>
    </changeSet>
    <changeSet id="55" author="nvoxland">
        <addColumn tableName="address">
            <column name="newcol2" type="varchar(255)"/>
        </addColumn>
    </changeSet>

    <!--<changeSet id="56" author="nvoxland">-->
        <!--<customChange class="liquibase.change.custom.ExampleCustomSqlChange">-->
            <!--<param name="tableName" value="person"/>-->
            <!--<param name="columnName" value="employer_id"/>-->
            <!--<param name="newValue" value="3"/>-->
        <!--</customChange>-->
    <!--</changeSet>-->
    <!--<changeSet id="57" author="nvoxland">-->
        <!--<customChange class="liquibase.change.custom.ExampleCustomSqlChange" tableName="person" columnName="employer_id" newValue="4"/>-->
    <!--</changeSet>-->
    <!--<changeSet id="58" author="nvoxland">-->
        <!--<customChange class="liquibase.change.custom.ExampleCustomTaskChange" helloTo="world"/>-->
    <!--</changeSet>-->

    <!--<changeSet id="60" author="nvoxland">-->
        <!--<executeCommand executable="getmac" os="Windows XP">-->
            <!--<arg value="/FO"/>-->
            <!--<arg value="CSV"/>-->
        <!--</executeCommand>-->
    <!--</changeSet>-->

</databaseChangeLog>

