Skip to content

Commit

Permalink
fix: align db schema everywhere and make it app (#42)
Browse files Browse the repository at this point in the history
* fix: align db schema everywhere and make it app

* fix: add waiting to resume resources
  • Loading branch information
mishraomp authored Jan 8, 2025
1 parent 6a72489 commit 867dae8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
43 changes: 38 additions & 5 deletions .github/workflows/resume-resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,31 @@ jobs:
- name: Resume AWS Resources
shell: bash
run: |
aws application-autoscaling register-scalable-target --service-namespace ecs --resource-id service/ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev/${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev-service --scalable-dimension ecs:service:DesiredCount --min-capacity 1 --max-capacity 2 --no-cli-pager --output json
aws ecs update-service --cluster ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev --service ${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev-service --desired-count 1 --no-cli-pager --output json
DB_CLUSTER_STATUS=$(aws rds describe-db-clusters --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-dev --query 'DBClusters[0].Status' --output text)
if [ "$DB_CLUSTER_STATUS" = "stopped" ]; then
aws rds start-db-cluster --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-dev --no-cli-pager --output json
# wait for the cluster to be available
attempt=1
max_attempts=20
until [[ $(aws rds describe-db-clusters --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-dev --query 'DBClusters[0].Status' --output text) == "available" ]] || [[ $attempt -gt $max_attempts ]]
do
echo "Waiting for DB cluster to be available... Attempt $attempt of $max_attempts"
sleep 60
((attempt++))
done
if [[ $attempt -gt $max_attempts ]]; then
echo "Timeout waiting for DB cluster to become available"
exit 1
fi
echo "DB cluster is now available"
else
echo "DB cluster is not in a stopped state. Current state: $DB_CLUSTER_STATUS"
fi
aws application-autoscaling register-scalable-target --service-namespace ecs --resource-id service/ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev/${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev-service --scalable-dimension ecs:service:DesiredCount --min-capacity 1 --max-capacity 2 --no-cli-pager --output json
aws ecs update-service --cluster ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev --service ${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev-service --desired-count 1 --no-cli-pager --output json
resume-resources-test:
name: Resume Resources Test
environment: test
Expand All @@ -52,11 +69,27 @@ jobs:
- name: Resume AWS Resources
shell: bash
run: |
aws application-autoscaling register-scalable-target --service-namespace ecs --resource-id service/ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test/${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test-service --scalable-dimension ecs:service:DesiredCount --min-capacity 1 --max-capacity 2 --no-cli-pager --output json
aws ecs update-service --cluster ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test --service ${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test-service --desired-count 1 --no-cli-pager --output json
DB_CLUSTER_STATUS=$(aws rds describe-db-clusters --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-test --query 'DBClusters[0].Status' --output text)
if [ "$DB_CLUSTER_STATUS" = "stopped" ]; then
aws rds start-db-cluster --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-test --no-cli-pager --output json
# wait for the cluster to be available
attempt=1
max_attempts=20
until [[ $(aws rds describe-db-clusters --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-test --query 'DBClusters[0].Status' --output text) == "available" ]] || [[ $attempt -gt $max_attempts ]]
do
echo "Waiting for DB cluster to be available... Attempt $attempt of $max_attempts"
sleep 60
((attempt++))
done
if [[ $attempt -gt $max_attempts ]]; then
echo "Timeout waiting for DB cluster to become available"
exit 1
fi
echo "DB cluster is now available"
else
echo "DB cluster is not in a stopped state. Current state: $DB_CLUSTER_STATUS"
fi
fi
aws application-autoscaling register-scalable-target --service-namespace ecs --resource-id service/ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test/${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test-service --scalable-dimension ecs:service:DesiredCount --min-capacity 1 --max-capacity 2 --no-cli-pager --output json
aws ecs update-service --cluster ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test --service ${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test-service --desired-count 1 --no-cli-pager --output json
2 changes: 1 addition & 1 deletion backend/src/prisma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const DB_USER = process.env.POSTGRES_USER || "postgres";
const DB_PWD = encodeURIComponent(process.env.POSTGRES_PASSWORD || "default"); // this needs to be encoded, if the password contains special characters it will break connection string.
const DB_PORT = process.env.POSTGRES_PORT || 5432;
const DB_NAME = process.env.POSTGRES_DATABASE || "postgres";
const DB_SCHEMA = process.env.POSTGRES_SCHEMA || "users";
const DB_SCHEMA = process.env.POSTGRES_SCHEMA || "app";
const dataSourceURL = `postgresql://${DB_USER}:${DB_PWD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?schema=${DB_SCHEMA}&connection_limit=5`;

@Injectable({ scope: Scope.DEFAULT})
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ services:
FLYWAY_USER: *POSTGRES_USER
FLYWAY_PASSWORD: *POSTGRES_PASSWORD
FLYWAY_BASELINE_ON_MIGRATE: true
FLYWAY_DEFAULT_SCHEMA: users
FLYWAY_DEFAULT_SCHEMA: app
depends_on:
database:
condition: service_healthy
Expand All @@ -43,7 +43,7 @@ services:
image: schemaspy/schemaspy:6.2.4
profiles: ["schemaspy"]
container_name: schemaspy
command: -t pgsql11 -db postgres -host database -port 5432 -u postgres -p default -schemas users
command: -t pgsql11 -db postgres -host database -port 5432 -u postgres -p default -schemas app
depends_on:
migrations:
condition: service_completed_successfully
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/api/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ resource "aws_ecs_task_definition" "node_api_task" {
value = var.db_name
},
{
name = "DB_SCHEMA"
name = "POSTGRES_SCHEMA"
value = "${var.db_schema}"
}
,
Expand Down

0 comments on commit 867dae8

Please # to comment.