Junior — Middle
How is database migration managed and automated using Liquibase?
sobes.tech AI
Answer from AI
Liquibase is a tool for version control and automation of database migrations. Migration management with Liquibase is carried out through a set of changes (change sets) described in XML, YAML, JSON, or SQL files, which record schema or data changes.
Main steps:
- Create a changelog file where you describe the changes (e.g., creating tables, adding columns).
- Each change is wrapped in a changeSet with a unique id and author.
- Liquibase stores a special table called DATABASECHANGELOG in the database, where it records applied changes to prevent reapplying them.
- To automate migration runs, you can integrate Liquibase into your build process (Maven, Gradle) or run it via the command line.
Example of a changeSet in XML:
<changeSet id="1" author="ivan">
<createTable tableName="users">
<column name="id" type="int" autoIncrement="true" primaryKey="true"/>
<column name="username" type="varchar(255)"/>
</createTable>
</changeSet>
When you run Liquibase, it will apply these changes to the database if they haven't been applied yet. This allows for controlling and automating migrations, ensuring schema consistency across different environments.