Repository navigation
feat: manage database schema with Liquibase #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8e897bd
feat: manage database schema with Liquibase
theaprince001 4a44a93
fix: address CodeRabbit findings - MySQL 8.4 auth and legacy schema u…
theaprince001 9af7a93
Merge branch 'master' into feat/liquibase-db-schema
theaprince001 7696f4f
docs: add migration guide; fix dev datasource and split migration cha…
theaprince001 c95464e
docs: simplify migration guide to match project architecture
theaprince001 aa44dcd
fix: address third-round CodeRabbit review
theaprince001 7d7320a
docs+prod: address CodeRabbit review
theaprince001 7d62780
refactor: remove speculative legacy posts.created_at backfill per mai…
theaprince001 01896b7
docs: remove stale 002a-* references from migration guide
theaprince001 9aa3170
docs: use single quotes for env exports to preserve special characters
theaprince001 6fc79f4
docs: add note on handling embedded single quotes in env vars
theaprince001 7ce86e3
docs: clarify .env quoting vs shell export quoting for passwords
theaprince001 8ef9459
docs: fix .env single quote escaping in migration guide
theaprince001 8c30f12
fix: secure MySQL development authentication
ohbus d50379d
fix: honor configured development database port
ohbus 02c4508
docs: clarify migration failure recovery
ohbus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # Database Migrations | ||
|
|
||
| Schema is managed by [Liquibase](https://www.liquibase.org/) via | ||
| [`src/main/resources/db/changelog/db.changelog-master.xml`](../src/main/resources/db/changelog/db.changelog-master.xml). | ||
| Hibernate runs with `ddl-auto=validate` — it never creates or modifies tables. | ||
| **Only Liquibase changes the schema.** | ||
|
|
||
| Liquibase applies pending changesets automatically when the application starts. | ||
| No manual migration step is required for local development or production — the | ||
| application applies migrations before Hibernate's `ddl-auto=validate` check runs. | ||
|
|
||
| ## Local development | ||
|
|
||
| Set up the environment file and start the database: | ||
|
|
||
| ```bash | ||
| # 1. Copy the example environment file | ||
| cp .env.example .env | ||
|
|
||
| # 2. Set nonempty values in .env for all required Compose variables | ||
| # (see docker-compose.base.yml for the full list). | ||
| # Wrap passwords in single quotes to prevent Compose from interpolating | ||
| # special characters (like $). For an apostrophe in a single-quoted | ||
| # value, escape it with a backslash (e.g., DB_ROOT_PASS='can\'t'). | ||
| # DB_PORT_HOST=3306 | ||
| # DB_NAME=twitter_clone | ||
| # DB_USER=moo_user | ||
| # DB_PASS='moo_pass' | ||
| # DB_ROOT_PASS='root_pass' | ||
| # JWT_SECRET='<at least 32 bytes of randomness>' | ||
|
|
||
| # 3. Bash / zsh: clear a stale shell override so Compose uses the .env port | ||
| unset DB_PORT_HOST | ||
|
|
||
| # 4. Start the database in detached mode so the command returns to the shell | ||
| docker compose up -d db | ||
| ``` | ||
|
|
||
| Shell variables take precedence over `.env`. If using PowerShell, run | ||
| `Remove-Item Env:DB_PORT_HOST -ErrorAction SilentlyContinue` before starting | ||
| Compose, or set `$env:DB_PORT_HOST` to the same value as `.env`. | ||
|
|
||
| Maven does not read `.env` — export every value the application needs. | ||
| The dev profile reads the database port from `${DB_PORT_HOST:3306}`, the | ||
| database name from `${DB_NAME:twitter_clone}`, and the root password from | ||
| `${DB_ROOT_PASS:root_pass}`. It also requires `JWT_SECRET` to be set (root | ||
| `application.properties` uses `${JWT_SECRET}` with no default): | ||
|
|
||
| ```bash | ||
| # bash / zsh — use the values from your .env file | ||
| # Use single quotes to preserve special characters (like $) in the password. | ||
| # If your password contains a single quote, close the quote, insert an escaped | ||
| # quote, and open a new quote (e.g., 'can'\''t'). | ||
| export DB_NAME='<value of DB_NAME from .env>' | ||
| export DB_PORT_HOST='<value of DB_PORT_HOST from .env>' | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| export DB_ROOT_PASS='<value of DB_ROOT_PASS from .env>' | ||
| export JWT_SECRET="$(openssl rand -hex 32)" | ||
| ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev | ||
| ``` | ||
|
|
||
| ```powershell | ||
| # PowerShell (Windows) — use the values from your .env file | ||
| # Use single quotes to preserve special characters (like $) in the password. | ||
| # If your password contains a single quote, double it (e.g., 'can''t'). | ||
| $env:DB_NAME = '<value of DB_NAME from .env>' | ||
| $env:DB_PORT_HOST = '<value of DB_PORT_HOST from .env>' | ||
| $env:DB_ROOT_PASS = '<value of DB_ROOT_PASS from .env>' | ||
| $env:JWT_SECRET = '<at least 32 bytes of randomness>' | ||
| ./mvnw spring-boot:run "-Dspring-boot.run.profiles=dev" | ||
| ``` | ||
|
|
||
| ## Production | ||
|
|
||
| Production applies pending changesets on application startup | ||
| (`spring.liquibase.enabled=true` in `application.properties`). What happens | ||
| when a migration fails depends on the deployment topology: | ||
|
|
||
| - **Password Quoting:** Docker Compose `.env` files and shell exports handle special characters differently. | ||
| Always single-quote values containing `$` in `.env` (e.g., `DB_ROOT_PASS='pa$word'`). | ||
| When exporting the same value in a shell, use the shell-specific single-quote escaping shown above. | ||
| If you use double quotes in a shell, the shell will expand `$`. | ||
| - **Rolling / blue-green deployments** (two app instances, traffic routed | ||
| between them): the previous instance keeps serving traffic while the new | ||
| instance applies migrations. If a migration fails, only the new instance is | ||
| affected, and the old one continues serving. | ||
| - **Single-container Compose** (this repository's default deployment): | ||
| `docker compose up` stops and recreates the app container when its image | ||
| changes. A migration failure at startup leaves no version serving traffic | ||
| until the failure is resolved and a working image is redeployed. | ||
|
|
||
| For zero-downtime migrations, use a multi-instance setup where the previous | ||
| version keeps running while the new version applies changes. | ||
|
|
||
| If a migration fails, inspect the application and Liquibase logs, the current | ||
| database schema, and `DATABASECHANGELOG`. Liquibase stops at the failed | ||
| changeset, so later corrections cannot run until the blocking failure is | ||
| resolved. Fix the reported database, data, configuration, or changelog problem, | ||
| then redeploy and verify that the failed changeset completes. | ||
|
|
||
| Rollback is **not a supported recovery path**. The current changelog does not | ||
| define `<rollback>` blocks, so `liquibase:rollback` can only reverse changesets | ||
| via Liquibase's auto-generated inverse operations — which for | ||
| `010-create-refresh-tokens-table` means dropping the table and all stored | ||
| refresh tokens. If a successfully applied migration needs to be reversed, add | ||
| a new forward-only changeset that compensates for it; do not edit the recorded | ||
| changeset. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.