Skip to content

Commit

Permalink
Add script and instructions to apply local migrations. (#58)
Browse files Browse the repository at this point in the history
* make: add `make apply_migration name=folder-name` entrypoint.
* docs: add README instructions to apply migrations.
  • Loading branch information
thruflo authored Oct 25, 2022
1 parent c9c74a3 commit 8a8a9dc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ shell:

shell_clean:
iex -S mix run --no-start

apply_migration:
./apply-local-migration.sh $(name)
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:

Expand All @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions apply-local-migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e

if [ -z "$1" ]
then
echo "Specify the migration using \`make apply_migration name=<migration folder 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

0 comments on commit 8a8a9dc

Please sign in to comment.