For the complete documentation index, see llms.txt. This page is also available as Markdown.

Work with SQL Files

Learn how to organize SQL changelog files, structure includeAll blocks, and write maintainable formatted SQL changesets in Harness Database DevOps.

A well-structured SQL file directory ensures consistent, traceable, and collaborative database change management with Harness Database DevOps. This guide walks through best practices for organizing SQL changelogs, structuring includeAll blocks, and writing maintainable formatted SQL changesets.

Tip Consider this as an example implementation of the changelog directory structure.

SQL file directory organization

A well-organized SQL file directory structure is crucial for maintaining database changes. The following is a recommended structure:

db/
 ├── master.yaml                  # Main changelog file
 ├── changes/
 │   ├── release-1/
 │   │   ├── 001-create-users-table.sql
 │   │   ├── 002-add-email-column.sql
 │   │   └── 003-create-orders-table.sql
 │   └── release-2/
 │       ├── 001-add-payment-table.sql
 │       └── 002-alter-users-table.sql
 └── application/

Below are sample contents for the files referenced in the folder structure above.

db/master.yaml

databaseChangeLog:
  - includeAll:
      path: changes/release-1/
      relativeToChangelogFile: true
  
  - includeAll:
      path: changes/release-2/
      relativeToChangelogFile: true

Liquibase formatted SQL files

Harness Database DevOps supports seamless integration of Liquibase formatted SQL files, enabling you to track database changes effectively. Harness Database DevOps uses metadata in the SQL files to help identify and manage changes.

SQL files in Liquibase require special comments that contain metadata for tracking changes. Every Liquibase SQL file must begin with the following comment to indicate it contains these structured comments.

In the above example SQL schema, the comments:

  1. --liquibase formatted sql is required at the top of every SQL file to indicate it contains Liquibase metadata.

  2. --changeset john-doe:1 defines a changeset with the specified author (john-doe) and ID (1), which is unique within the changelog file. This comment is similar to YAML changesets.

SQL format rules

Do not include a space before --liquibase formatted sql. Formatting variations may cause the Liquibase parser to return an error. When specifying changeset attributes in a formatted SQL changelog, any attribute value that contains spaces must be quoted. For example:

Notice:

  • context: "production" does not need quotes, because there are no spaces, but it is valid to include them. Go to What is a context? to understand how contexts control deployment scope.

  • labels: "schema-update critical" requires quotes due to the space between words.

Tip

Using a space in an author name is not recommended. Prefer to use dot- or dash-separated names.

SQL statement format

When splitStatements is set to false in your changeset, avoid trailing semicolons on individual SQL statements.

With splitStatements: false, some JDBC drivers treat a trailing semicolon as an additional empty statement, which can cause migration failures. When splitStatements: true (the default), Liquibase uses the semicolon as the statement delimiter, so semicolons are required.

Include and includeAll tags

Liquibase provides two primary tags for referencing other changelog files in your root changelog: include and includeAll. The include tag allows you to include a single SQL file in your changelog:

While includeAll allows you to include multiple SQL files in your changelog:

Use includeAll for releases or environments where speed and scalability are prioritized, and use include when explicit control and maintainability are needed, especially in controlled isolated environments.

Changeset

Each changeset in a formatted SQL file begins with a comment of the form:

Go to What is a Changeset? to review the full changeset attribute reference.

Rollback actions

If you need to roll back a change, you can specify rollback actions directly in the changeset. These actions run when the changeset is rolled back.

A clear and consistent SQL file structure allows teams to manage database changes with properly formatted Liquibase SQL files. By following the best practices for directory organization, changeset formatting, spacing, and rollback definitions, you can ensure your changelogs are well-structured, maintainable, and CI/CD-ready.

Next steps

Last updated

Was this helpful?