Skip to content

Commit

Permalink
Merge pull request #3652 from uselagoon/fix-env-services-drop-index
Browse files Browse the repository at this point in the history
fix: add DropUnique to rollback ES migration
  • Loading branch information
tobybellwood authored Feb 14, 2024
2 parents f443a51 + 3e1c266 commit 85aa0f1
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/test-db-migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Test database migration and rollback

on:
push:
branches:
- 'main'
paths:
- 'services/api/database/**'
- '.github/workflows/test-db-migrations.yml'
tags:
- 'v*.*.*'
pull_request:
branches:
- 'main'
paths:
- 'services/api/database/**'
- '.github/workflows/test-db-migrations.yml'

jobs:
makeup:
runs-on: ubuntu-latest
steps:
-
name: Checkout PR
uses: actions/checkout@v4
if: ${{ github.event_name == 'pull_request' }}
with:
fetch-depth: "0"
ref: ${{ github.event.pull_request.head.sha }}
-
name: Checkout Branch or Tag
uses: actions/checkout@v4
if: ${{ github.event_name != 'pull_request' }}
with:
fetch-depth: "0"
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Build necessary images
run: |
make build
-
name: Bring up a docker compose lagoon
run: |
make up
-
name: Show initial migration logs
run: |
docker compose -p lagoon logs api-init
-
name: Initiate rollback
run: |
docker compose -p lagoon exec api sh -c './node_modules/.bin/knex migrate:rollback --all --cwd /app/services/api/database'
-
name: Reperform initial migration
run: |
docker compose -p lagoon exec api sh -c './node_modules/.bin/knex migrate:latest --cwd /app/services/api/database'
-
name: Remove testing setup
run: |
make down
8 changes: 3 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ services:
- CONSOLE_LOGGING_LEVEL=trace
api-init:
image: ${IMAGE_REPO:-lagoon}/api
command: ./node_modules/.bin/knex migrate:latest --cwd /app/services/api/database
command: >
sh -c "./node_modules/.bin/knex migrate:list --cwd /app/services/api/database
&& ./node_modules/.bin/knex migrate:latest --cwd /app/services/api/database"
volumes:
- ./services/api/database:/app/services/api/database
- ./node-packages:/app/node-packages:delegated
Expand Down Expand Up @@ -195,10 +197,6 @@ services:
environment:
- MINIO_ROOT_USER=minio
- MINIO_ROOT_PASSWORD=minio123
local-registry:
image: ${IMAGE_REPO:-lagoon}/local-registry
ports:
- '5000:5000'
drush-alias:
image: uselagoon/drush-alias:latest
ports:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
exports.up = async function(knex) {
return knex.schema
.raw(`DELETE es1 FROM environment_service es1 INNER JOIN environment_service es2 WHERE es1.id < es2.id AND es1.name = es2.name;`)
.alterTable('environment_service', function (table) {
table.string('type', 300);
table.timestamp('updated').notNullable().defaultTo(knex.fn.now());
Expand All @@ -27,6 +28,7 @@ exports.down = async function(knex) {
table.dropColumn('type');
table.dropColumn('updated');
table.dropColumn('created');
table.dropUnique(['name', 'environment'], 'service_environment');
})
.dropTable('environment_service_container')
};
7 changes: 6 additions & 1 deletion services/api/src/resources/environment/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ export const deleteAllEnvironments: ResolverFn = async (
return 'success';
};

// @deprecated in favor of addOrUpdateEnvironmentService and deleteEnvironmentService, will eventually be removed
export const setEnvironmentServices: ResolverFn = async (
root,
{ input: { environment: environmentId, services } },
Expand All @@ -745,7 +746,11 @@ export const setEnvironmentServices: ResolverFn = async (

await query(sqlClientPool, Sql.deleteServices(environmentId));

for (const service of services) {
// remove any duplicates, since there is no other identifying information related to these duplicates don't matter.
// as this function is also being deprecated its usage over time will eventually drop
// this means removal of duplicates is an acceptable trade off while the transition takes place
var uniq = services.filter((value, index, array) => array.indexOf(value) === index);
for (const service of uniq) {
await query(sqlClientPool, Sql.insertService(environmentId, service));
}

Expand Down

0 comments on commit 85aa0f1

Please sign in to comment.