Java.DBMigrationTools.What is the purpose of include and includeAll in changelogs?

Short Answer

  • **<include>** lets you include one specific changelog file into another
  • **<includeAll>** lets you include all changelogs from a folder, automatically

This helps you modularize and organize your migrations across many files..

🔎 Detailed Explanation

🔹 include

Used to include a single specific file inside another changelog:

<include file="tables/users.xml" relativeToChangelogFile="true"/>

✅ Use this when you want explicit control over the load order
✅ Often used in a master changelog that lists all migration files

🔹 includeAll

Used to include all changelogs in a directory, in sorted order:

<includeAll path="tables/" relativeToChangelogFile="true"/>

✅ Includes all changelogs from the tables/ directory
✅ Files are loaded in alphabetical order
✅ Convenient when managing many small changelogs

🧠 Use Cases

Use CaseUse include or includeAll
Want full control over migration orderinclude
Want to auto-include all scripts in a folderincludeAll
Team adds changelogs frequently in parallelincludeAll for flexibility
Production migrations in strict orderinclude for safety

⚠️ Caution with includeAll

  • ❗ Ordering is by file name, so use naming like V001_init.xml, V002_add_index.xml
  • ❗ May re-include deleted files if you don’t clean up unused changelogs

📌 Key Takeaways

✅ Use <include> to add one changelog file, with manual order control
✅ Use <includeAll> to add all changelogs from a directory, automatically sorted
✅ Modularization improves maintainability, especially in large projects

Posted in Без рубрики | Leave a comment

Java.DBMigrationTools.How do you apply a Liquibase migration from the command line?

Short Answer

You apply a Liquibase migration using the update command from the CLI:

liquibase update

This command tells Liquibase to:

  • Connect to your database
  • Read the changelog file
  • Apply any changesets that haven’t been run yet

🔎 Step-by-Step Command Line Usage

1. ✅ Basic Example

liquibase \
  --changeLogFile=db/changelog.xml \
  --url=jdbc:postgresql://localhost:5432/mydb \
  --username=myuser \
  --password=mypass \
  update

This runs all unapplied changesets in changelog.xml.

2. ✅ Set Configuration Once (Optional)

Create a liquibase.properties file to avoid typing all parameters every time:

changeLogFile=db/changelog.xml
url=jdbc:postgresql://localhost:5432/mydb
username=myuser
password=mypass
driver=org.postgresql.Driver

Then you can run:

liquibase update

Other Useful CLI Commands

CommandDescription
updateApply pending changesets
updateCount 1Apply only the next 1 changeset
updateSQLShow the SQL that would be executed
statusShow which changesets have not been applied
rollbackCount 1Roll back the last 1 changeset
tag my-release-v1Mark current DB state with a version tag
historyShow execution history of applied changes
clearCheckSumsClear checksums if you edited a changeset

🧠 Behind the scenes

  • Liquibase checks the DATABASECHANGELOG table
  • Compares changesets against what’s already been applied
  • Runs only new changesets (unless runAlways or runOnChange is used)

📌 Key Takeaways

✅ Use the update command to apply migrations via the command line
✅ Prefer using a liquibase.properties file for reusability
✅ Useful for local dev, automation, or CI/CD pipelines
✅ Track progress and debug with commands like status, history, and updateSQL

Posted in Без рубрики | Leave a comment

Java.DBMigrationTools.Can Liquibase generate a changelog from an existing database?

Short Answer

Yes, Liquibase can generate a changelog from an existing database schema using the generateChangeLog command.
This reverse-engineers the current database structure into a changelog file (XML, YAML, JSON, or SQL) so you can start tracking and versioning it with Liquibase.


🔎 How It Works

You run:

liquibase generateChangeLog \
  --url=jdbc:your_database_url \
  --username=your_user \
  --password=your_pass \
  --changeLogFile=initial-changelog.xml

🔹 Liquibase connects to your database
🔹 Reads the current schema (tables, columns, constraints, indexes, sequences…)
🔹 Writes them out as a Liquibase changelog

You can specify:

  • --changeLogFile → path to write the changelog
  • --format → output format: XML (default), YAML, JSON, or SQL

🧪 Example Output (XML)

<changeSet id="1" author="liquibase">
  <createTable tableName="users">
    <column name="id" type="BIGINT" autoIncrement="true"/>
    <column name="email" type="VARCHAR(255)"/>
  </createTable>
</changeSet>

⚠️ Things to Keep in Mind

  • ✅ Great for bootstrapping Liquibase into an existing project
  • ❌ Not ideal for ongoing migrations — generated changelogs may be large and unstructured
  • ✅ After generation, you should clean up, organize, and commit to version control

📌 Key Takeaways

✅ Liquibase can generate a changelog from your existing DB schema using generateChangeLog
✅ This is ideal for initial setup or reverse-engineering legacy databases
✅ You can output in XML, YAML, JSON, or SQL formats and customize the scope

Posted in Без рубрики | Leave a comment

Java.DBMigrationTools.How does Liquibase track which changes were applied?

Short Answer

Liquibase tracks applied changes by recording each successfully executed changeset in a special database table called DATABASECHANGELOG.
Each entry includes metadata like id, author, filename, checksum, and execution timestamp.


🔎 Detailed Explanation

When you run liquibase update, Liquibase:

  1. Reads your changelog file(s)
  2. For each <changeSet>:
    • Checks if it’s already listed in the DATABASECHANGELOG table
    • Compares the checksum to detect changes
  3. Executes the changeset only if it hasn’t been run yet, or if you’ve specified runAlways or runOnChange
  4. Records a new row in DATABASECHANGELOG after successful execution

📋 Key Columns in DATABASECHANGELOG

ColumnPurpose
IDThe unique ID from the changeset
AUTHORAuthor name from the changeset
FILENAMEChangelog file name where the changeset is defined
DATEEXECUTEDTimestamp when this changeset was run
ORDEREXECUTEDOrder of execution
MD5SUM (checksum)Verifies if the changeset content has been modified
EXECTYPEEXECUTED, FAILED, SKIPPED, etc.
DESCRIPTIONDescription of the change (optional)

🧠 Why is this important?

  • ✅ Ensures each changeset runs exactly once
  • ✅ Supports safe retries — rerunning update won’t re-apply executed changesets
  • ✅ Detects unauthorized or accidental modifications via checksum mismatch
  • ✅ Enables rollback, tagging, auditing, and repeatable deployments

🔄 Related Concepts

  • Checksum mismatch → means the changeset was edited after execution
  • clearCheckSums command → resets checksums if you deliberately modified a changeset and want Liquibase to accept the new version
  • runOnChange="true" → Liquibase re-applies the changeset if the checksum changes (use with caution)

📌 Key Takeaways

✅ Liquibase uses the DATABASECHANGELOG table to track exactly which changesets were applied, when, and in what order.
✅ This mechanism ensures idempotency, auditability, and integrity of schema changes across environments.
✅ You never need to manually track what’s been applied — Liquibase does it all.

Posted in Без рубрики | Leave a comment

Java.DBMigrationTools.What does runOnChange do?

Short Answer

runOnChange="true" tells Liquibase to re-run a changeset if its definition changes, even if it was already applied.

By default, Liquibase only runs each changeset once, but runOnChange forces Liquibase to track changes via checksums and re-apply the changeset if it detects any modifications.


🔎 Detailed Explanation

🔸 Without runOnChange

Liquibase stores a checksum for each applied changeset in the DATABASECHANGELOG table.

If the changeset is later modified, Liquibase will:

  • ❌ Detect a checksum mismatch
  • ❌ Throw a validation error
  • ✅ Require you to fix it manually or use clearCheckSums

This behavior prevents untracked schema drift.


🔸 With runOnChange="true"

If a changeset has:

<changeSet id="1" author="alice" runOnChange="true">
  <createView viewName="user_view" ... />
</changeSet>

Liquibase will:

  • Detect a checksum change
  • Re-execute the changeset automatically
  • ✅ Update the DATABASECHANGELOG record with the new checksum

🧠 Common Use Cases

Use CaseUse runOnChange?
Creating or modifying a view✅ Yes
Changing a stored procedure or trigger✅ Yes
Adding a comment or default value✅ Yes
Creating tables/columns❌ Usually no

⚠️ Caution

Use runOnChange carefully:

  • It re-applies changes each time the checksum changes — which can overwrite custom manual edits in prod
  • Not recommended for large data operations (e.g. insert or update)

📌 Key Takeaways

runOnChange="true" re-executes a changeset if it has been modified after execution
✅ Useful for views, procedures, and other objects that evolve over time
❌ Should be used with caution — especially in production environments

Posted in Без рубрики | Leave a comment

Java.DBMigrationTools.What are preconditions in Liquibase?

Short Answer

Preconditions in Liquibase are conditions that must be true before a changeset or changelog is applied.
If the precondition fails, Liquibase can be told to halt, warn, or continue, depending on your configuration.


🔎 Detailed Explanation

A precondition acts like a safeguard — for example:

  • “Only run this change if a table exists”
  • “Skip this if a column is already present”
  • “Apply this only on PostgreSQL”

You can define preconditions:

  • At the changelog level (applies to all changesets)
  • At the changeset level (applies to one changeset only)

Common Use Cases for Preconditions

Use CasePrecondition Type
Ensure a table existstableExists
Ensure a column doesn’t already existnot, columnExists
Restrict to a DBMS (e.g., PostgreSQL)dbms
Block updates on productionrunningAs (based on user)

🧪 Example – XML format

<changeSet id="1" author="alice">
  <preConditions onFail="HALT">
    <not>
      <tableExists tableName="users"/>
    </not>
  </preConditions>

  <createTable tableName="users">
    <column name="id" type="int" autoIncrement="true"/>
    <column name="username" type="varchar(255)"/>
  </createTable>
</changeSet>

✅ This change will only run if the users table does not exist.
If the table does exist, Liquibase will halt the migration (because onFail="HALT").

🛠️ Other Precondition Types

  • sqlCheck – Run custom SQL that returns true/false
  • changeLogPropertyDefined
  • columnExists
  • sequenceExists
  • foreignKeyConstraintExists
  • customPrecondition – write your own in Java

📌 Key Takeaways

Preconditions let you add logic before a changeset runs, preventing errors or unintended changes.
✅ You can halt, warn, or skip based on the result.
✅ They are useful for idempotency, environment targeting, and fail-safety.

Posted in Без рубрики | Leave a comment

Java.DBMigrationTools.What is a DATABASECHANGELOG table?

Short Answer

The DATABASECHANGELOG table is a system table created and maintained by Liquibase.
It tracks which changesets have already been executed, preventing duplicates and ensuring safe, repeatable database migrations.


🔎 Detailed Explanation

Whenever Liquibase applies a changeset from your changelog file, it logs a record in the DATABASECHANGELOG table. This allows Liquibase to:

  • Skip already executed changesets
  • Validate checksums (to detect tampering)
  • Maintain the execution order
  • Enable rollbacks and tagging

📋 Key Columns in DATABASECHANGELOG

Column NameDescription
IDUnique ID of the changeset (from your changelog)
AUTHORThe name of the developer or tool that wrote the changeset
FILENAMEThe changelog file the changeset came from
DATEEXECUTEDTimestamp when the changeset was executed
ORDEREXECUTEDSequence number of execution
MD5SUMChecksum to detect changes to an already-applied changeset
EXECTYPEResult: EXECUTED, FAILED, SKIPPED, etc.
TAGOptional tag label for schema versioning

🧠 Why Is This Table Important?

  • ✅ Prevents running the same changes twice
  • ✅ Ensures safe migrations across environments (dev, test, prod)
  • ✅ Enables rollback and versioning
  • ✅ Supports audit and compliance tracking

📌 Key Takeaways

DATABASECHANGELOG is the core mechanism Liquibase uses to track schema changes.
✅ It ensures that each changeset runs exactly once, safely and in order.
✅ It is stored inside your target database, alongside your actual tables.

Posted in Без рубрики | Leave a comment

Java.DBMigrationTools.Name some file formats Liquibase supports.

Short Answer

Liquibase supports the following changelog file formats:

  1. XML
  2. YAML
  3. JSON
  4. Formatted SQL

🔎 Details

FormatDescriptionCommon Use Case
XMLMost powerful and fully featuredTraditional projects, fine-grained control
YAMLMore human-readable, conciseModern teams, cleaner syntax
JSONGood for machine-generated changelogsTools or scripted migrations
SQLUses special comments for Liquibase directivesDevelopers who prefer raw SQL

📝 Example file names:

  • changelog.xml
  • db-changelog.yaml
  • changelog.json
  • changelog.sql (with --liquibase formatted sql at the top)

📌 Key Takeaways

✅ Liquibase supports XML, YAML, JSON, and SQL as changelog formats.
✅ Choose based on team preference, tooling, and readability needs.
✅ All formats support the same core features like changesets, rollback, and tagging.

Posted in Без рубрики | Leave a comment

Java.DBMigrationTools.Can database changes be rolled back?

Short Answer

Yes, database changes can be rolled back — but only under specific conditions:

  • In a transaction → changes can be rolled back automatically on failure.
  • Most schema changes (DDL) like CREATE TABLE are not transactional in many databases.
  • ✅ Migration tools like Liquibase can rollback if you explicitly define rollback logic.
  • ❌ Tools like Flyway do not rollback — you must undo changes manually.

🔎 1. Database Transaction Rollback

✅ If you’re performing DML operations (insert, update, delete) in a transaction:

BEGIN;
UPDATE users SET email = 'test@example.com' WHERE id = 1;
ROLLBACK; -- Undoes the update

✅ Works in most RDBMS (PostgreSQL, MySQL with InnoDB, etc.)

❌ But many DDL statements (like ALTER TABLE, CREATE TABLE) are auto-committed:

  • PostgreSQL supports transactional DDL
  • MySQL/MariaDB does not (DDL can’t be rolled back)
  • Oracle supports transactional DDL in some cases
  • SQLite does support it

🔎 2. Liquibase Rollback

✅ You can define explicit rollback in changesets:

<changeSet id="3" author="alice">
  <addColumn tableName="users">
    <column name="phone" type="varchar(20)"/>
  </addColumn>
  <rollback>
    <dropColumn tableName="users" columnName="phone"/>
  </rollback>
</changeSet>

Then run

liquibase rollbackCount 1

✅ Liquibase will run the rollback logic to revert changes.

❌ But this is manual — rollback only happens when you run it.
❌ There’s no automatic rollback on error during a migration.

🔎 3. Flyway Rollback

❌ Flyway has no rollback feature.

✅ If a migration fails:

  • The script is marked as failed
  • You must fix manually
  • Optionally write an “undo” migration (U1__undo_users.sql), but Flyway won’t run it automatically

📌 Key Takeaways

ContextRollback possible?How?
SQL transactions✅ For DML (and some DDL)Use BEGIN; ... ROLLBACK;
Liquibase✅ If definedUse <rollback> blocks + rollback commands
Flyway❌ Not supported nativelyManual repair or versioning
DDL (e.g. CREATE)❌ In most DBsUse tool-level rollback or backups
Posted in Без рубрики | Leave a comment

Java.DBMigrationTools.What happens when a migration fails?

Short Answer

When a migration fails:

  • The migration process stops immediately
  • The failed changeset is not marked as executed
  • Any partial changes already applied by that changeset remain in the database unless you handle rollback manually

❗ No automatic rollback is performed unless you explicitly define and run one.


🔎 Liquibase – What happens on failure?

🔹 Suppose you run:

liquibase update

🔹 Scenario:

One of your changesets (e.g. id=3) has a syntax error or constraint violation.

🔸 What happens:

  • Liquibase stops at the error.
  • That changeset is not marked as executed in DATABASECHANGELOG
  • Previous changesets remain applied
  • Partial SQL from the failed changeset may have affected the DB, depending on DB settings and the operation

✅ You can:

  • Fix the issue
  • Re-run liquibase update (it will skip changesets already marked as executed)

🔹 Example:

<changeSet id="1" author="alice">
  <createTable tableName="users">
    <column name="id" type="bigint" autoIncrement="true"/>
  </createTable>
</changeSet>

<changeSet id="2" author="alice">
  <addColumn tableName="users">
    <column name="email" type="varchar(255)"/>
  </addColumn>
</changeSet>

<changeSet id="3" author="alice">
  <!-- This fails due to bad SQL -->
  <sql>ALTER TABLE users ADD age_in_days_typo INT</sql>
</changeSet>

✅ Changeset 3 fails
✅ Changesets 1 and 2 are marked as executed
❌ 3 is not
❗ You must fix the SQL, then re-run

🔹 Rollback?

Liquibase does not rollback automatically on failure.
You can:

  • Define a <rollback> section per changeset
  • Manually run:
liquibase rollbackCount 1

But rollback must be explicitly triggered, not automatic.

🔎 Flyway – What happens on failure?

In Flyway:

  • The failed script is recorded with a failure status in flyway_schema_history
  • The entire migration run is stopped
  • You must repair or fix the problem before continuing

✅ Fix the file or write a new script
✅ Then run:

flyway repair  # marks the failed migration as pending or removes bad checksum
flyway migrate

Flyway does not support rollback, so you must clean the database (dev only) or manually fix the schema.


📌 Key Takeaways

ToolOn FailureRollback?Recovery
LiquibaseStops, logs error❌ Manual (unless scripted)Fix issue → rerun update
FlywayStops, logs failure❌ Not supported nativelyFix + repair + re-run
Posted in Без рубрики | Leave a comment