From 4607245acf37b0dbd98e0bac4124620089c43da6 Mon Sep 17 00:00:00 2001 From: CocoByte Date: Tue, 2 Jul 2024 08:58:37 -0600 Subject: [PATCH 01/10] Rebuild DB workflow DRAFT --- .github/workflows/rebuild-db.yaml | 113 ++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 .github/workflows/rebuild-db.yaml diff --git a/.github/workflows/rebuild-db.yaml b/.github/workflows/rebuild-db.yaml new file mode 100644 index 000000000..3c75a5209 --- /dev/null +++ b/.github/workflows/rebuild-db.yaml @@ -0,0 +1,113 @@ +# This workflow can be run from the CLI +# gh workflow run rebuild-db.yaml -f environment=ENVIRONMENT +# OR +# cf run-task getgov-ENVIRONMENT --command 'python manage.py flush' --name flush +# cf run-task getgov-ENVIRONMENT --command 'python manage.py load' --name loaddata + +name: Rebuild database +run-name: Rebuild database for ${{ github.event.inputs.environment }} + +on: + workflow_dispatch: + inputs: + environment: + type: choice + description: Which environment should we flush and re-load data for? + options: + - staging + - development + - ag + - litterbox + - hotgov + - cb + - bob + - meoward + - backup + - ky + - es + - nl + - rh + - za + - gd + - rb + - ko + - ab + - rjm + - dk + +jobs: + connect-to-service: + runs-on: ubuntu-latest + env: + CF_USERNAME: CF_${{ github.event.inputs.environment }}_USERNAME + CF_PASSWORD: CF_${{ github.event.inputs.environment }}_PASSWORD + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Cloud Foundry CLI + run: | + sudo apt-get update + sudo apt-get install -y wget + wget -q -O cf-cli.tgz "https://packages.cloudfoundry.org/stable?release=linux64-binary&source=github" + tar -xzf cf-cli.tgz + sudo mv cf /usr/local/bin + + - name: Authenticate to Cloud Foundry + run: | + cf api # Replace with your CF API endpoint + cf auth ${{ secrets.CF_USERNAME }} ${{ secrets.CF_PASSWORD }} + + - name: Target organization and space + run: | + cf target -o cisa-dotgov -s nl + + - name: Connect to service + id: connect + run: | + cf connect-to-service -no-client getgov-nl getgov-nl-database > connection_info.txt + cat connection_info.txt + + - name: Extract connection details + id: extract + run: | + port=$(grep -oP 'port:\s*\K\d+' connection_info.txt) + username=$(grep -oP 'user:\s*\K\w+' connection_info.txt) + broker_name=$(grep -oP 'dbname:\s*\K\w+' connection_info.txt) + echo "::set-output name=port::$port" + echo "::set-output name=username::$username" + echo "::set-output name=broker_name::$broker_name" + + - name: Connect to PostgreSQL + run: | + psql -h localhost -p ${{ steps.extract.outputs.port }} -U ${{ steps.extract.outputs.username }} -d ${{ steps.extract.outputs.broker_name }} + env: + PGPASSWORD: ${{ secrets.PG_PASSWORD }} + + - name: Get table names + id: get_tables + run: | + tables=$(psql -h localhost -p ${{ steps.extract.outputs.port }} -U ${{ steps.extract.outputs.username }} -d ${{ steps.extract.outputs.broker_name }} -c "\dt" -t | awk '{print $3}') + echo "::set-output name=tables::$tables" + + - name: Drop all tables + run: | + for table in ${{ steps.get_tables.outputs.tables }} + do + psql -h localhost -p ${{ steps.extract.outputs.port }} -U ${{ steps.extract.outputs.username }} -d ${{ steps.extract.outputs.broker_name }} -c "DROP TABLE IF EXISTS $table CASCADE;" + done + env: + PGPASSWORD: ${{ secrets.PG_PASSWORD }} + + - name: Migrate + run: | + cf ssh getgov-nl -c "/tmp/lifecycle/shell ./manage.py migrate" + + - name: Run fixtures + run: | + cf ssh getgov-nl -c "/tmp/lifecycle/shell ./manage.py load" + + - name: Create cache table + run: | + cf ssh getgov-nl -c "/tmp/lifecycle/shell ./manage.py createcachetable" From b1e89a651bc65c5316672b9324436e492f44c991 Mon Sep 17 00:00:00 2001 From: CocoByte Date: Tue, 2 Jul 2024 13:10:04 -0600 Subject: [PATCH 02/10] incremental update to test --- .github/workflows/rebuild-db.yaml | 53 ++++++++++++------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/.github/workflows/rebuild-db.yaml b/.github/workflows/rebuild-db.yaml index 3c75a5209..f84a6e0a7 100644 --- a/.github/workflows/rebuild-db.yaml +++ b/.github/workflows/rebuild-db.yaml @@ -1,8 +1,5 @@ # This workflow can be run from the CLI # gh workflow run rebuild-db.yaml -f environment=ENVIRONMENT -# OR -# cf run-task getgov-ENVIRONMENT --command 'python manage.py flush' --name flush -# cf run-task getgov-ENVIRONMENT --command 'python manage.py load' --name loaddata name: Rebuild database run-name: Rebuild database for ${{ github.event.inputs.environment }} @@ -14,7 +11,6 @@ on: type: choice description: Which environment should we flush and re-load data for? options: - - staging - development - ag - litterbox @@ -43,30 +39,23 @@ jobs: CF_PASSWORD: CF_${{ github.event.inputs.environment }}_PASSWORD steps: - - name: Checkout repository - uses: actions/checkout@v2 + # - name: Delete existing data for ${{ github.event.inputs.environment }} + # uses: cloud-gov/cg-cli-tools@main + # with: + # cf_username: ${{ secrets[env.CF_USERNAME] }} + # cf_password: ${{ secrets[env.CF_PASSWORD] }} + # cf_org: cisa-dotgov + # cf_space: ${{ github.event.inputs.environment }} + # cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py flush --no-input' --name flush" - - name: Set up Cloud Foundry CLI - run: | - sudo apt-get update - sudo apt-get install -y wget - wget -q -O cf-cli.tgz "https://packages.cloudfoundry.org/stable?release=linux64-binary&source=github" - tar -xzf cf-cli.tgz - sudo mv cf /usr/local/bin - - - name: Authenticate to Cloud Foundry - run: | - cf api # Replace with your CF API endpoint - cf auth ${{ secrets.CF_USERNAME }} ${{ secrets.CF_PASSWORD }} - - - name: Target organization and space - run: | - cf target -o cisa-dotgov -s nl + # - name: Target organization and space + # run: | + # cf target -o cisa-dotgov -s nl - name: Connect to service id: connect run: | - cf connect-to-service -no-client getgov-nl getgov-nl-database > connection_info.txt + cf connect-to-service -no-client getgov-${{ github.event.inputs.environment }} getgov-${{ github.event.inputs.environment }}-database > connection_info.txt cat connection_info.txt - name: Extract connection details @@ -100,14 +89,14 @@ jobs: env: PGPASSWORD: ${{ secrets.PG_PASSWORD }} - - name: Migrate - run: | - cf ssh getgov-nl -c "/tmp/lifecycle/shell ./manage.py migrate" + # - name: Migrate + # run: | + # cf ssh getgov-${{ github.event.inputs.environment }} -c "/tmp/lifecycle/shell ./manage.py migrate" - - name: Run fixtures - run: | - cf ssh getgov-nl -c "/tmp/lifecycle/shell ./manage.py load" + # - name: Run fixtures + # run: | + # cf ssh getgov-${{ github.event.inputs.environment }} -c "/tmp/lifecycle/shell ./manage.py load" - - name: Create cache table - run: | - cf ssh getgov-nl -c "/tmp/lifecycle/shell ./manage.py createcachetable" + # - name: Create cache table + # run: | + # cf ssh getgov-${{ github.event.inputs.environment }} -c "/tmp/lifecycle/shell ./manage.py createcachetable" From a4c06b9cdf8a14b1e0e18e02d3a180192c65705f Mon Sep 17 00:00:00 2001 From: CocoByte Date: Fri, 19 Jul 2024 18:26:00 -0600 Subject: [PATCH 03/10] added drop table script --- .../management/commands/drop_tables.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/registrar/management/commands/drop_tables.py diff --git a/src/registrar/management/commands/drop_tables.py b/src/registrar/management/commands/drop_tables.py new file mode 100644 index 000000000..8ef1e9584 --- /dev/null +++ b/src/registrar/management/commands/drop_tables.py @@ -0,0 +1,39 @@ +import logging +from django.conf import settings +from django.core.management import BaseCommand +from django.apps import apps +from django.db import connection, transaction + +from registrar.management.commands.utility.terminal_helper import TerminalHelper + +logger = logging.getLogger(__name__) + + +class Command(BaseCommand): + help = 'Drops all tables in the database' + + def handle(self, **options): + """Delete all rows from a list of tables""" + + if settings.IS_PRODUCTION: + logger.error("drop_tables cannot be run in production") + return + + self.print_tables() + logger.info(self.style.WARNING('Dropping all tables...')) + with connection.cursor() as cursor: + cursor.execute("DROP SCHEMA public CASCADE;") + cursor.execute("CREATE SCHEMA public;") + logger.info(self.style.SUCCESS('All tables dropped.')) + + def print_tables(self): + logger.info(self.style.WARNING('Fetching table names...')) + with connection.cursor() as cursor: + cursor.execute("SELECT table_name FROM information_schema.tables WHERE table_schema='public'") + table_names = cursor.fetchall() + if table_names: + logger.info(self.style.NOTICE('Tables in the database:')) + for name in table_names: + logger.info(name[0]) + else: + logger.info(self.style.WARNING('No tables found.')) \ No newline at end of file From 9b079811c8a489c6f74f990742c75fefe11cde91 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:31:39 -0600 Subject: [PATCH 04/10] Update drop_tables.py --- .../management/commands/drop_tables.py | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/registrar/management/commands/drop_tables.py b/src/registrar/management/commands/drop_tables.py index 8ef1e9584..1a5a40ee0 100644 --- a/src/registrar/management/commands/drop_tables.py +++ b/src/registrar/management/commands/drop_tables.py @@ -19,21 +19,20 @@ class Command(BaseCommand): logger.error("drop_tables cannot be run in production") return - self.print_tables() logger.info(self.style.WARNING('Dropping all tables...')) - with connection.cursor() as cursor: - cursor.execute("DROP SCHEMA public CASCADE;") - cursor.execute("CREATE SCHEMA public;") - logger.info(self.style.SUCCESS('All tables dropped.')) - - def print_tables(self): - logger.info(self.style.WARNING('Fetching table names...')) with connection.cursor() as cursor: cursor.execute("SELECT table_name FROM information_schema.tables WHERE table_schema='public'") table_names = cursor.fetchall() if table_names: - logger.info(self.style.NOTICE('Tables in the database:')) - for name in table_names: - logger.info(name[0]) + try: + logger.info(self.style.NOTICE('Dropping tables in the database:')) + for name in table_names: + name_as_str = name[0] + logger.info(f"Dropping {name_as_str}") + cursor.execute(f"DROP TABLE {name_as_str} CASCADE;") + except Exception as err: + logger.error(f"Could not drop tables from DB: {err}") + else: + logger.info(self.style.SUCCESS('All tables dropped.')) else: - logger.info(self.style.WARNING('No tables found.')) \ No newline at end of file + logger.info(self.style.WARNING('No tables found.')) From 9b65879a4f6da779d707dacda55f28d6440673b1 Mon Sep 17 00:00:00 2001 From: CocoByte Date: Mon, 22 Jul 2024 16:38:25 -0600 Subject: [PATCH 05/10] updated workflow --- .github/workflows/rebuild-db.yaml | 95 ++++++++++++------------------- 1 file changed, 35 insertions(+), 60 deletions(-) diff --git a/.github/workflows/rebuild-db.yaml b/.github/workflows/rebuild-db.yaml index f84a6e0a7..fe47b43a8 100644 --- a/.github/workflows/rebuild-db.yaml +++ b/.github/workflows/rebuild-db.yaml @@ -31,72 +31,47 @@ on: - rjm - dk + jobs: - connect-to-service: + reset-db: runs-on: ubuntu-latest env: CF_USERNAME: CF_${{ github.event.inputs.environment }}_USERNAME CF_PASSWORD: CF_${{ github.event.inputs.environment }}_PASSWORD - steps: - # - name: Delete existing data for ${{ github.event.inputs.environment }} - # uses: cloud-gov/cg-cli-tools@main - # with: - # cf_username: ${{ secrets[env.CF_USERNAME] }} - # cf_password: ${{ secrets[env.CF_PASSWORD] }} - # cf_org: cisa-dotgov - # cf_space: ${{ github.event.inputs.environment }} - # cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py flush --no-input' --name flush" + - name: Drop Tables for ${{ github.event.inputs.environment }} + uses: cloud-gov/cg-cli-tools@main + with: + cf_username: ${{ secrets[env.CF_USERNAME] }} + cf_password: ${{ secrets[env.CF_PASSWORD] }} + cf_org: cisa-dotgov + cf_space: ${{ github.event.inputs.environment }} + cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py drop_tables --no-input' --name flush" + + - name: Run Django migrations for ${{ github.event.inputs.environment }} + uses: cloud-gov/cg-cli-tools@main + with: + cf_username: ${{ secrets[env.CF_USERNAME] }} + cf_password: ${{ secrets[env.CF_PASSWORD] }} + cf_org: cisa-dotgov + cf_space: ${{ github.event.inputs.environment }} + cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py migrate' --name migrate" - # - name: Target organization and space - # run: | - # cf target -o cisa-dotgov -s nl + - name: Run fixtures for ${{ github.event.inputs.environment }} + uses: cloud-gov/cg-cli-tools@main + with: + cf_username: ${{ secrets[env.CF_USERNAME] }} + cf_password: ${{ secrets[env.CF_PASSWORD] }} + cf_org: cisa-dotgov + cf_space: ${{ github.event.inputs.environment }} + cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py load' --name load" - - name: Connect to service - id: connect - run: | - cf connect-to-service -no-client getgov-${{ github.event.inputs.environment }} getgov-${{ github.event.inputs.environment }}-database > connection_info.txt - cat connection_info.txt + - name: Create cache table for ${{ github.event.inputs.environment }} + uses: cloud-gov/cg-cli-tools@main + with: + cf_username: ${{ secrets[env.CF_USERNAME] }} + cf_password: ${{ secrets[env.CF_PASSWORD] }} + cf_org: cisa-dotgov + cf_space: ${{ github.event.inputs.environment }} + cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py createcachetable' --name createcachetable" - - name: Extract connection details - id: extract - run: | - port=$(grep -oP 'port:\s*\K\d+' connection_info.txt) - username=$(grep -oP 'user:\s*\K\w+' connection_info.txt) - broker_name=$(grep -oP 'dbname:\s*\K\w+' connection_info.txt) - echo "::set-output name=port::$port" - echo "::set-output name=username::$username" - echo "::set-output name=broker_name::$broker_name" - - - name: Connect to PostgreSQL - run: | - psql -h localhost -p ${{ steps.extract.outputs.port }} -U ${{ steps.extract.outputs.username }} -d ${{ steps.extract.outputs.broker_name }} - env: - PGPASSWORD: ${{ secrets.PG_PASSWORD }} - - - name: Get table names - id: get_tables - run: | - tables=$(psql -h localhost -p ${{ steps.extract.outputs.port }} -U ${{ steps.extract.outputs.username }} -d ${{ steps.extract.outputs.broker_name }} -c "\dt" -t | awk '{print $3}') - echo "::set-output name=tables::$tables" - - - name: Drop all tables - run: | - for table in ${{ steps.get_tables.outputs.tables }} - do - psql -h localhost -p ${{ steps.extract.outputs.port }} -U ${{ steps.extract.outputs.username }} -d ${{ steps.extract.outputs.broker_name }} -c "DROP TABLE IF EXISTS $table CASCADE;" - done - env: - PGPASSWORD: ${{ secrets.PG_PASSWORD }} - - # - name: Migrate - # run: | - # cf ssh getgov-${{ github.event.inputs.environment }} -c "/tmp/lifecycle/shell ./manage.py migrate" - - # - name: Run fixtures - # run: | - # cf ssh getgov-${{ github.event.inputs.environment }} -c "/tmp/lifecycle/shell ./manage.py load" - - # - name: Create cache table - # run: | - # cf ssh getgov-${{ github.event.inputs.environment }} -c "/tmp/lifecycle/shell ./manage.py createcachetable" From d4f26b8d061f162dddd1cdcae28b6276770d1fb3 Mon Sep 17 00:00:00 2001 From: CocoByte Date: Wed, 24 Jul 2024 14:18:12 -0600 Subject: [PATCH 06/10] linted and corrected command --- .github/workflows/rebuild-db.yaml | 2 +- src/registrar/management/commands/drop_tables.py | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/rebuild-db.yaml b/.github/workflows/rebuild-db.yaml index fe47b43a8..3232bbe96 100644 --- a/.github/workflows/rebuild-db.yaml +++ b/.github/workflows/rebuild-db.yaml @@ -46,7 +46,7 @@ jobs: cf_password: ${{ secrets[env.CF_PASSWORD] }} cf_org: cisa-dotgov cf_space: ${{ github.event.inputs.environment }} - cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py drop_tables --no-input' --name flush" + cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py drop_tables' --name flush" - name: Run Django migrations for ${{ github.event.inputs.environment }} uses: cloud-gov/cg-cli-tools@main diff --git a/src/registrar/management/commands/drop_tables.py b/src/registrar/management/commands/drop_tables.py index 1a5a40ee0..e4d18f576 100644 --- a/src/registrar/management/commands/drop_tables.py +++ b/src/registrar/management/commands/drop_tables.py @@ -1,10 +1,6 @@ import logging from django.conf import settings from django.core.management import BaseCommand -from django.apps import apps -from django.db import connection, transaction - -from registrar.management.commands.utility.terminal_helper import TerminalHelper logger = logging.getLogger(__name__) From 7297446a82a4747d31a946f8b23a27bd156729b4 Mon Sep 17 00:00:00 2001 From: CocoByte Date: Wed, 31 Jul 2024 22:21:27 -0600 Subject: [PATCH 07/10] Remove abandoned rebuild-db workflow and add instructions for reset-db workflow --- .github/workflows/rebuild-db.yaml | 77 ------------------- .github/workflows/reset-db.yaml | 12 +++ .../management/commands/drop_tables.py | 34 -------- 3 files changed, 12 insertions(+), 111 deletions(-) delete mode 100644 .github/workflows/rebuild-db.yaml delete mode 100644 src/registrar/management/commands/drop_tables.py diff --git a/.github/workflows/rebuild-db.yaml b/.github/workflows/rebuild-db.yaml deleted file mode 100644 index 3232bbe96..000000000 --- a/.github/workflows/rebuild-db.yaml +++ /dev/null @@ -1,77 +0,0 @@ -# This workflow can be run from the CLI -# gh workflow run rebuild-db.yaml -f environment=ENVIRONMENT - -name: Rebuild database -run-name: Rebuild database for ${{ github.event.inputs.environment }} - -on: - workflow_dispatch: - inputs: - environment: - type: choice - description: Which environment should we flush and re-load data for? - options: - - development - - ag - - litterbox - - hotgov - - cb - - bob - - meoward - - backup - - ky - - es - - nl - - rh - - za - - gd - - rb - - ko - - ab - - rjm - - dk - - -jobs: - reset-db: - runs-on: ubuntu-latest - env: - CF_USERNAME: CF_${{ github.event.inputs.environment }}_USERNAME - CF_PASSWORD: CF_${{ github.event.inputs.environment }}_PASSWORD - steps: - - name: Drop Tables for ${{ github.event.inputs.environment }} - uses: cloud-gov/cg-cli-tools@main - with: - cf_username: ${{ secrets[env.CF_USERNAME] }} - cf_password: ${{ secrets[env.CF_PASSWORD] }} - cf_org: cisa-dotgov - cf_space: ${{ github.event.inputs.environment }} - cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py drop_tables' --name flush" - - - name: Run Django migrations for ${{ github.event.inputs.environment }} - uses: cloud-gov/cg-cli-tools@main - with: - cf_username: ${{ secrets[env.CF_USERNAME] }} - cf_password: ${{ secrets[env.CF_PASSWORD] }} - cf_org: cisa-dotgov - cf_space: ${{ github.event.inputs.environment }} - cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py migrate' --name migrate" - - - name: Run fixtures for ${{ github.event.inputs.environment }} - uses: cloud-gov/cg-cli-tools@main - with: - cf_username: ${{ secrets[env.CF_USERNAME] }} - cf_password: ${{ secrets[env.CF_PASSWORD] }} - cf_org: cisa-dotgov - cf_space: ${{ github.event.inputs.environment }} - cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py load' --name load" - - - name: Create cache table for ${{ github.event.inputs.environment }} - uses: cloud-gov/cg-cli-tools@main - with: - cf_username: ${{ secrets[env.CF_USERNAME] }} - cf_password: ${{ secrets[env.CF_PASSWORD] }} - cf_org: cisa-dotgov - cf_space: ${{ github.event.inputs.environment }} - cf_command: "run-task getgov-${{ github.event.inputs.environment }} --command 'python manage.py createcachetable' --name createcachetable" - diff --git a/.github/workflows/reset-db.yaml b/.github/workflows/reset-db.yaml index 49e4b5e5f..f4af8bc12 100644 --- a/.github/workflows/reset-db.yaml +++ b/.github/workflows/reset-db.yaml @@ -4,6 +4,18 @@ # cf run-task getgov-ENVIRONMENT --command 'python manage.py flush' --name flush # cf run-task getgov-ENVIRONMENT --command 'python manage.py load' --name loaddata +# IMPORTANT NOTE: +# When running this workflow, it is important to grab the right set of migrations +# for your target sandbox. For example, if you just recently pushed new migrations +# from SOURCE_BRANCH to TARGET_SANDBOX, you must use that same SOURCE_BRANCH when +# running this workflow (NOT main) or else your sandbox will generate an error +# due to a db model mismatch. +# +# This means in the Github workflow settings you would select the following inputs: +# +# "Use workflow from": SOURCE_BRANCH +# "Which environment should we flush and re-load data for?"": TARGET_SANDBOX + name: Reset database run-name: Reset database for ${{ github.event.inputs.environment }} diff --git a/src/registrar/management/commands/drop_tables.py b/src/registrar/management/commands/drop_tables.py deleted file mode 100644 index e4d18f576..000000000 --- a/src/registrar/management/commands/drop_tables.py +++ /dev/null @@ -1,34 +0,0 @@ -import logging -from django.conf import settings -from django.core.management import BaseCommand - -logger = logging.getLogger(__name__) - - -class Command(BaseCommand): - help = 'Drops all tables in the database' - - def handle(self, **options): - """Delete all rows from a list of tables""" - - if settings.IS_PRODUCTION: - logger.error("drop_tables cannot be run in production") - return - - logger.info(self.style.WARNING('Dropping all tables...')) - with connection.cursor() as cursor: - cursor.execute("SELECT table_name FROM information_schema.tables WHERE table_schema='public'") - table_names = cursor.fetchall() - if table_names: - try: - logger.info(self.style.NOTICE('Dropping tables in the database:')) - for name in table_names: - name_as_str = name[0] - logger.info(f"Dropping {name_as_str}") - cursor.execute(f"DROP TABLE {name_as_str} CASCADE;") - except Exception as err: - logger.error(f"Could not drop tables from DB: {err}") - else: - logger.info(self.style.SUCCESS('All tables dropped.')) - else: - logger.info(self.style.WARNING('No tables found.')) From ffe2ddebb4f831fe8d701b42be72a7436c9566a6 Mon Sep 17 00:00:00 2001 From: CocoByte Date: Wed, 31 Jul 2024 22:33:37 -0600 Subject: [PATCH 08/10] wording adjustment --- .github/workflows/reset-db.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/reset-db.yaml b/.github/workflows/reset-db.yaml index f4af8bc12..50c83895a 100644 --- a/.github/workflows/reset-db.yaml +++ b/.github/workflows/reset-db.yaml @@ -5,11 +5,11 @@ # cf run-task getgov-ENVIRONMENT --command 'python manage.py load' --name loaddata # IMPORTANT NOTE: -# When running this workflow, it is important to grab the right set of migrations +# When running this workflow, it is important to use the right source branch # for your target sandbox. For example, if you just recently pushed new migrations # from SOURCE_BRANCH to TARGET_SANDBOX, you must use that same SOURCE_BRANCH when # running this workflow (NOT main) or else your sandbox will generate an error -# due to a db model mismatch. +# due to a db mismatch. # # This means in the Github workflow settings you would select the following inputs: # From 80a39401339c18b4f039b55091ec8f435ccbf721 Mon Sep 17 00:00:00 2001 From: CocoByte Date: Mon, 5 Aug 2024 12:28:32 -0600 Subject: [PATCH 09/10] Removed instructions --- .github/workflows/reset-db.yaml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/.github/workflows/reset-db.yaml b/.github/workflows/reset-db.yaml index 50c83895a..49e4b5e5f 100644 --- a/.github/workflows/reset-db.yaml +++ b/.github/workflows/reset-db.yaml @@ -4,18 +4,6 @@ # cf run-task getgov-ENVIRONMENT --command 'python manage.py flush' --name flush # cf run-task getgov-ENVIRONMENT --command 'python manage.py load' --name loaddata -# IMPORTANT NOTE: -# When running this workflow, it is important to use the right source branch -# for your target sandbox. For example, if you just recently pushed new migrations -# from SOURCE_BRANCH to TARGET_SANDBOX, you must use that same SOURCE_BRANCH when -# running this workflow (NOT main) or else your sandbox will generate an error -# due to a db mismatch. -# -# This means in the Github workflow settings you would select the following inputs: -# -# "Use workflow from": SOURCE_BRANCH -# "Which environment should we flush and re-load data for?"": TARGET_SANDBOX - name: Reset database run-name: Reset database for ${{ github.event.inputs.environment }} From 9a531d4cec8ca264976bd2def71558a4831371ad Mon Sep 17 00:00:00 2001 From: CocoByte Date: Mon, 5 Aug 2024 12:46:27 -0600 Subject: [PATCH 10/10] Updated migration-troubleshooting MD to have a note on usage of reset-db workflow for migration troubleshooting --- docs/developer/migration-troubleshooting.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/developer/migration-troubleshooting.md b/docs/developer/migration-troubleshooting.md index 22a02503d..395d23ee8 100644 --- a/docs/developer/migration-troubleshooting.md +++ b/docs/developer/migration-troubleshooting.md @@ -30,7 +30,19 @@ You should end up with `40_some_migration_from_main`, `41_local_migration` Alternatively, assuming that the conflicting migrations are not dependent on each other, you can manually edit the migration file such that your new migration is incremented by one (file name, and definition inside the file) but this approach is not recommended. -### Scenario 2: Conflicting migrations on sandbox +### Scenario 2: Conflicting migrations on sandbox (can be fixed with GH workflow) +A 500 error on a sanbox after a fresh push usually indicates a migration issue. +Most of the time, these migration issues can easily be fixed by simply running the +"reset-db" workflow in Github. + +For the workflow, select the following inputs before running it; +"Use workflow from": Branch-main +"Which environment should we flush and re-load data for?" + +This is not a cure-all since it simply flushes and re-runs migrations against your sandbox. +If running this workflow does not solve your issue, proceed examining the scenarios below. + +### Scenario 3: Conflicting migrations on sandbox (cannot be fixed with GH workflow) This occurs when the logs return the following: >Conflicting migrations detected; multiple leaf nodes in the migration graph: (0040_example, 0041_example in base).