Liquibase
createTable allows to definition of all columns
required for creating table
, but sometimes requirement is to add some new columns in existing tables. Liquibase
provides addColumn
ChangeType to alter the existing table and add a new column
.
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet author="wesome" id="1693111591793-1">
<createTable catalogName="appledb" schemaName="appledb" tablespace="appledb" tableName="apple">
<column name="apple_id" type="BIGINT">
<constraints unique="true" primaryKey="true" />
</column>
<column name="apple_name" type="VARCHAR(20)" />
</createTable>
</changeSet>
<changeSet author="wesome" id="1693111591793-2">
<addColumn catalogName="appledb" schemaName="appledb" tableName="apple">
<column afterColumn="apple_name" name="current_date_time" type="datetime">
<constraints nullable="false" />
</column>
</addColumn>
</changeSet>
<changeSet author="wesome" id="1692599548266-3">
<insert catalogName="appledb" tableName="apple">
<column name="apple_id" value="1" />
<column name="apple_name" value="Fuji" />
<column name="current_date_time" valueDate="now()" />
</insert>
</changeSet>
</databaseChangeLog>
liquibase.properties (update changeLog File file format with SQL, XML, YAML, JSON as per changelog.mysql.)
changeLogFile=changelog.mysql.xml
liquibase.command.url:jdbc:mysql://localhost:3306/appleDb?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
liquibase.command.username : root
liquibase.command.password : rootroot
Run command liquibase update