Last active
March 23, 2020 16:42
-
-
Save pyrliu/f3aacdc9e02f5e54c6e6d040c977486a to your computer and use it in GitHub Desktop.
graphile-migrate with GitLab CI (community edition) "post-merge-master" validate, commit and push back to master
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stages: | |
- pre-build | |
migrate-test: | |
image: node:12 | |
stage: pre-build | |
only: | |
- master | |
allow_failure: false | |
before_script: | |
- git remote set-url origin https://gitlab-bot:${GITLAB_BOT_PASS}@${CI_SERVER_HOST}/${CI_PROJECT_PATH_SLUG}.git | |
- git config --global user.email '[email protected]' | |
- git config --global user.name 'gitlab-bot' | |
script: | |
- cd test/migrations | |
- yarn install | |
- npx graphile-migrate migrate | |
- ./validate-master-on-mr.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"connectionString": "postgres://user:pass@server/migration_validator_primary", | |
"shadowConnectionString": "postgres://user:pass@server/migration_validator_shadow", | |
"rootConnectionString": "postgres://user:pass@server/template1", | |
"migrationsFolder": "../../migrations" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"private": true, | |
"dependencies": { | |
"graphile-migrate": "^0.1.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
COMMIT_FILES=(`ls ../../migrations/committed/*.sql`) | |
COMMIT_FILES_LENGTH=${#COMMIT_FILES[@]} | |
function debug () { | |
echo "validator: $1" | |
} | |
echo "" | |
debug "COMMIT_FILES_LENGTH: $COMMIT_FILES_LENGTH" | |
CURRENT_FILES=(`ls ../../migrations/current/*.sql`) | |
CURRENT_FILES_LENGTH=${#CURRENT_FILES[@]} | |
echo "" | |
debug "CURRENT_FILES_LENGTH: $CURRENT_FILES_LENGTH" | |
echo "" | |
if [[ $CURRENT_FILES_LENGTH -eq 0 ]] | |
then | |
debug "nothing to migrate, all is well and dandy" | |
exit 0 | |
fi | |
debug "npx graphile-migrate commit" | |
npx graphile-migrate commit | |
AFTER_COMMIT_FILES=(`ls ../../migrations/committed/*.sql`) | |
AFTER_COMMIT_FILES_LENGTH=${#AFTER_COMMIT_FILES[@]} | |
if [[ $COMMIT_FILES_LENGTH -eq $AFTER_COMMIT_FILES_LENGTH ]] | |
then | |
debug "no new migrations created, something went wrong" | |
exit 1 | |
fi | |
debug "git add migrations/committed/*.sql" | |
git add ../../migrations/committed/*.sql | |
debug "git remove migrations/current/*.sql" | |
git rm ../../migrations/current/*.sql | |
CURRENT_GIT_COMMIT=`git rev-parse --short HEAD` | |
debug "git commit -m '[skip ci] automatic migrations test and commit ($CURRENT_GIT_COMMIT)'" | |
git commit -m "[skip ci] automatic migrations test and commit ($CURRENT_GIT_COMMIT)" | |
debug "git push --follow-tags origin HEAD:master" | |
git push --follow-tags origin HEAD:master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on Discord discussion at http://discord.gg/graphile
Based in this, and that we have a GitLab CE edition without "merge trains" or fancy stuff. I created the following files to automatically test, commit and merge the migration files to our master repository.
Folder structure:
Install steps:
migration_validator_primary
+migration_validator_shadow
databases to a server what your GitLab can access.GITLAB_BOT_PASS
orGITLAB_BOT_TOKEN
in your GitLab variables (set it also to masked).gmrc
settingsTesting: