Liquibase Hello world

Getting start with Liquibase

Creating Your First Liquibase Migration Using SQL

text
https://docs.liquibase.com/workflows/liquibase-community/migrate-with-sql.html

Best Practices for Managing Liquibase Changelogs

Install on Ubuntu

bash
sudo snap install liquibase

Connect to the Postgres

text
https://docs.liquibase.com/start/install/tutorials/postgresql.html

Sql command in liquibase

sql
-- liquibase formatted sql
-- changeset liquibase:1
CREATE TABLE test_table (
    test_id INT,
    test_column VARCHAR(256),
    PRIMARY KEY (test_id)
);

run the Liquibase status command to see whether the connection is successful:

bash
liquibase --username=test --password=test --changelog-file=<changelog.xml> status

How to configuration changelog.xml

xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<include file="changesets/db-agnostic/initial-schema.xml"/>
<!-- Load Example Data -->
<include file="data/loadCOUNTRIES.sql"/>
<include file="data/loadCITIES.sql"/>
<include file="data/loadAIRLINES.sql"/>
<include file="data/loadFLIGHTS1.sql"/>
<include file="data/loadFLIGHTS2.sql"/>
<include file="data/loadFLIGHTAVAILABILITY1.sql"/>
<include file="data/loadFLIGHTAVAILABILITY2.sql"/>
<include file="changesets/db-agnostic/schema-only.xml"/>
<include file="changesets/db-agnostic/qa-only.xml"/>
<include file="changesets/db-agnostic/sql-server-only.xml"/>
<include file="changesets/db-agnostic/tag-database.xml"/>
</databaseChangeLog>

included all files in SQL

xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog  http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd  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">
<includeAll path="com/example/changelogs/"/>
<includeAll path="liquibase/parser/core/yaml/included/"/>
</databaseChangeLog>

relativeToChangelogFile : Specifies whether the file path is relative to the changelog file rather than looked up in the search path. Default: false.

SQL Format Example

Full Example to how make liquibase with SQL

xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog     xmlns_xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi_schemaLocation="https://www.liquibase.org/xml/ns/dbchangelog    https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.5.xsd">
<includeAll  path="migrations/" relativeToChangelogFile="true"/>
</databaseChangeLog>

As with changesets in other formats, each changeset must have an author, a unique identifier, and should be an “atomic unit of change”.

sql
-- liquibase formatted sql
-- changeset stevedonie:create-test-table
CREATE TABLE testTable (
    columnName1 VARCHAR(355)
);

-- rollback DROP TABLE testTable;

Make sure your SQL changelogs are in the XML changelog file

text
<include file="<path to SQL files>/<changelog file name>.sql"
relativeToChangelogFile="true" />

Liquibase Change Types

Use Liquibase command

text
Liquibase update

Use Docker for Liquibase

text
https://docs.liquibase.com/workflows/liquibase-community/using-liquibase-and-docker.html?Highlight=p
ostgres

Run and execute commands update and rollback

after install docker and go into the pod of Liquibase, we can execute update command

bash
docker-compose -f docker-compose-postgres.yml exec liquibase /bin/sh

update

bash
liquibase update --url=jdbc:postgresql://<db_host>:5432/<db_name>?currentSchema=public
--changelog-file=changelog.xml --username=<db_username> --password=<db_password>

rollback

text
https://docs.liquibase.com/commands/rollback/rollback-by-tag.html
https://docs.liquibase.com/commands/rollback/rollback-sql.html
bash
liquibase rollback-count --count=1
--url=jdbc:postgresql://<db_host>:5432/<db_name>?currentSchema=public --changelog-file=changelog.xml
--username=<db_username> --password=<db_password>