Search code examples
postgresqlliquibase

Managing common SQL code in postgres database via liquibase


We are using SQL formatted changelogs. We have a sql file, create_db.sql for each postgres database managed via liquibase. We have few common tables for each database which is placed in a common.sql file.

It looks like include element can serve my purpose. But it does not work with SQL formatted changelogs.

One approach I can think of handling this case is to not have common.sql altogether and place all the common code in each create_db.sql as I could not find any way of importing common.sql in each create_db.sql to be managed via liquibase using sql formatted changelogs. But this will cause a lot of code duplication.

Can someone suggest some other better way of managing the common.sql file also via liquibase for each postgres database?


Solution

  • I misunderstood the below statement in the documentation.

    At this time, the tag cannot be used in an SQL formatted root changelog.

    Only root changelog cannot be SQL formatted but <include> does work with SQL formatted changelogs as explained by the below statement

    Your reference changelog can be SQL, XML, YAML, or JSON file types.

    The example in the documentation also shows the same

    <?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">
        <include file="com/example/news/news.changelog.sql"/>
        <include file="com/example/directory/directory.changelog.sql"/>
    </databaseChangeLog>