From 3a4af1ae8db804104e411b3f7705187cfd97d36b Mon Sep 17 00:00:00 2001 From: Lumither Tao Date: Mon, 13 Oct 2025 14:36:21 -0400 Subject: [PATCH] feat(migration) add migration --- Makefile | 20 +++++++++++--- backend/server/build.rs | 27 ++++++++++--------- container/release/backend.containerfile | 4 +-- frontend/Makefile | 8 ++++++ frontend/next.config.mjs | 2 +- frontend/src/constants.ts | 2 +- .../modules/blog => migration/ddl}/schema.sql | 0 migration/migration.sh | 18 +++++++++++++ prod.compose.yaml | 25 +++++++++++++++-- 9 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 frontend/Makefile rename {backend/server/src/modules/blog => migration/ddl}/schema.sql (100%) create mode 100644 migration/migration.sh diff --git a/Makefile b/Makefile index 581ed84..bfdd1e5 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,21 @@ ifeq ($(ARCH), arm64) ARCH := aarch64 endif -.PHONY: up -clean: - echo "todo" +.PHONY: all +all:up +.PHONY: build +build: + ARCH=$(ARCH) docker compose -f prod.compose.yaml build + +.PHONY: up +up: + ARCH=$(ARCH) docker compose -f prod.compose.yaml up -d + +.PHONY: down +down: + ARCH=$(ARCH) docker compose -f prod.compose.yaml down + +.PHONY: rm +rm: + ARCH=$(ARCH) docker compose -f prod.compose.yaml rm diff --git a/backend/server/build.rs b/backend/server/build.rs index aae7d8b..cdd610d 100644 --- a/backend/server/build.rs +++ b/backend/server/build.rs @@ -1,16 +1,17 @@ -use chrono::Utc; -use git2::Repository; +// use chrono::Utc; +// use git2::Repository; fn main() { - let repo = Repository::open("../..").expect("failed to open repository"); - let commit = repo - .head() - .expect("failed to get HEAD") - .peel_to_commit() - .expect("failed to get HEAD commit"); - let hash = commit.id().to_string(); - let utc_timestamp = Utc::now().date_naive().format("%Y%m%d").to_string(); - let pkg_version = env!("CARGO_PKG_VERSION"); - let build_id = format!("{}:{}@{}", pkg_version, &hash[..8], utc_timestamp); - println!("cargo:rustc-env=BUILD_ID={}", build_id); + // let repo = Repository::open("../..").expect("failed to open repository"); + // let commit = repo + // .head() + // .expect("failed to get HEAD") + // .peel_to_commit() + // .expect("failed to get HEAD commit"); + // let hash = commit.id().to_string(); + // let utc_timestamp = Utc::now().date_naive().format("%Y%m%d").to_string(); + // let pkg_version = env!("CARGO_PKG_VERSION"); + // let build_id = format!("{}:{}@{}", pkg_version, &hash[..8], utc_timestamp); + // println!("cargo:rustc-env=BUILD_ID={}", build_id); + println!("cargo:rustc-env=BUILD_ID=undefined"); } diff --git a/container/release/backend.containerfile b/container/release/backend.containerfile index a82fbb1..c30d3b7 100644 --- a/container/release/backend.containerfile +++ b/container/release/backend.containerfile @@ -5,6 +5,6 @@ RUN ["cargo", "build", "-r"] FROM gcr.io/distroless/cc AS production ARG ARCH -COPY --from=builder /app/target/release/api_server ./api_server +COPY --from=builder /app/target/release/server ./server COPY --from=builder /usr/lib/${ARCH}-linux-gnu/libz.so.1 /usr/lib/${ARCH}-linux-gnu/ -ENTRYPOINT ["./api_server"] \ No newline at end of file +ENTRYPOINT ["./server"] \ No newline at end of file diff --git a/frontend/Makefile b/frontend/Makefile new file mode 100644 index 0000000..266c5ba --- /dev/null +++ b/frontend/Makefile @@ -0,0 +1,8 @@ + +.PHONY:all +all: standalone + +.PHONY: standalone +standalone: + npm run build + cp -r public .next/standalone/ && cp -r .next/static .next/standalone/.next/ diff --git a/frontend/next.config.mjs b/frontend/next.config.mjs index c98ab2c..460605e 100644 --- a/frontend/next.config.mjs +++ b/frontend/next.config.mjs @@ -8,7 +8,7 @@ const nextConfig = { }, { protocol: 'https', - hostname: 'pic.re' + hostname: 'cdn.jsdelivr.net' } ] }, diff --git a/frontend/src/constants.ts b/frontend/src/constants.ts index 37ff01d..2288946 100644 --- a/frontend/src/constants.ts +++ b/frontend/src/constants.ts @@ -1,4 +1,4 @@ export const SITE_CONFIG = { name: 'Atelier Lumiaethor', - desc: '' + desc: 'In pursuit of absolute simplicity and ultimate sophistication' }; \ No newline at end of file diff --git a/backend/server/src/modules/blog/schema.sql b/migration/ddl/schema.sql similarity index 100% rename from backend/server/src/modules/blog/schema.sql rename to migration/ddl/schema.sql diff --git a/migration/migration.sh b/migration/migration.sh new file mode 100644 index 0000000..0cb0aa6 --- /dev/null +++ b/migration/migration.sh @@ -0,0 +1,18 @@ +#!/bin/sh +set -e + +PGPASSFILE="/root/.pgpass" +echo "postgres:5432:${POSTGRES_DB}:${POSTGRES_USER}:${POSTGRES_PASSWORD}" > "$PGPASSFILE" +chmod 600 "$PGPASSFILE" + +echo "waiting for postgres" +until pg_isready -h postgres -U "$POSTGRES_USER" -d "$POSTGRES_DB"; do + echo "retrying, sleep 2s" + sleep 2 +done +echo "db ready, running migrations" +for file in /migrations/*.sql; do + echo "applying $file" + psql -h postgres -U "$POSTGRES_USER" -d "$POSTGRES_DB" -f "$file" +done +echo "migration complete" diff --git a/prod.compose.yaml b/prod.compose.yaml index aece627..93e8cac 100644 --- a/prod.compose.yaml +++ b/prod.compose.yaml @@ -14,7 +14,7 @@ services: backend: depends_on: - - postgres + - migrate build: context: ./backend dockerfile: ../container/release/backend.containerfile @@ -29,10 +29,31 @@ services: dns: - 1.1.1.1 + + migrate: + image: postgres:17 + depends_on: + postgres: + condition: service_healthy + env_file: + - ./.env + - ./.env.local + volumes: + - ./migration/ddl:/migrations:ro + - ./migration/migration.sh:/migration.sh:ro + entrypoint: [ "/bin/sh", "/migration.sh" ] + + postgres: - image: postgres:16.3 + image: postgres:17 ports: - "5432:5432" env_file: - ./.env - ./.env.local + healthcheck: + test: [ "CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB" ] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s