Skip to content

Cum să generezi changelog din diferența între baze de date cu Liquibase

DodaTech Updated 2025-01-15 1 min read

In this tutorial, you'll learn about Cum să generezi changelog din diferența între baze de date cu Liquibase. We cover key concepts, practical examples, and best practices.

Comanda liquibase diffChangelog generează un changelog Liquibase care conține changeset-urile necesare pentru a aduce o bază de date la starea alteia.

Problema

Scrierea manuală a changelog-urilor pentru a sincroniza două baze de date consumă mult timp. diffChangelog automatizează acest proces, generând changeset-uri gata de utilizare.

Comanda de bază

liquibase diffChangelog \
  --referenceUrl=jdbc:postgresql://localhost:5432/baza_sursa \
  --referenceUsername=user \
  --referencePassword=pass \
  --url=jdbc:postgresql://localhost:5432/baza_destinatie \
  --username=user \
  --password=pass \
  --changelogFile=changelog-diff.xml

Output:

Diff changelog generated: changelog-diff.xml
Changesets: 5
  - createTable: users
  - addColumn: email
  - addForeignKeyConstraint: fk_orders_users

Exemplu de changelog generat

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog">
  <changeSet author="diff-generated" id="1715000000-1">
    <createTable tableName="users">
      <column name="id" type="BIGINT" autoIncrement="true">
        <constraints primaryKey="true" nullable="false"/>
      </column>
      <column name="email" type="VARCHAR(255)"/>
    </createTable>
  </changeSet>
</databaseChangeLog>

Formate suportate

# XML (implicit)
liquibase diffChangelog --changelogFile=diff.xml

# YAML
liquibase diffChangelog --changelogFile=diff.yaml

# JSON
liquibase diffChangelog --changelogFile=diff.json

Erori frecvente

  1. Fișierul changelog deja există — folosește o altă cale sau șterge fișierul existent.
  2. Diferențe prea mari — pentru baze foarte diferite, generează changelog-ul pe părți.
  3. Ordinea changeset-urilor — verifică ordinea generată, mai ales pentru foreign key.

FAQ

### diffChangelog vs diff — care e diferența?

diff arată diferențele în consolă, iar diffChangelog generează un changelog executabil.

Pot folosi changelog-ul generat direct în producție?

Este recomandat să revizuiești și să testezi changelog-ul înainte de a-l rula în producție, mai ales pentru operațiuni distructive.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tools integrate seamlessly with Liquibase for enhanced productivity and security.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro