Skip to content

Commit 5c6e0aa

Browse files
committed
feat: a nix flake app tool to run dbmate migrations and produce
schema.slq files
1 parent e633e59 commit 5c6e0aa

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ result*
2222
#IDE
2323
.idea/
2424
.vscode/
25+
26+
db

nix/tools/dbmate-tool.sh.in

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,37 @@ export DATABASE_URL="postgres://$PGSQL_USER:$PGPASSWORD@localhost:$PORTNO/postgr
196196

197197
ls -la "$MIGRATIONS_DIR"
198198
pwd
199-
dbmate --migrations-dir "$MIGRATIONS_DIR/init-scripts" up
200-
"${PSQLBIN}/psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"
199+
# First dbmate command
200+
if ! dbmate --migrations-dir "$MIGRATIONS_DIR/init-scripts" up; then
201+
echo "Error: Initial migration failed"
202+
exit 1
203+
fi
204+
205+
# Password update command
206+
if ! "${PSQLBIN}/psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"; then
207+
echo "Error: Failed to update supabase_admin password"
208+
exit 1
209+
fi
210+
211+
# Set up database URL
201212
export DATABASE_URL="postgres://$PGSQL_SUPERUSER:$PGPASSWORD@localhost:$PORTNO/postgres?sslmode=disable"
202-
dbmate --migrations-dir "$MIGRATIONS_DIR/migrations" up
203-
echo "PostgreSQL configuration completed successfully"
213+
214+
# Second dbmate command
215+
if ! dbmate --migrations-dir "$MIGRATIONS_DIR/migrations" up; then
216+
echo "Error: Final migration failed"
217+
exit 1
218+
fi
219+
220+
if [ -f "./db/schema.sql" ]; then
221+
mv "./db/schema.sql" "./db/schema-$PSQL_VERSION.sql"
222+
rm -rf ./db
223+
echo "Schema file moved to ./migrations/schema-$PSQL_VERSION.sql"
224+
else
225+
echo "Warning: schema.sql file not found in ./db directory"
226+
exit 1
227+
fi
228+
229+
"${PSQLBIN}/pg_dump" -U "$PGSQL_SUPERUSER" -p "$PORTNO" -h localhost -d postgres -s -f "./pg-dump-schema-$PSQL_VERSION.sql"
230+
231+
# If we get here, all commands succeeded
232+
echo "PostgreSQL migration completed successfully"

0 commit comments

Comments
 (0)