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.
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: trueLiquibase 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.
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.
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:
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.
Caution
When using SQL format for your changelog files, rollback actions are required. Go to Automatic and custom rollbacks to understand rollback strategies and implementation options.
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
Go to What is a changelog? to understand the changelog file format and how changesets are ordered.
Go to What is a context? to learn how to control which changesets apply to which environments.
Go to Automatic and custom rollbacks to configure rollback strategies for your SQL files.
Last updated
Was this helpful?