A Procedure
is a schema object
that combines a set of SQL statements and queries
together to perform a specific task. Procedures
are created in a schema
and stored in a database.
Liquibase
provides a createProcedure
Change Type to create a Procedure
.
Create Procedure in ChangeSet
Simple and small Procedures
, the SQL
statements can be added to the Changeset
only.
<?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 autoIncrement="true" name="apple_id" type="BIGINT">
<constraints unique="true" primaryKey="true" />
</column>
<column name="apple_name" type="VARCHAR(20)" />
<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
</createTable>
</changeSet>
<changeSet author="wesome" id="1692701200879-2">
<insert catalogName="appledb" tableName="apple">
<column name="apple_name" value="Macintosh" />
<column name="current_date_time" valueDate="now()" />
</insert>
</changeSet>
<changeSet author="wesome" id="1692701200879-3">
<insert catalogName="appledb" tableName="apple">
<column name="apple_name" value="Fuji" />
<column name="current_date_time" valueDate="now()" />
</insert>
</changeSet>
<changeSet author="wesome" id="1692701200879-4">
<insert catalogName="appledb" tableName="apple">
<column name="apple_name" value="Gala" />
<column name="current_date_time" valueDate="now()" />
</insert>
</changeSet>
<changeSet author="wesome" id="1692701200879-5">
<createProcedure catalogName="appleDb" dbms="mysql" encoding="UTF-8" procedureName="appleCount" relativeToChangelogFile="true" replaceIfExists="true" schemaName="appleDb">
CREATE PROCEDURE appleCount()
BEGIN
SELECT * FROM appleDb.apple;
END
</createProcedure>
</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
The procedure can be invoked by running the command
CALL appleDb.appleCount;
Create Procedure in Sql file
Complex Procedures
should be stored in individual separate files, Liquibase
allows to include SQL files containing Procedures
.
procedure.sql
CREATE PROCEDURE appleCount()
BEGIN
SELECT * FROM appleDb.apple;
END
<?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 autoIncrement="true" name="apple_id" type="BIGINT">
<constraints unique="true" primaryKey="true" />
</column>
<column name="apple_name" type="VARCHAR(20)" />
<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
</createTable>
</changeSet>
<changeSet author="wesome" id="1692701200879-2">
<insert catalogName="appledb" tableName="apple">
<column name="apple_name" value="Macintosh" />
<column name="current_date_time" valueDate="now()" />
</insert>
</changeSet>
<changeSet author="wesome" id="1692701200879-3">
<insert catalogName="appledb" tableName="apple">
<column name="apple_name" value="Fuji" />
<column name="current_date_time" valueDate="now()" />
</insert>
</changeSet>
<changeSet author="wesome" id="1692701200879-4">
<insert catalogName="appledb" tableName="apple">
<column name="apple_name" value="Gala" />
<column name="current_date_time" valueDate="now()" />
</insert>
</changeSet>
<changeSet author="wesome" id="1692701200879-5">
<createProcedure catalogName="appleDb" dbms="mysql" path="procedure.sql" encoding="UTF-8" procedureName="appleCount" relativeToChangelogFile="true" replaceIfExists="true" schemaName="appleDb">
</createProcedure>
</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
The procedure can be invoked by running the command
CALL appleDb.appleCount;