Skip to content
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

Add SQL-based migration script, with migration dir #878

Merged
merged 13 commits into from
Oct 8, 2023

Conversation

spwoodcock
Copy link
Member

@spwoodcock spwoodcock commented Oct 5, 2023

Issue

Solution

I have made a migration bash script, that is bundled with the backend.

The way it works:

  • SQL migration scripts are placed in the src/backend/migrations dir.
    • These should be idempotent, i.e. can run over and over without causing errors.
    • There should also be a commented out SQL script for how to revert the migration.
    • Scripts should be named sequentially, i.e. the first is 001-some-migration.sql, then they increment by one.
    • Example 000-remove-user-password.sql:
-- ## Migration to remove password field from public.users (replaced with OSM OAuth)


-- ## Apply Migration
-- Start a transaction
BEGIN;
-- Drop the 'password' column if it exists
ALTER TABLE IF EXISTS public.users
DROP COLUMN IF EXISTS password;
-- Commit the transaction
COMMIT;


-- ## Revert Migration (comment above, uncomment below)
-- -- Start a transaction
-- BEGIN;
-- -- Add the 'password' column back if it doesn't exist
-- ALTER TABLE public.users
-- ADD COLUMN IF NOT EXISTS password character varying;
-- -- Commit the transaction
-- COMMIT;
  • When the docker compose stack starts, an additional container starts up and runs a bash script once.
  • The script generates a table called migrations, which simply tracks the script name and execution date.
  • The migrations directory is scanned for new files, and if there is no record in the database of being applied, the migration is applied.

Note

I also lazily bundled a quick fix for the CI backend docker image build in this PR.

tl;dr

Create a .sql script under src/backend/migrations and it will be applied when the backend next starts.

@spwoodcock spwoodcock added enhancement New feature or request priority:high Should be addressed as a priority backend Related to backend code labels Oct 5, 2023
@spwoodcock spwoodcock self-assigned this Oct 5, 2023
@spwoodcock spwoodcock temporarily deployed to test October 5, 2023 17:23 — with GitHub Actions Inactive
@spwoodcock spwoodcock temporarily deployed to test October 5, 2023 18:01 — with GitHub Actions Inactive
@spwoodcock spwoodcock temporarily deployed to test October 5, 2023 18:32 — with GitHub Actions Inactive
@spwoodcock spwoodcock temporarily deployed to test October 5, 2023 18:41 — with GitHub Actions Inactive
@spwoodcock spwoodcock temporarily deployed to test October 5, 2023 18:53 — with GitHub Actions Inactive
@nrjadkry
Copy link
Contributor

nrjadkry commented Oct 6, 2023

@spwoodcock this looks good.
I have tested this in my setup too. One thing I notice is that, how should I run the migration script after I made a new migration file. Is there any script that runs migration files in the current db, without having to do docker restart like the one we have in alembic alembic upgrade head

By the way, have you added this in the documentation too. If not, shall I give it a go.

@spwoodcock
Copy link
Member Author

spwoodcock commented Oct 6, 2023

Thanks for the reminder about docs 👍

I'll update before I merge & include info on running manually during development (during deploy to different environments this will be handled automatically).

Option 1 is like you say: docker compose restart migrations.

Option 2: docker compose exec api bash /migrate-entrypoint.sh.

Option 3:

Make sure you have the 4 env vars for the database connection set on your machine, then:

bash src/backend/migrate-entrypoint.sh

@spwoodcock spwoodcock temporarily deployed to test October 8, 2023 15:29 — with GitHub Actions Inactive
@spwoodcock spwoodcock merged commit dd405ce into development Oct 8, 2023
2 of 3 checks passed
@spwoodcock spwoodcock deleted the feat/migrations branch October 8, 2023 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to backend code enhancement New feature or request priority:high Should be addressed as a priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants