From 8a8a9dcb22656dbea3c3ce16dbf0cae434a63a83 Mon Sep 17 00:00:00 2001 From: James Arthur Date: Tue, 25 Oct 2022 12:57:34 +0200 Subject: [PATCH] Add script and instructions to apply local migrations. (#58) * make: add `make apply_migration name=folder-name` entrypoint. * docs: add README instructions to apply migrations. --- Makefile | 3 +++ README.md | 36 ++++++++++++++++++++++++++++++++++-- apply-local-migration.sh | 15 +++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100755 apply-local-migration.sh diff --git a/Makefile b/Makefile index 5d04c960..abd695ce 100644 --- a/Makefile +++ b/Makefile @@ -71,3 +71,6 @@ shell: shell_clean: iex -S mix run --no-start + +apply_migration: + ./apply-local-migration.sh $(name) diff --git a/README.md b/README.md index 7e7fd5f9..a6b9babf 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ It's an Elixir application that integrates with Postgres over logical replicatio ## Pre-reqs -Docker and Elixir 1.13. +Docker and [Elixir 1.14 compiled with Erlang 24](https://thinkingelixir.com/install-elixir-using-asdf/). ## Usage @@ -48,7 +48,7 @@ And then develop using: make shell ``` -This runs active-active replication with Postgres over logical replication and exposes a protocol buffers API over web sockets on `localhost:30002` for the ElectricSQL client libraries. +This runs active-active replication with Postgres over logical replication and exposes a protocol buffers API over web sockets on `localhost:5133`. For example to write some data into one of the Postgres instances: @@ -58,6 +58,38 @@ docker exec -it -e PGPASSWORD=password electric_db_a_1 psql -h 127.0.0.1 -U elec There's a second instance, `electric-db_b_1`, if you want to see data being replicated between them. +Note that you can tear down all the containers with: + +```sh +make stop_dev_env +``` + +## Migrations + +When running locally, you can apply migrations directly using `make apply_migration`. First make sure you've [built your migrations](https://electric-sql.com/docs/usage/migrations) in your application folder, then set the `ELECTRIC_MIGRATIONS_DIR` environment variable to the path to the migrations folder: + +```sh +export ELECTRIC_MIGRATIONS_DIR='../path/to/migrations' +``` + +Now (re)run the electric service (with the env var set): + +```sh +make shell +``` + +You can now apply named migrations using: + +```sh +make apply_migration $MIGRATION_NAME +``` + +Where `MIGRATION_NAME` is the name of a migration folder created using [`electric migrations new`](https://electric-sql.com/docs/usage/migrations#2-schema-evolution), for example: + +```sh +make apply_migration 1666288253_create_items +``` + ## OSX Note that if, when running on OSX, you get errors like: diff --git a/apply-local-migration.sh b/apply-local-migration.sh new file mode 100755 index 00000000..f7e0e210 --- /dev/null +++ b/apply-local-migration.sh @@ -0,0 +1,15 @@ +#!/bin/bash +set -e + +if [ -z "$1" ] + then + echo "Specify the migration using \`make apply_migration name=\`." + + exit 1 +fi + +data="{\"vsn\":\"$1\"}" +json='Content-Type: application/json' + +curl -v -X PUT http://localhost:5050/api/migrations/postgres_1 -H $json -d $data +curl -v -X PUT http://localhost:5050/api/migrations/postgres_2 -H $json -d $data