|
| 1 | +# Github Actions for Automated DB Migrations |
| 2 | + |
| 3 | +This guide will help you set up a Github Action to automatically run database migrations when you push changes to your repository. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | +To set up the Github Action, you will need to have the following: |
| 7 | + - access with admin permissions to the repository you want to set up the action for |
| 8 | + - a database that you want to run migrations on |
| 9 | + |
| 10 | +## Steps |
| 11 | +This guide will walk you through the steps to set up the Github Action for automated database migrations. |
| 12 | +We will provide several types of migrations execution, including: |
| 13 | + - running migrations using SSH access to a remote server |
| 14 | + - running migrations on a remote database using connection strings |
| 15 | + - running migrations on a remote database using Cohere Toolkit's API migrations endpoint |
| 16 | +For each type of migration execution we need to setup Github environment and secrets. |
| 17 | +To seting up the Github environment and secrets, follow the steps below: |
| 18 | +- navigate to your repository on Github |
| 19 | +- click on the `Settings` tab |
| 20 | +- click on the `Environments` menu on the left |
| 21 | +- click on the `New environment` button or select an existing environment(we will use the `Production` environment in this guide) |
| 22 | +- fill in the environment name and click on the `Configure environment` button |
| 23 | +- optionally set rules for the environment, for example, you can set the `Production` environment to be available only for the `main` branch |
| 24 | +- click on the `Add secret` button |
| 25 | + |
| 26 | +### Running Migrations using SSH access to a remote server |
| 27 | +To run migrations using SSH access to a remote server, you will need to set up the following secrets |
| 28 | +- `SSH_PRIVATE_KEY` - the private key to use for SSH authentication |
| 29 | +- `SSH_HOST` - the hostname or IP address of the remote server |
| 30 | +- `SSH_PORT` - the port to use for SSH authentication |
| 31 | +- `SSH_USER_NAME` - the username to use for SSH authentication |
| 32 | +The sample Github Action workflow file is located at `.github/workflows/run_alembic_migrations.yml` |
| 33 | +Set the `SSH_PROJECT_DIR` variable to the path to the project directory on the remote server. See comments in the `run_alembic_migrations.yml` file for more details. |
| 34 | +The action scripts are located at `.github/scripts` directory. The `run_docker_migration.sh` script is used |
| 35 | +to run the migrations using Docker. The `run_src_migration.sh` script is used to run the migrations using the source code. |
| 36 | +If the Toolkit is deployed on a remote server using Docker, please remove `sh $SSH_PROJECT_DIR/.github/scripts/run_src_migration.sh` string from the `run_alembic_migrations.yml` |
| 37 | +If the Toolkit is deployed on a remote server using the source code, please remove `sh $SSH_PROJECT_DIR/.github/scripts/run_docker_migration.sh` string from the `run_alembic_migrations.yml` |
| 38 | +Please note that this is a basic example and you may need to adjust the scripts to fit your specific use case. |
| 39 | + |
| 40 | +### Running Migrations on a remote database using connection strings |
| 41 | +To run migrations on a remote database using connection strings, you will need to set up the following secrets |
| 42 | +In this example, we will use a PostgreSQL database available from the internet. If you are using a private database, you may need to set up a VPN or SSH tunnel to access the database. |
| 43 | +- `MIGRATION_DATABASE_URL` - the connection string to the database. It should be in the format `postgresql+psycopg2://username:password@host:port/database` eg. `postgresql+psycopg2://postgres:postgres@db:5432` |
| 44 | + |
| 45 | +### Running Migrations on a remote database using Cohere Toolkit's API migrations endpoint |
| 46 | +To run migrations on a remote database using Cohere Toolkit's API migrations endpoint, you will need to set up the following secrets |
| 47 | +- `MIGRATION_API_ENDPOINT` - the URL of the Cohere Toolkit API migrations endpoint, eg. `https://>Your Cohere Toolkit host name<:>API port, 8000 by default</migrate` |
| 48 | +- `MIGRATION_API_TOKEN` - the API token to use for authentication with the Cohere Toolkit API migrations endpoint. You can get the API token from the Toolkit's `.env` file |
| 49 | + |
| 50 | +Please note that this is a basic example and you may need to adjust the scripts to fit your specific use case. |
| 51 | +If all the secrets above are set up, the remote database migrations type will used as default. |
| 52 | +If some variables are not set, the migration type will be selected based on the set variables. |
| 53 | +If script cant select the migration type(some variables are not set), the script will exit with an error message. |
0 commit comments