Database Migrations
Managing schema changes across environments.
Overview
Database migrations are a way to manage changes to your database schema over time. They allow you to version-control your database structure and ensure that all environments (development, staging, production) are in sync. LibreApps Desktop uses Liquibase for this purpose.
How it Works
- Change Log: You define your database changes (tables, columns, indexes) in a YAML, XML, or JSON file called a "change log".
- Change Sets: Each individual change is wrapped in a "change set" with a unique ID and author.
- Execution: When the microservice starts up, Liquibase checks the
DATABASECHANGELOGtable to see which change sets have already been applied. - Update: Any new change sets are executed in order, and the
DATABASECHANGELOGtable is updated.
Example Change Set
databaseChangeLog:
- changeSet:
id: 1
author: jetstart
changes:
- createTable:
tableName: users
columns:
- column:
name: id
type: bigint
autoIncrement: true
constraints:
primaryKey: true
- column:
name: username
type: varchar(255)
constraints:
nullable: false
unique: true
Benefits
- Version Control: Keep your database schema in sync with your application code.
- Automation: Automatically apply schema changes during deployment.
- Rollback: Easily revert changes if something goes wrong.
- Consistency: Ensure that all developers and environments are using the same database structure.
Best Practices
- ✅ Do this: Create a new change set for every database change.
- ✅ Do this: Use descriptive IDs and comments for your change sets.
- ❌ Don't do this: Manually modify the database schema in production; always use migrations.