You can configure Liquibase to avoid changing the database schema in several ways, depending on what you're trying to achieve. Here are a few common scenarios and how to handle them:
1. Running Liquibase to only check the current state (status, future rollback, etc.) without applying changes:
-
liquibase status: This command reports whether the database is up-to-date with the changelog without attempting to apply any unrun changesets. It's a read-only operation.
liquibase --changeLogFile=path/to/changelog.xml --url="jdbc:..." --username=... --password=... status
-
liquibase future-rollback-sql: This command generates the SQL required to rollback the database to a specific point without actually executing the SQL. This is useful for planning and review.
liquibase --changeLogFile=path/to/changelog.xml --url="jdbc:..." --username=... --password=... future-rollback-sql --tag=your_tag > rollback.sql
-
liquibase diff-changelog (to compare schemas): This is great for understanding the potential changes Liquibase would make. It doesn't modify the database; it just shows you the differences between your database and a reference database or data model.
liquibase --changeLogFile=path/to/changelog.xml --url="jdbc:..." --username=... --password=... diff-changelog --referenceUrl="jdbc:..." --referenceUsername=... --referencePassword=...
2. Filtering Changesets:
You can use Liquibase's filtering capabilities to control which changesets are run. This is useful if your changelog contains changes you don't want to apply to a specific environment. Here are some options:
-
--contexts: Changesets can be tagged with context attributes. You can then use the --contexts flag to only run changesets that have a matching context. If a changeset doesn't have a context attribute, it always runs.
<changeSet id="1" author="me" context="production">
<createTable tableName="my_table">
<column name="id" type="INT" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
<column name="name" type="VARCHAR(255)"/>
</createTable>
</changeSet>
<changeSet id="2" author="me" context="development">
<addColumn tableName="my_table">
<column name="description" type="VARCHAR(1000)"/>
</addColumn>
</changeSet>
liquibase --changeLogFile=path/to/changelog.xml --url="jdbc:..." --username=... --password=... update --contexts=production
In this example, only the first changeset (creating my_table) would be applied if you specify --contexts=production.
-
--labels: Similar to contexts, but can be used for more fine-grained control.
<changeSet id="1" author="me" labels="bugfix,urgent">
<update tableName="my_table">
<column name="status" value="fixed"/>
<where>id = 123</where>
</update>
</changeSet>
liquibase --changeLogFile=path/to/changelog.xml --url="jdbc:..." --username=... --password=... update --labels="bugfix"
-
--includeObjects / --excludeObjects: These options (available in some Liquibase editions, such as Liquibase Pro) allow you to specify which database objects to include or exclude from updates. This is highly specific.
3. Using preConditions and validCheckSum:
-
preConditions: You can add <preConditions> to your changesets to prevent them from running if certain conditions are not met. For example, you could check if a table already exists or if a certain column has a specific type. If the pre-condition fails, the changeset does not run.
<changeSet id="3" author="me">
<preConditions onFail="MARK_RAN"> <!-- OR 'HALT', 'CONTINUE', 'WARN' -->
<not>
<tableExists tableName="my_table"/>
</not>
</preConditions>
<createTable tableName="my_table">
<column name="id" type="INT" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
<column name="name" type="VARCHAR(255)"/>
</createTable>
</changeSet>
In this example, the createTable changeset will only run if the table my_table does not already exist. The onFail="MARK_RAN" attribute tells Liquibase to mark the changeset as run even if the precondition fails (so it won't be retried). Other options for onFail are HALT (stop execution), CONTINUE (continue to the next changeset), and WARN (issue a warning and continue).
-
validCheckSum: Liquibase calculates a checksum for each changeset and stores it in the DATABASECHANGELOG table. If you modify a changeset that has already been run, Liquibase will detect a checksum mismatch and, by default, halt execution. You can use the --ignoreCheckSum option to bypass this check (but be very careful when doing so, as it can lead to database inconsistencies). The proper way to handle changes to run changesets is to not change them. Instead, create a new changeset to modify what the old changeset created. This helps maintain a clear history of your database changes.
4. Read-Only User:
You can configure Liquibase to use a database user with only SELECT privileges. This will prevent Liquibase from making any changes to the schema, but it will still be able to read the DATABASECHANGELOG table and report the current status. This is mostly useful for running status, diff-changelog, etc.
5. Using a Separate Schema/Catalog:
Liquibase can be configured to manage changes in a specific schema or catalog. This allows you to isolate your Liquibase changes from other parts of your database. Use the --defaultSchemaName and --defaultCatalogName parameters. Ensure your JDBC connection string and Liquibase configuration point to the correct schema/catalog.
Important Considerations:
-
DATABASECHANGELOG Table: Liquibase always needs to write to the DATABASECHANGELOG table. This is how it tracks which changesets have been applied. Therefore, it needs at least CREATE TABLE and INSERT privileges on the schema where the DATABASECHANGELOG table resides (which can be different from the schema you're modifying).
-
Testing: Always test your Liquibase configurations in a non-production environment before applying them to production. This will help you identify and resolve any issues before they affect your live data.
-
Changelog Design: A well-designed changelog is crucial for managing database changes effectively. Keep your changesets small and focused, and use meaningful descriptions.
-
Rollback Strategy: Plan your rollback strategy carefully. Liquibase can automatically generate rollback SQL for many types of changes, but it's always a good idea to review the generated SQL and ensure it will correctly revert your changes.
In summary, the "best" way to prevent Liquibase from changing the schema depends on your specific needs. If you just want to check the status, use liquibase status. If you want to control which changesets are run, use --contexts or --labels. If you want to prevent changesets from running under certain conditions, use preConditions. Careful planning and testing are essential for successful database migrations with Liquibase.