From 8e897bda7f993c7eac01fb459d72732474dfcfcf Mon Sep 17 00:00:00 2001 From: Theaprince001 Date: Fri, 6 Nov 2026 14:32:56 +0530 Subject: [PATCH 01/15] feat: manage database schema with Liquibase --- docker-compose.base.yml | 2 +- pom.xml | 5 + .../resources/application-prod.properties | 5 +- src/main/resources/application.properties | 10 + .../db/changelog/001-initial-schema.xml | 275 ++++++++++++++++++ .../db/changelog/db.changelog-master.xml | 10 + 6 files changed, 303 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/db/changelog/001-initial-schema.xml create mode 100644 src/main/resources/db/changelog/db.changelog-master.xml diff --git a/docker-compose.base.yml b/docker-compose.base.yml index de424fd..fc87242 100644 --- a/docker-compose.base.yml +++ b/docker-compose.base.yml @@ -24,7 +24,7 @@ services: db: image: mysql:8.4 container_name: moo-db - command: --default-authentication-plugin=mysql_native_password +# command: --default-authentication-plugin=mysql_native_password environment: MYSQL_DATABASE: ${DB_NAME:?DB_NAME is required} MYSQL_USER: ${DB_USER:?DB_USER is required} diff --git a/pom.xml b/pom.xml index 38f25dc..349c672 100644 --- a/pom.xml +++ b/pom.xml @@ -183,6 +183,11 @@ springdoc-openapi-starter-webmvc-ui 3.1.1 + + + org.springframework.boot + spring-boot-starter-liquibase + diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index e9f46a5..3f79fae 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -28,10 +28,7 @@ # =========== DATA SOURCE ============= # ===================================== -# Set here configurations for the database connection spring.datasource.url=jdbc:mysql://${MYSQL_DB_HOST}:${MYSQL_DB_PORT}/${MYSQL_DB_NAME}?createDatabaseIfNotExist=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Kolkata&useSSL=false - -# Username and secret spring.datasource.username=${MYSQL_DB_UNAME} spring.datasource.password=${MYSQL_DB_PASSWD} @@ -58,3 +55,5 @@ spring.jpa.properties.hibernate.id.new_generator_mappings=false springdoc.api-docs.enabled=false springdoc.swagger-ui.enabled=false +spring.liquibase.enabled=true +spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 2b330c3..e43e586 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -35,3 +35,13 @@ spring.data.web.pageable.default-page-size=20 spring.data.web.pageable.max-page-size=100 spring.jpa.properties.hibernate.default_batch_fetch_size=50 spring.jpa.open-in-view=false + +# Enable Liquibase +spring.liquibase.enabled=true + +# Specify the path to the master changelog file +spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml + +# IMPORTANT: Disable Hibernate's automatic table creation +# This prevents conflicts. Liquibase is now in charge of the schema. +spring.jpa.hibernate.ddl-auto=validate diff --git a/src/main/resources/db/changelog/001-initial-schema.xml b/src/main/resources/db/changelog/001-initial-schema.xml new file mode 100644 index 0000000..4e00e3a --- /dev/null +++ b/src/main/resources/db/changelog/001-initial-schema.xml @@ -0,0 +1,275 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/db/changelog/db.changelog-master.xml b/src/main/resources/db/changelog/db.changelog-master.xml new file mode 100644 index 0000000..29ed828 --- /dev/null +++ b/src/main/resources/db/changelog/db.changelog-master.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file From 4a44a9384b1d36b59a1d8123ad690551f567c57b Mon Sep 17 00:00:00 2001 From: Theaprince001 Date: Tue, 10 Nov 2026 12:39:23 +0530 Subject: [PATCH 02/15] fix: address CodeRabbit findings - MySQL 8.4 auth and legacy schema upgrade --- src/main/resources/application-dev.properties | 5 ++--- .../db/changelog/001-initial-schema.xml | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 9b9d6bf..d97efaf 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -29,8 +29,7 @@ # ===================================== # Set here configurations for the database connection -spring.datasource.url=jdbc:mysql://localhost:3306/twitter?createDatabaseIfNotExist=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Kolkata&useSSL=false - +spring.datasource.url=jdbc:mysql://localhost:3306/moo?allowPublicKeyRetrieval=true&useSSL=false # Username and secret spring.datasource.username=root spring.datasource.password=root @@ -59,7 +58,7 @@ spring.jpa.properties.hibernate.format_sql=true # schema will be automatically updated accordingly to java entities found in # the project spring.jpa.generate-ddl=true -spring.jpa.hibernate.ddl-auto=update +spring.jpa.hibernate.ddl-auto=validate # Allows Hibernate to generate SQL optimized for a particular DBMS spring.jpa.properties.hibernate.id.new_generator_mappings=false diff --git a/src/main/resources/db/changelog/001-initial-schema.xml b/src/main/resources/db/changelog/001-initial-schema.xml index 4e00e3a..25a9779 100644 --- a/src/main/resources/db/changelog/001-initial-schema.xml +++ b/src/main/resources/db/changelog/001-initial-schema.xml @@ -59,6 +59,27 @@ + + + + + + + + + + + + + + + UPDATE posts + SET created_at = COALESCE(`timestamp`, CURRENT_TIMESTAMP(6)) + WHERE created_at IS NULL; + + + + From 7696f4f1d943ddf0a56c1a68a72db1a40228c01f Mon Sep 17 00:00:00 2001 From: Theaprince001 Date: Tue, 10 Nov 2026 13:25:09 +0530 Subject: [PATCH 03/15] docs: add migration guide; fix dev datasource and split migration changesets --- docs/migrations.md | 106 +++++++++++++ src/main/resources/application-dev.properties | 8 +- .../db/changelog/001-initial-schema.xml | 139 ++++++++++++++---- 3 files changed, 219 insertions(+), 34 deletions(-) create mode 100644 docs/migrations.md diff --git a/docs/migrations.md b/docs/migrations.md new file mode 100644 index 0000000..9d7502b --- /dev/null +++ b/docs/migrations.md @@ -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.** + +## Local development + +Migrations run automatically when the app starts: + +```bash +# Start the database +docker compose up db + +# Run the app — Liquibase applies pending changesets automatically +./mvnw spring-boot:run -Dspring-boot.run.profiles=dev +``` + +The dev profile reads the database URL from `${DB_NAME:twitter_clone}` and the +root password from `${DB_ROOT_PASS:root_pass}`. The defaults match what +`docker compose up db` creates, so no environment variables are required for +the standard setup. + +To use a different database or password, export the matching variable before +starting the app: + +```bash +# bash / zsh +export DB_NAME=moo +export DB_ROOT_PASS=my_root_pass +./mvnw spring-boot:run -Dspring-boot.run.profiles=dev +``` + +```powershell +# PowerShell (Windows) +$env:DB_NAME="moo" +$env:DB_ROOT_PASS="my_root_pass" +./mvnw spring-boot:run "-Dspring-boot.run.profiles=dev" +``` + +## Applying migrations manually + +Useful for CI pipelines, scripted deploys, or previewing changes before an +app start: + +```bash +./mvnw liquibase:update \ + -Dliquibase.url="jdbc:mysql://localhost:3306/twitter_clone" \ + -Dliquibase.username=root \ + -Dliquibase.password=root_pass + +# Apply only the next N changesets (useful for staged rollouts) +./mvnw liquibase:update-count -Dliquibase.count=1 \ + -Dliquibase.url="jdbc:mysql://localhost:3306/twitter_clone" \ + -Dliquibase.username=root \ + -Dliquibase.password=root_pass +``` + +## Baselining an existing (pre-Liquibase) database + +If a database was created by an older `ddl-auto=update` version that predates +this Liquibase setup, baseline it before running migrations. Baselining marks +the existing changesets as already applied without executing them, so +Liquibase knows where to start applying future changes. + +```bash +# Mark all existing changesets as already applied without executing them +./mvnw liquibase:changelog-sync \ + -Dliquibase.url="jdbc:mysql://localhost:3306/legacy_db" \ + -Dliquibase.username=root \ + -Dliquibase.password=root_pass +``` + +Prefer in-place migration: changesets `002a-add-posts-created-at-column`, +`002a-backfill-posts-created-at`, and `002a-add-posts-created-at-not-null` +provide a safe, retry-friendly migration path for older schemas that have +`posts.timestamp` but not `posts.created_at`. If you prefer to baseline +instead of migrate, run the backfill manually: + +```sql +UPDATE posts SET created_at = `timestamp` WHERE created_at IS NULL; +ALTER TABLE posts MODIFY created_at DATETIME(6) NOT NULL; +``` + +## Production + +Production applies pending changesets on startup +(`spring.liquibase.enabled=true`). For zero-downtime deployments, run +migrations as a pre-deploy step before rolling out the new app version, +so failures are caught before any new pods receive traffic: + +```bash +./mvnw liquibase:update \ + -Dliquibase.url="${MYSQL_DB_URL}" \ + -Dliquibase.username="${MYSQL_DB_UNAME}" \ + -Dliquibase.password="${MYSQL_DB_PASSWD}" +``` + +Then roll out the new app version. If a migration needs to be rolled back, +use `liquibase:rollback -Dliquibase.rollbackCount=1` with the same connection +parameters. + +See +[`db.changelog-master.xml`](../src/main/resources/db/changelog/db.changelog-master.xml) +for the ordered list of changesets. \ No newline at end of file diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index d97efaf..2650b7c 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -29,10 +29,13 @@ # ===================================== # Set here configurations for the database connection -spring.datasource.url=jdbc:mysql://localhost:3306/moo?allowPublicKeyRetrieval=true&useSSL=false +spring.datasource.url=jdbc:mysql://localhost:3306/${DB_NAME:twitter_clone}?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Kolkata + # Username and secret spring.datasource.username=root -spring.datasource.password=root +spring.datasource.password=${DB_ROOT_PASS:root_pass} + + # Keep the connection alive if idle for a long time (needed in production) spring.datasource.testWhileIdle=true @@ -63,7 +66,6 @@ spring.jpa.hibernate.ddl-auto=validate # Allows Hibernate to generate SQL optimized for a particular DBMS spring.jpa.properties.hibernate.id.new_generator_mappings=false - # ==================================== # ====== CHANGE SERVER PORT HERE ===== # ==================================== diff --git a/src/main/resources/db/changelog/001-initial-schema.xml b/src/main/resources/db/changelog/001-initial-schema.xml index 25a9779..3f348fe 100644 --- a/src/main/resources/db/changelog/001-initial-schema.xml +++ b/src/main/resources/db/changelog/001-initial-schema.xml @@ -4,7 +4,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> - + @@ -24,7 +24,7 @@ - + @@ -33,7 +33,7 @@ - + @@ -42,7 +42,7 @@ - + @@ -59,11 +59,27 @@ - + + - @@ -72,15 +88,42 @@ + + + + + + + + + + UPDATE posts SET created_at = COALESCE(`timestamp`, CURRENT_TIMESTAMP(6)) WHERE created_at IS NULL; - - + + + + + + + SELECT COUNT(*) FROM information_schema.COLUMNS + WHERE UPPER(TABLE_NAME) = 'POSTS' + AND UPPER(COLUMN_NAME) = 'CREATED_AT' + AND UPPER(IS_NULLABLE) = 'YES' + + + + + + + @@ -91,7 +134,7 @@ - + @@ -100,7 +143,7 @@ - + @@ -110,7 +153,7 @@ - + @@ -119,7 +162,7 @@ - + @@ -130,7 +173,7 @@ - + @@ -141,7 +184,7 @@ - + @@ -152,7 +195,7 @@ - + @@ -165,14 +208,31 @@ - + - + + + + SELECT COUNT(*) FROM ( + SELECT INDEX_NAME + FROM information_schema.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'follows' + AND NON_UNIQUE = 0 + AND INDEX_NAME != 'PRIMARY' + AND COLUMN_NAME IN ('follower_id', 'following_id') + GROUP BY INDEX_NAME + HAVING COUNT(DISTINCT COLUMN_NAME) = 2 + ) AS existing_composite_unique_indexes + + - + - + @@ -183,7 +243,7 @@ - + @@ -194,7 +254,7 @@ - + @@ -207,14 +267,31 @@ - + - + + + + SELECT COUNT(*) FROM ( + SELECT INDEX_NAME + FROM information_schema.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'likes' + AND NON_UNIQUE = 0 + AND INDEX_NAME != 'PRIMARY' + AND COLUMN_NAME IN ('users_id', 'posts_id') + GROUP BY INDEX_NAME + HAVING COUNT(DISTINCT COLUMN_NAME) = 2 + ) AS existing_composite_unique_indexes + + - + - + @@ -223,7 +300,7 @@ - + @@ -236,7 +313,7 @@ - + @@ -246,7 +323,7 @@ - + @@ -259,7 +336,7 @@ - + @@ -270,7 +347,7 @@ - + @@ -279,7 +356,7 @@ - + From c95464e2d2df3211df24f99df650255ad816292c Mon Sep 17 00:00:00 2001 From: Theaprince001 Date: Tue, 10 Nov 2026 14:07:56 +0530 Subject: [PATCH 04/15] docs: simplify migration guide to match project architecture --- docs/migrations.md | 132 ++++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 67 deletions(-) diff --git a/docs/migrations.md b/docs/migrations.md index 9d7502b..c47cc63 100644 --- a/docs/migrations.md +++ b/docs/migrations.md @@ -5,102 +5,100 @@ Schema is managed by [Liquibase](https://www.liquibase.org/) via 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 -Migrations run automatically when the app starts: +Set up the environment file and start the database: ```bash -# Start the database +# 1. Copy the example environment file +cp .env.example .env + +# 2. Set nonempty values in .env for the required Compose variables +# (see docker-compose.base.yml for the full list): +# DB_NAME=twitter_clone +# DB_USER=moo_user +# DB_PASS=moo_pass +# DB_ROOT_PASS=root_pass + +# 3. Start the database docker compose up db +``` + +Then run the application. Liquibase applies pending changesets on startup: -# Run the app — Liquibase applies pending changesets automatically +```bash ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev ``` The dev profile reads the database URL from `${DB_NAME:twitter_clone}` and the -root password from `${DB_ROOT_PASS:root_pass}`. The defaults match what -`docker compose up db` creates, so no environment variables are required for -the standard setup. - -To use a different database or password, export the matching variable before -starting the app: +root password from `${DB_ROOT_PASS:root_pass}`. If you set different values in +`.env` for Compose, export the same values for the application so both connect +to the same database: ```bash # bash / zsh -export DB_NAME=moo -export DB_ROOT_PASS=my_root_pass +export DB_NAME=twitter_clone +export DB_ROOT_PASS=root_pass ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev ``` ```powershell # PowerShell (Windows) -$env:DB_NAME="moo" -$env:DB_ROOT_PASS="my_root_pass" +$env:DB_NAME="twitter_clone" +$env:DB_ROOT_PASS="root_pass" ./mvnw spring-boot:run "-Dspring-boot.run.profiles=dev" ``` -## Applying migrations manually +## Upgrading an existing (pre-Liquibase) database -Useful for CI pipelines, scripted deploys, or previewing changes before an -app start: +If a database was created by an older `ddl-auto=update` build that predates +this Liquibase setup, the schema may not match what the changelog creates. +The `posts.created_at` migration is provided in-place by three guarded +changesets in `001-initial-schema.xml`: -```bash -./mvnw liquibase:update \ - -Dliquibase.url="jdbc:mysql://localhost:3306/twitter_clone" \ - -Dliquibase.username=root \ - -Dliquibase.password=root_pass - -# Apply only the next N changesets (useful for staged rollouts) -./mvnw liquibase:update-count -Dliquibase.count=1 \ - -Dliquibase.url="jdbc:mysql://localhost:3306/twitter_clone" \ - -Dliquibase.username=root \ - -Dliquibase.password=root_pass -``` +- `002a-add-posts-created-at-column` — adds `created_at` if it is missing +- `002a-backfill-posts-created-at` — copies `timestamp` into `created_at` + when a legacy `timestamp` column is present +- `002a-add-posts-created-at-not-null` — enforces `NOT NULL` once the column + is populated -## Baselining an existing (pre-Liquibase) database - -If a database was created by an older `ddl-auto=update` version that predates -this Liquibase setup, baseline it before running migrations. Baselining marks -the existing changesets as already applied without executing them, so -Liquibase knows where to start applying future changes. +Each step is independently guarded, so a partial failure (e.g. the backfill +fails due to a lock timeout) is retried on the next startup instead of being +silently skipped. Just start the application against the legacy database: ```bash -# Mark all existing changesets as already applied without executing them -./mvnw liquibase:changelog-sync \ - -Dliquibase.url="jdbc:mysql://localhost:3306/legacy_db" \ - -Dliquibase.username=root \ - -Dliquibase.password=root_pass +./mvnw spring-boot:run -Dspring-boot.run.profiles=dev ``` -Prefer in-place migration: changesets `002a-add-posts-created-at-column`, -`002a-backfill-posts-created-at`, and `002a-add-posts-created-at-not-null` -provide a safe, retry-friendly migration path for older schemas that have -`posts.timestamp` but not `posts.created_at`. If you prefer to baseline -instead of migrate, run the backfill manually: +Liquibase runs the three `002a-*` changesets in order, preserving existing +`timestamp` data. -```sql -UPDATE posts SET created_at = `timestamp` WHERE created_at IS NULL; -ALTER TABLE posts MODIFY created_at DATETIME(6) NOT NULL; -``` +Do **not** run `liquibase:changelog-sync` against a legacy database without +verifying that every changeset's schema change already exists. `changelog-sync` +marks each changeset as applied without executing it — if an index or +column from the changelog is missing on the legacy schema, that change is +silently skipped and will never be applied on future startups. ## Production -Production applies pending changesets on startup -(`spring.liquibase.enabled=true`). For zero-downtime deployments, run -migrations as a pre-deploy step before rolling out the new app version, -so failures are caught before any new pods receive traffic: - -```bash -./mvnw liquibase:update \ - -Dliquibase.url="${MYSQL_DB_URL}" \ - -Dliquibase.username="${MYSQL_DB_UNAME}" \ - -Dliquibase.password="${MYSQL_DB_PASSWD}" -``` - -Then roll out the new app version. If a migration needs to be rolled back, -use `liquibase:rollback -Dliquibase.rollbackCount=1` with the same connection -parameters. - -See -[`db.changelog-master.xml`](../src/main/resources/db/changelog/db.changelog-master.xml) -for the ordered list of changesets. \ No newline at end of file +Production applies pending changesets on application startup +(`spring.liquibase.enabled=true` in `application.properties`). The recommended +deployment flow: + +1. Deploy the new application version. +2. Liquibase runs before the app becomes ready. If a changeset fails, the app + does not start, and the previous version continues serving traffic. +3. Verify `DATABASECHANGELOG` shows the expected changesets applied. +4. For zero-downtime deployments, deploy to a single canary instance first, + verify the schema, then roll out the remaining instances. + +Rollback is **not a supported recovery path**. The current changelog does +not define `` 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 migration needs to be reversed, deploy a new +forward-only changeset that compensates for the previous one. \ No newline at end of file From aa44dcd26a364105ad2d94c819945dd77d583338 Mon Sep 17 00:00:00 2001 From: Theaprince001 Date: Wed, 11 Nov 2026 12:02:48 +0530 Subject: [PATCH 05/15] fix: address third-round CodeRabbit review - docs/migrations.md: - require JWT_SECRET export before running Maven (Maven does not read .env) - use detached docker compose up -d db so the command returns - warn that dev profile defaults to twitter_clone; require explicit DB_NAME override to target a legacy witter database - distinguish rolling / blue-green vs single-container Compose for migration failure behavior; correct the false claim about previous version continuing to serve traffic - changelog: scope posts.created_at not-null sqlCheck to current schema with TABLE_SCHEMA = SCHEMA() (wrapped in for H2) - prod properties: restore removed database connection and credential comments --- docs/migrations.md | 86 ++++++++++++------- .../resources/application-prod.properties | 7 +- .../db/changelog/001-initial-schema.xml | 16 ++-- 3 files changed, 72 insertions(+), 37 deletions(-) diff --git a/docs/migrations.md b/docs/migrations.md index c47cc63..668dace 100644 --- a/docs/migrations.md +++ b/docs/migrations.md @@ -17,32 +17,28 @@ Set up the environment file and start the database: # 1. Copy the example environment file cp .env.example .env -# 2. Set nonempty values in .env for the required Compose variables +# 2. Set nonempty values in .env for all required Compose variables # (see docker-compose.base.yml for the full list): # DB_NAME=twitter_clone # DB_USER=moo_user # DB_PASS=moo_pass # DB_ROOT_PASS=root_pass +# JWT_SECRET= -# 3. Start the database -docker compose up db -``` - -Then run the application. Liquibase applies pending changesets on startup: - -```bash -./mvnw spring-boot:run -Dspring-boot.run.profiles=dev +# 3. Start the database in detached mode so the command returns to the shell +docker compose up -d db ``` +Maven does not read `.env` — export every value the application needs. The dev profile reads the database URL from `${DB_NAME:twitter_clone}` and the -root password from `${DB_ROOT_PASS:root_pass}`. If you set different values in -`.env` for Compose, export the same values for the application so both connect -to the same database: +root password from `${DB_ROOT_PASS:root_pass}`, and it requires `JWT_SECRET` +to be set (root `application.properties` uses `${JWT_SECRET}` with no default): ```bash # bash / zsh export DB_NAME=twitter_clone export DB_ROOT_PASS=root_pass +export JWT_SECRET="$(openssl rand -hex 32)" ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev ``` @@ -50,6 +46,7 @@ export DB_ROOT_PASS=root_pass # PowerShell (Windows) $env:DB_NAME="twitter_clone" $env:DB_ROOT_PASS="root_pass" +$env:JWT_SECRET="" ./mvnw spring-boot:run "-Dspring-boot.run.profiles=dev" ``` @@ -57,6 +54,39 @@ $env:DB_ROOT_PASS="root_pass" If a database was created by an older `ddl-auto=update` build that predates this Liquibase setup, the schema may not match what the changelog creates. +Select the target database explicitly before starting the upgrade. The dev +profile defaults to `twitter_clone` and enables `createDatabaseIfNotExist=true`, +so a legacy setup that used a different name (for example `twitter`) would be +silently ignored — the app would create a fresh empty `twitter_clone` and +upgrade that, leaving the real legacy data untouched. Point the app at the +existing database by exporting `DB_NAME` with the legacy name: + +```bash +# bash / zsh — target a legacy database named "twitter" +export DB_NAME=twitter +export DB_ROOT_PASS=root_pass +export JWT_SECRET="$(openssl rand -hex 32)" +./mvnw spring-boot:run -Dspring-boot.run.profiles=dev +``` + +```powershell +# PowerShell (Windows) — target a legacy database named "twitter" +$env:DB_NAME="twitter" +$env:DB_ROOT_PASS="root_pass" +$env:JWT_SECRET="" +./mvnw spring-boot:run "-Dspring-boot.run.profiles=dev" +``` + +Before the migration runs, verify the startup log shows the intended database: + +```text +Database JDBC URL [jdbc:mysql://localhost:3306/twitter?...] +Default catalog/schema: twitter/undefined +``` + +If `Default catalog/schema` shows `twitter_clone` when you expected `twitter`, +stop the app and re-check the exported `DB_NAME`. + The `posts.created_at` migration is provided in-place by three guarded changesets in `001-initial-schema.xml`: @@ -68,14 +98,7 @@ changesets in `001-initial-schema.xml`: Each step is independently guarded, so a partial failure (e.g. the backfill fails due to a lock timeout) is retried on the next startup instead of being -silently skipped. Just start the application against the legacy database: - -```bash -./mvnw spring-boot:run -Dspring-boot.run.profiles=dev -``` - -Liquibase runs the three `002a-*` changesets in order, preserving existing -`timestamp` data. +silently skipped. Do **not** run `liquibase:changelog-sync` against a legacy database without verifying that every changeset's schema change already exists. `changelog-sync` @@ -86,15 +109,20 @@ silently skipped and will never be applied on future startups. ## Production Production applies pending changesets on application startup -(`spring.liquibase.enabled=true` in `application.properties`). The recommended -deployment flow: - -1. Deploy the new application version. -2. Liquibase runs before the app becomes ready. If a changeset fails, the app - does not start, and the previous version continues serving traffic. -3. Verify `DATABASECHANGELOG` shows the expected changesets applied. -4. For zero-downtime deployments, deploy to a single canary instance first, - verify the schema, then roll out the remaining instances. +(`spring.liquibase.enabled=true` in `application.properties`). What happens +when a migration fails depends on the deployment topology: + +- **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. Rollback is **not a supported recovery path**. The current changelog does not define `` blocks, so `liquibase:rollback` can only reverse diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index 3f79fae..090d142 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -16,7 +16,7 @@ # along with this program. If not, see . # -###### THIS IS THE DEV PROPERTIES ##### +###### THIS IS THE PRODUCTION PROPERTIES ##### # ===================================== # ========= SPRING SECURITY =========== @@ -28,7 +28,10 @@ # =========== DATA SOURCE ============= # ===================================== -spring.datasource.url=jdbc:mysql://${MYSQL_DB_HOST}:${MYSQL_DB_PORT}/${MYSQL_DB_NAME}?createDatabaseIfNotExist=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Kolkata&useSSL=false +# Set here configurations for the database connection +spring.datasource.url=jdbc:mysql://${MYSQL_DB_HOST}:${MYSQL_DB_PORT}/${MYSQL_DB_NAME}?createDatabaseIfNotExist=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Kolkata&sslMode=REQUIRED + +# Username and secret spring.datasource.username=${MYSQL_DB_UNAME} spring.datasource.password=${MYSQL_DB_PASSWD} diff --git a/src/main/resources/db/changelog/001-initial-schema.xml b/src/main/resources/db/changelog/001-initial-schema.xml index 3f348fe..6169499 100644 --- a/src/main/resources/db/changelog/001-initial-schema.xml +++ b/src/main/resources/db/changelog/001-initial-schema.xml @@ -110,12 +110,16 @@ - - SELECT COUNT(*) FROM information_schema.COLUMNS - WHERE UPPER(TABLE_NAME) = 'POSTS' - AND UPPER(COLUMN_NAME) = 'CREATED_AT' - AND UPPER(IS_NULLABLE) = 'YES' - + + + + SELECT COUNT(*) FROM information_schema.COLUMNS + WHERE UPPER(TABLE_NAME) = 'POSTS' + AND UPPER(COLUMN_NAME) = 'CREATED_AT' + AND UPPER(IS_NULLABLE) = 'YES' + AND TABLE_SCHEMA = SCHEMA() + + Date: Wed, 11 Nov 2026 12:40:14 +0530 Subject: [PATCH 06/15] docs+prod: address CodeRabbit review --- docs/migrations.md | 33 +++++++++++++------ .../resources/application-prod.properties | 3 +- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/docs/migrations.md b/docs/migrations.md index 668dace..2c00854 100644 --- a/docs/migrations.md +++ b/docs/migrations.md @@ -62,18 +62,18 @@ upgrade that, leaving the real legacy data untouched. Point the app at the existing database by exporting `DB_NAME` with the legacy name: ```bash -# bash / zsh — target a legacy database named "twitter" -export DB_NAME=twitter -export DB_ROOT_PASS=root_pass +# bash / zsh — use the values from your .env file +export DB_NAME="" +export DB_ROOT_PASS="" export JWT_SECRET="$(openssl rand -hex 32)" ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev ``` ```powershell -# PowerShell (Windows) — target a legacy database named "twitter" -$env:DB_NAME="twitter" -$env:DB_ROOT_PASS="root_pass" -$env:JWT_SECRET="" +# PowerShell (Windows) — use the values from your .env file +$env:DB_NAME = "" +$env:DB_ROOT_PASS = "" +$env:JWT_SECRET = "" ./mvnw spring-boot:run "-Dspring-boot.run.profiles=dev" ``` @@ -113,9 +113,22 @@ Production applies pending changesets on application startup when a migration fails depends on the deployment topology: - **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. + between them): the previous instance keeps serving reads while the new + instance applies migrations. If a migration fails, only the new instance + is affected. + + **However**, the `002a-add-posts-created-at-not-null` changeset adds + `created_at` as `NOT NULL` without a default. Once applied, the **old + instance can no longer INSERT into `posts`** — MySQL's default strict + mode rejects inserts that omit a `NOT NULL` column with no default. + Before this migration runs in a two-instance setup, either: + + - **Pause writes to `posts`** during the migration window, or + - **Stage the migration across two releases:** Release A ships with the + column nullable and dual-writes populated by both old and new + instances; Release B enforces `NOT NULL` once all writers populate it. + + Blue-green alone is not sufficient for this changeset. - **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 diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index 090d142..51460ed 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -29,8 +29,7 @@ # ===================================== # Set here configurations for the database connection -spring.datasource.url=jdbc:mysql://${MYSQL_DB_HOST}:${MYSQL_DB_PORT}/${MYSQL_DB_NAME}?createDatabaseIfNotExist=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Kolkata&sslMode=REQUIRED - +spring.datasource.url=jdbc:mysql://${MYSQL_DB_HOST}:${MYSQL_DB_PORT}/${MYSQL_DB_NAME}?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Kolkata&sslMode=REQUIRED # Username and secret spring.datasource.username=${MYSQL_DB_UNAME} spring.datasource.password=${MYSQL_DB_PASSWD} From 7d62780cfbd9405bed942477714b11c66baa7499 Mon Sep 17 00:00:00 2001 From: Theaprince001 Date: Thu, 12 Nov 2026 12:07:50 +0530 Subject: [PATCH 07/15] refactor: remove speculative legacy posts.created_at backfill per maintainer --- .../db/changelog/001-initial-schema.xml | 68 ------------------- 1 file changed, 68 deletions(-) diff --git a/src/main/resources/db/changelog/001-initial-schema.xml b/src/main/resources/db/changelog/001-initial-schema.xml index 6169499..c8e6090 100644 --- a/src/main/resources/db/changelog/001-initial-schema.xml +++ b/src/main/resources/db/changelog/001-initial-schema.xml @@ -59,74 +59,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - UPDATE posts - SET created_at = COALESCE(`timestamp`, CURRENT_TIMESTAMP(6)) - WHERE created_at IS NULL; - - - - - - - - - - - - SELECT COUNT(*) FROM information_schema.COLUMNS - WHERE UPPER(TABLE_NAME) = 'POSTS' - AND UPPER(COLUMN_NAME) = 'CREATED_AT' - AND UPPER(IS_NULLABLE) = 'YES' - AND TABLE_SCHEMA = SCHEMA() - - - - - - - From 01896b7289d586e497970be44a655bc79371f7b6 Mon Sep 17 00:00:00 2001 From: Theaprince001 Date: Thu, 12 Nov 2026 15:48:52 +0530 Subject: [PATCH 08/15] docs: remove stale 002a-* references from migration guide --- docs/migrations.md | 91 ++++++---------------------------------------- 1 file changed, 11 insertions(+), 80 deletions(-) diff --git a/docs/migrations.md b/docs/migrations.md index 2c00854..be80506 100644 --- a/docs/migrations.md +++ b/docs/migrations.md @@ -19,10 +19,10 @@ cp .env.example .env # 2. Set nonempty values in .env for all required Compose variables # (see docker-compose.base.yml for the full list): -# DB_NAME=twitter_clone -# DB_USER=moo_user -# DB_PASS=moo_pass -# DB_ROOT_PASS=root_pass +# DB_NAME= +# DB_USER= +# DB_PASS= +# DB_ROOT_PASS= # JWT_SECRET= # 3. Start the database in detached mode so the command returns to the shell @@ -34,78 +34,22 @@ The dev profile reads the database URL from `${DB_NAME:twitter_clone}` and the root password from `${DB_ROOT_PASS:root_pass}`, and it requires `JWT_SECRET` to be set (root `application.properties` uses `${JWT_SECRET}` with no default): -```bash -# bash / zsh -export DB_NAME=twitter_clone -export DB_ROOT_PASS=root_pass -export JWT_SECRET="$(openssl rand -hex 32)" -./mvnw spring-boot:run -Dspring-boot.run.profiles=dev -``` - -```powershell -# PowerShell (Windows) -$env:DB_NAME="twitter_clone" -$env:DB_ROOT_PASS="root_pass" -$env:JWT_SECRET="" -./mvnw spring-boot:run "-Dspring-boot.run.profiles=dev" -``` - -## Upgrading an existing (pre-Liquibase) database - -If a database was created by an older `ddl-auto=update` build that predates -this Liquibase setup, the schema may not match what the changelog creates. -Select the target database explicitly before starting the upgrade. The dev -profile defaults to `twitter_clone` and enables `createDatabaseIfNotExist=true`, -so a legacy setup that used a different name (for example `twitter`) would be -silently ignored — the app would create a fresh empty `twitter_clone` and -upgrade that, leaving the real legacy data untouched. Point the app at the -existing database by exporting `DB_NAME` with the legacy name: - ```bash # bash / zsh — use the values from your .env file -export DB_NAME="" -export DB_ROOT_PASS="" +export DB_NAME="" +export DB_ROOT_PASS="" 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 -$env:DB_NAME = "" -$env:DB_ROOT_PASS = "" +$env:DB_NAME = "" +$env:DB_ROOT_PASS = "" $env:JWT_SECRET = "" ./mvnw spring-boot:run "-Dspring-boot.run.profiles=dev" ``` -Before the migration runs, verify the startup log shows the intended database: - -```text -Database JDBC URL [jdbc:mysql://localhost:3306/twitter?...] -Default catalog/schema: twitter/undefined -``` - -If `Default catalog/schema` shows `twitter_clone` when you expected `twitter`, -stop the app and re-check the exported `DB_NAME`. - -The `posts.created_at` migration is provided in-place by three guarded -changesets in `001-initial-schema.xml`: - -- `002a-add-posts-created-at-column` — adds `created_at` if it is missing -- `002a-backfill-posts-created-at` — copies `timestamp` into `created_at` - when a legacy `timestamp` column is present -- `002a-add-posts-created-at-not-null` — enforces `NOT NULL` once the column - is populated - -Each step is independently guarded, so a partial failure (e.g. the backfill -fails due to a lock timeout) is retried on the next startup instead of being -silently skipped. - -Do **not** run `liquibase:changelog-sync` against a legacy database without -verifying that every changeset's schema change already exists. `changelog-sync` -marks each changeset as applied without executing it — if an index or -column from the changelog is missing on the legacy schema, that change is -silently skipped and will never be applied on future startups. - ## Production Production applies pending changesets on application startup @@ -113,22 +57,9 @@ Production applies pending changesets on application startup when a migration fails depends on the deployment topology: - **Rolling / blue-green deployments** (two app instances, traffic routed - between them): the previous instance keeps serving reads while the new - instance applies migrations. If a migration fails, only the new instance - is affected. - - **However**, the `002a-add-posts-created-at-not-null` changeset adds - `created_at` as `NOT NULL` without a default. Once applied, the **old - instance can no longer INSERT into `posts`** — MySQL's default strict - mode rejects inserts that omit a `NOT NULL` column with no default. - Before this migration runs in a two-instance setup, either: - - - **Pause writes to `posts`** during the migration window, or - - **Stage the migration across two releases:** Release A ships with the - column nullable and dual-writes populated by both old and new - instances; Release B enforces `NOT NULL` once all writers populate it. - - Blue-green alone is not sufficient for this changeset. + 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 From 9aa31707747f7a77f00de594fa953cf68f7c991c Mon Sep 17 00:00:00 2001 From: Theaprince001 Date: Thu, 12 Nov 2026 22:42:30 +0530 Subject: [PATCH 09/15] docs: use single quotes for env exports to preserve special characters --- docs/migrations.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/migrations.md b/docs/migrations.md index be80506..42a64aa 100644 --- a/docs/migrations.md +++ b/docs/migrations.md @@ -36,17 +36,17 @@ to be set (root `application.properties` uses `${JWT_SECRET}` with no default): ```bash # bash / zsh — use the values from your .env file -export DB_NAME="" -export DB_ROOT_PASS="" +export DB_NAME='' +export DB_ROOT_PASS='' 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 -$env:DB_NAME = "" -$env:DB_ROOT_PASS = "" -$env:JWT_SECRET = "" +$env:DB_NAME = '' +$env:DB_ROOT_PASS = '' +$env:JWT_SECRET = '' ./mvnw spring-boot:run "-Dspring-boot.run.profiles=dev" ``` From 6fc79f4384a28f4f58f7d588961ab6dcdb432953 Mon Sep 17 00:00:00 2001 From: Theaprince001 Date: Thu, 12 Nov 2026 22:52:45 +0530 Subject: [PATCH 10/15] docs: add note on handling embedded single quotes in env vars --- docs/migrations.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/migrations.md b/docs/migrations.md index 42a64aa..825afc8 100644 --- a/docs/migrations.md +++ b/docs/migrations.md @@ -36,6 +36,9 @@ 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='' export DB_ROOT_PASS='' export JWT_SECRET="$(openssl rand -hex 32)" @@ -44,6 +47,8 @@ export JWT_SECRET="$(openssl rand -hex 32)" ```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 = '' $env:DB_ROOT_PASS = '' $env:JWT_SECRET = '' From 7ce86e3e716630ad66f4b3b46649d7e79d474d98 Mon Sep 17 00:00:00 2001 From: Theaprince001 Date: Tue, 17 Nov 2026 12:46:51 +0530 Subject: [PATCH 11/15] docs: clarify .env quoting vs shell export quoting for passwords --- docs/migrations.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/migrations.md b/docs/migrations.md index 825afc8..a685165 100644 --- a/docs/migrations.md +++ b/docs/migrations.md @@ -18,12 +18,15 @@ Set up the environment file and start the database: cp .env.example .env # 2. Set nonempty values in .env for all required Compose variables -# (see docker-compose.base.yml for the full list): -# DB_NAME= -# DB_USER= -# DB_PASS= -# DB_ROOT_PASS= -# JWT_SECRET= +# (see docker-compose.base.yml for the full list). +# Wrap passwords in single quotes to prevent Compose from interpolating +# special characters (like $). For embedded single quotes in .env, +# use double quotes and escape them, or double the single quote (''). +# DB_NAME=twitter_clone +# DB_USER=moo_user +# DB_PASS='moo_pass' +# DB_ROOT_PASS='root_pass' +# JWT_SECRET='' # 3. Start the database in detached mode so the command returns to the shell docker compose up -d db @@ -61,6 +64,10 @@ 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 From 8ef94597da28ff85a36eb77b34a7d7c0fbd6dd54 Mon Sep 17 00:00:00 2001 From: Theaprince001 Date: Tue, 17 Nov 2026 12:56:19 +0530 Subject: [PATCH 12/15] docs: fix .env single quote escaping in migration guide --- docs/migrations.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/migrations.md b/docs/migrations.md index a685165..1b522bb 100644 --- a/docs/migrations.md +++ b/docs/migrations.md @@ -20,8 +20,8 @@ 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 embedded single quotes in .env, -# use double quotes and escape them, or double the single quote (''). +# special characters (like $). For an apostrophe in a single-quoted +# value, escape it with a backslash (e.g., DB_ROOT_PASS='can\'t'). # DB_NAME=twitter_clone # DB_USER=moo_user # DB_PASS='moo_pass' From 8c30f127b52c62b6e014defde5ad9af711ba2226 Mon Sep 17 00:00:00 2001 From: Subhrodip Mohanta Date: Tue, 17 Nov 2026 13:13:04 +0100 Subject: [PATCH 13/15] fix: secure MySQL development authentication --- docker-compose.base.yml | 1 - src/main/resources/application-dev.properties | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/docker-compose.base.yml b/docker-compose.base.yml index fc87242..e8b9710 100644 --- a/docker-compose.base.yml +++ b/docker-compose.base.yml @@ -24,7 +24,6 @@ services: db: image: mysql:8.4 container_name: moo-db -# command: --default-authentication-plugin=mysql_native_password environment: MYSQL_DATABASE: ${DB_NAME:?DB_NAME is required} MYSQL_USER: ${DB_USER:?DB_USER is required} diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 2650b7c..d3cc21c 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -29,7 +29,7 @@ # ===================================== # Set here configurations for the database connection -spring.datasource.url=jdbc:mysql://localhost:3306/${DB_NAME:twitter_clone}?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Kolkata +spring.datasource.url=jdbc:mysql://localhost:3306/${DB_NAME:twitter_clone}?createDatabaseIfNotExist=true&sslMode=REQUIRED&serverTimezone=Asia/Kolkata # Username and secret spring.datasource.username=root From d50379d4ea1ffca5717d91a138f092513aaccf5b Mon Sep 17 00:00:00 2001 From: Subhrodip Mohanta Date: Tue, 17 Nov 2026 13:23:10 +0100 Subject: [PATCH 14/15] fix: honor configured development database port --- docs/getting-started/configuration.md | 5 +++-- docs/migrations.md | 12 ++++++++---- src/main/resources/application-dev.properties | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/docs/getting-started/configuration.md b/docs/getting-started/configuration.md index 0321bf7..9e0b8ba 100644 --- a/docs/getting-started/configuration.md +++ b/docs/getting-started/configuration.md @@ -27,8 +27,9 @@ The production profile reads: - `MYSQL_DB_UNAME` - `MYSQL_DB_PASSWD` -The development profile uses MySQL on `localhost:3306` with the local Compose -credentials. Tests use an in-memory H2 database in MySQL compatibility mode. +The development profile uses MySQL on `localhost` with the local Compose +credentials and `DB_PORT_HOST` port (`3306` by default). Tests use an in-memory +H2 database in MySQL compatibility mode. ## API documentation diff --git a/docs/migrations.md b/docs/migrations.md index 1b522bb..e670f01 100644 --- a/docs/migrations.md +++ b/docs/migrations.md @@ -22,6 +22,7 @@ cp .env.example .env # 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' @@ -33,9 +34,10 @@ docker compose up -d db ``` Maven does not read `.env` — export every value the application needs. -The dev profile reads the database URL from `${DB_NAME:twitter_clone}` and the -root password from `${DB_ROOT_PASS:root_pass}`, and it requires `JWT_SECRET` -to be set (root `application.properties` uses `${JWT_SECRET}` with no default): +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 @@ -43,6 +45,7 @@ to be set (root `application.properties` uses `${JWT_SECRET}` with no default): # 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='' +export DB_PORT_HOST='' export DB_ROOT_PASS='' export JWT_SECRET="$(openssl rand -hex 32)" ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev @@ -53,6 +56,7 @@ export JWT_SECRET="$(openssl rand -hex 32)" # 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 = '' +$env:DB_PORT_HOST = '' $env:DB_ROOT_PASS = '' $env:JWT_SECRET = '' ./mvnw spring-boot:run "-Dspring-boot.run.profiles=dev" @@ -85,4 +89,4 @@ not define `` 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 migration needs to be reversed, deploy a new -forward-only changeset that compensates for the previous one. \ No newline at end of file +forward-only changeset that compensates for the previous one. diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index d3cc21c..c3d4c71 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -29,7 +29,7 @@ # ===================================== # Set here configurations for the database connection -spring.datasource.url=jdbc:mysql://localhost:3306/${DB_NAME:twitter_clone}?createDatabaseIfNotExist=true&sslMode=REQUIRED&serverTimezone=Asia/Kolkata +spring.datasource.url=jdbc:mysql://localhost:${DB_PORT_HOST:3306}/${DB_NAME:twitter_clone}?createDatabaseIfNotExist=true&sslMode=REQUIRED&serverTimezone=Asia/Kolkata # Username and secret spring.datasource.username=root From 02c45087071390c4a6bb7f4879283f36b5fca2ec Mon Sep 17 00:00:00 2001 From: Subhrodip Mohanta Date: Tue, 17 Nov 2026 16:21:31 +0100 Subject: [PATCH 15/15] docs: clarify migration failure recovery --- docs/migrations.md | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/docs/migrations.md b/docs/migrations.md index e670f01..890a815 100644 --- a/docs/migrations.md +++ b/docs/migrations.md @@ -29,10 +29,17 @@ cp .env.example .env # DB_ROOT_PASS='root_pass' # JWT_SECRET='' -# 3. Start the database in detached mode so the command returns to the shell +# 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 @@ -84,9 +91,16 @@ when a migration fails depends on the deployment topology: For zero-downtime migrations, use a multi-instance setup where the previous version keeps running while the new version applies changes. -Rollback is **not a supported recovery path**. The current changelog does -not define `` blocks, so `liquibase:rollback` can only reverse -changesets via Liquibase's auto-generated inverse operations — which for +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 `` 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 migration needs to be reversed, deploy a new -forward-only changeset that compensates for the previous one. +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.