Skip to content

Commit 1715134

Browse files
Fix(CI): SQLx Migrations and SKIP_DOCKER script config
1 parent 7b89021 commit 1715134

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

.cargo/config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ RUST_LOG="web_server=debug,lib_web=debug,services=debug"
88

99
# This will be relative to Cargo.toml
1010
# In deployed images, probably use absolute path.
11-
SERVICE_WEB_FOLDER="web/"
11+
SERVICE_WEB_FOLDER="web/"
12+
MIGRATIONS_PATH="sql/dev_initial"

scripts/init_db.sh

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ set -eo pipefail
2121
DB_HOST="${DB_HOST:=localhost}"
2222
DB_PORT="${DB_PORT:=5432}"
2323
DB_USER="${DB_USER:=postgres}"
24-
DB_PASSWORD="${DB_PASSWORD:=password}"
24+
DB_PASSWORD="${DB_PASSWORD:=welcome}"
2525
DB_NAME="${DB_NAME:=postgres}"
2626
SUPERUSER="${SUPERUSER:=postgres}"
2727
SUPERUSER_PWD="${SUPERUSER_PWD:=welcome}"
@@ -36,7 +36,7 @@ then
3636
RUNNING_POSTGRES_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.ID}}')
3737
if [[ -n $RUNNING_POSTGRES_CONTAINER ]]; then
3838
echo >&2 "there is a postgres container already running, kill it with"
39-
echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}"
39+
echo >&2 " docker stop ${RUNNING_POSTGRES_CONTAINER}"
4040
exit 1
4141
fi
4242
CONTAINER_NAME="postgres_$(date '+%s')"
@@ -90,8 +90,18 @@ until psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'
9090
done
9191
>&2 echo "Postgres is up and running on port ${DB_PORT}!"
9292

93+
9394
DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
94-
export DATABASE_URL
95-
sqlx database create
96-
sqlx migrate run
95+
MIGRATIONS_PATH="sql/dev_initial"
96+
97+
# Run migrations
98+
if [ -d "${MIGRATIONS_PATH}" ]; then
99+
echo "Running migrations from ${MIGRATIONS_PATH}..."
100+
sqlx migrate run --source "${MIGRATIONS_PATH}" --database-url "${DATABASE_URL}"
101+
echo "Migrations complete!"
102+
else
103+
echo "Error: Could not find migrations directory. Please set MIGRATIONS_PATH to the correct location."
104+
exit 1
105+
fi
106+
97107
>&2 echo "Postgres has been migrated, ready to go!"

sql/dev_initial/00-recreate-db.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- DEV ONLY - Brute Force DROP DB (for local dev and unit test)
2+
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE
3+
usename = 'app_user' OR datname = 'app_db';
4+
DROP DATABASE IF EXISTS app_db;
5+
DROP USER IF EXISTS app_user;
6+
7+
-- DEV ONLY - Dev only password (for local dev and unit test).
8+
CREATE USER app_user PASSWORD 'dev_only_pwd';
9+
CREATE DATABASE app_db owner app_user ENCODING = 'UTF-8';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---- Base app schema
2+
3+
-- User
4+
CREATE TABLE "user" (
5+
id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1000) PRIMARY KEY,
6+
7+
username varchar(128) NOT NULL UNIQUE
8+
);

sql/dev_initial/02-dev-seed.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INSERT INTO "user" (username) VALUES ('demo1');

0 commit comments

Comments
 (0)