Skip to content

Commit

Permalink
feat: add graphile-migrate for managing migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
wgwz committed Sep 27, 2023
1 parent 66110e5 commit 17c0f32
Show file tree
Hide file tree
Showing 7 changed files with 1,532 additions and 0 deletions.
131 changes: 131 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,134 @@ cython_debug/
# vendor/

/.idea/

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
140 changes: 140 additions & 0 deletions .gmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Graphile Migrate configuration.
*
* If you decide to commit this file (recommended) please ensure that it does
* not contain any secrets (passwords, etc) - we recommend you manage these
* with environmental variables instead.
*
* This file is in JSON5 format, in VSCode you can use "JSON with comments" as
* the file format.
*/
{
/*
* connectionString: this tells Graphile Migrate where to find the database
* to run the migrations against.
*
* RECOMMENDATION: use `DATABASE_URL` envvar instead.
*/
// "connectionString": "postgres://appuser:apppassword@host:5432/appdb",

/*
* shadowConnectionString: like connectionString, but this is used for the
* shadow database (which will be reset frequently).
*
* RECOMMENDATION: use `SHADOW_DATABASE_URL` envvar instead.
*/
// "shadowConnectionString": "postgres://appuser:apppassword@host:5432/appdb_shadow",

/*
* rootConnectionString: like connectionString, but this is used for
* dropping/creating the database in `graphile-migrate reset`. This isn't
* necessary, shouldn't be used in production, but helps during development.
*
* RECOMMENDATION: use `ROOT_DATABASE_URL` envvar instead.
*/
// "rootConnectionString": "postgres://adminuser:adminpassword@host:5432/postgres",

/*
* pgSettings: key-value settings to be automatically loaded into PostgreSQL
* before running migrations, using an equivalent of `SET LOCAL <key> TO
* <value>`
*/
"pgSettings": {
// "search_path": "app_public,app_private,app_hidden,public",
},

/*
* placeholders: substituted in SQL files when compiled/executed. Placeholder
* keys should be prefixed with a colon and in all caps, like
* `:COLON_PREFIXED_ALL_CAPS`. Placeholder values should be strings. They
* will be replaced verbatim with NO ESCAPING AT ALL (this differs from how
* psql handles placeholders) so should only be used with "safe" values. This
* is useful for committing migrations where certain parameters can change
* between environments (development, staging, production) but you wish to
* use the same signed migration files for all.
*
* The special value "!ENV" can be used to indicate an environmental variable
* of the same name should be used.
*
* Graphile Migrate automatically sets the `:DATABASE_NAME` and
* `:DATABASE_OWNER` placeholders, and you should not attempt to override
* these.
*/
"placeholders": {
// ":DATABASE_VISITOR": "!ENV", // Uses process.env.DATABASE_VISITOR
},

/*
* Actions allow you to run scripts or commands at certain points in the
* migration lifecycle. SQL files are ran against the database directly.
* "command" actions are ran with the following environmental variables set:
*
* - GM_DBURL: the PostgreSQL URL of the database being migrated
* - GM_DBNAME: the name of the database from GM_DBURL
* - GM_DBUSER: the user from GM_DBURL
* - GM_SHADOW: set to 1 if the shadow database is being migrated, left unset
* otherwise
*
* If "shadow" is unspecified, the actions will run on events to both shadow
* and normal databases. If "shadow" is true the action will only run on
* actions to the shadow DB, and if false only on actions to the main DB.
*/

/*
* afterReset: actions executed after a `graphile-migrate reset` command.
*/
"afterReset": [
// "afterReset.sql",
// { "_": "command", "command": "graphile-worker --schema-only" },
"initial_schema.sql"
],

/*
* afterAllMigrations: actions executed once all migrations are complete.
*/
"afterAllMigrations": [
{
"_": "command",
"shadow": true,
"command": "pg_dump --no-sync --schema-only --no-owner --file=./migrations/schema_snapshot.sql $SHADOW_DATABASE_URL",
},
],

/*
* afterCurrent: actions executed once the current migration has been
* evaluated (i.e. in watch mode).
*/
"afterCurrent": [
// {
// "_": "command",
// "shadow": true,
// "command": "if [ \"$IN_TESTS\" = \"1\" ]; then ./scripts/test-seed; fi",
// },
],

/*
* blankMigrationContent: content to be written to the current migration
* after commit. NOTE: this should only contain comments.
*/
// "blankMigrationContent": "-- Write your migration here\n",

/****************************************************************************\
*** ***
*** You probably don't want to edit anything below here. ***
*** ***
\****************************************************************************/

/*
* manageGraphileMigrateSchema: if you set this false, you must be sure to
* keep the graphile_migrate schema up to date yourself. We recommend you
* leave it at its default.
*/
// "manageGraphileMigrateSchema": true,

/*
* migrationsFolder: path to the folder in which to store your migrations.
*/
// migrationsFolder: "./migrations",

"//generatedWith": "1.4.1"
}
72 changes: 72 additions & 0 deletions migrations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Migrations

This readme provides info about how to work with migrations in this repo.

## Local development

In order to develop this project locally we must use the following commands.
If this is your first time setting up the project locally, we need to initialize your database.
First, you must run the local database:

```
$ pwd
/Users/kyle/regen/indexer
$ docker-compose up --build postgres
```

Then, you must initialize the database:

```
$ export DATABASE_URL="postgres://postgres:postgres@localhost:5432/indexer"
$ export SHADOW_DATABASE_URL="postgres://postgres:postgres@localhost:5432/indexer_shadow"
$ export ROOT_DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres"
$ yarn run graphile-migrate reset --erase
```

Now, we set up a watch process that will monitor `migrations/current.sql` for your changes as well as apply them to your local database:

```
$ yarn run graphile-migrate watch
```

When you are satisfied with the changes in `migration/current.sql`, you commit them:

```
$ yarn run graphile-migrate commit
```

By committing your changes you should see a new SQL file in `migration/committed/`.

## Schema Snapshot

The schema snapshot is stored and tracked in version control as `migrations/schema_snapshot.sql`.
Each time you apply a migration in local development this snapshot will be automatically updated.
See `.gmrc` and `afterAllMigrations` from [the `graphile-migrate` configuration docs](https://github.com/graphile/migrate#configuration) for how this is done.
This allows us to keep track of the changes being introduced to the schema.
You must commit your changes to this file.

This is a helpful file to keep in mind when you have questions about entities in the database.
For example, it allows you to also view the functions in the database being used in various RLS policies.
Similarly, you can view the various policies in the database or which tables have RLS enabled.

## Deploying to staging or production

The migrations are always automatically run in Heroku for staging and production.
See the `migrate` command in `package.json` and `Procfile` for Heroku.

## Debugging

This section contains some notes that may be useful for debugging common scenarios.

### Viewing migrations applied in a particular database

Our migration tool tracks which migrations have been applied in the following table:

```
regen_registry=# select * from graphile_migrate.migrations;
hash | previous_hash | filename | date
-----------------------------------------------+---------------+------------+-------------------------------
sha1:28ab5499d9a4520daa9428681a9bf1152f9887af | | 000001.sql | 2023-05-08 20:20:31.213547+00
```

This is one way that you can track the migrations that will be deployed to staging or production.
1 change: 1 addition & 0 deletions migrations/current.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- Enter migration here
Loading

0 comments on commit 17c0f32

Please sign in to comment.