-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a separate PG DB for Census data (#2573)
* Add bucket and DB * Lint * Formatting * Duplicate changes to compose-web * Fix naming errors * Try separate database * WIp * Get config test to pass * Make same changes in docker-compose-web --------- Co-authored-by: Alex Steel <[email protected]>
- Loading branch information
1 parent
da1d340
commit f7b8197
Showing
9 changed files
with
104 additions
and
20 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
app_name = "census_to_gsafac" | ||
app_name = "census_historical_migration" | ||
db_name = "census-to-gsafac-db" | ||
|
||
|
||
class DBRouter: | ||
def db_for_read(self, model, **hints): | ||
if model._meta.app_label == app_name: | ||
return db_name | ||
return None | ||
return "default" | ||
|
||
def db_for_write(self, model, **hints): | ||
return self.db_for_read(model, hints) | ||
if model._meta.app_label == app_name: | ||
return db_name | ||
return "default" | ||
|
||
def allow_migrate(self, db, app_label, model_name=None, **hints): | ||
if app_label == app_name: | ||
return db == db_name | ||
return False | ||
return db == "default" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,10 +173,10 @@ | |
), | ||
"census-to-gsafac-db": env.dj_db_url( | ||
"DATABASE_URL_CENSUS_TO_GSAFAC_DB", | ||
default="postgres://postgres:[email protected]/census-to-gsafac-db", | ||
default="postgres://postgres:[email protected]:5433/backend", | ||
), | ||
} | ||
|
||
DATABASE_ROUTERS = ["census_historical_migration.routers.DBRouter"] | ||
POSTGREST = { | ||
"URL": env.str("POSTGREST_URL", "http://api:3000"), | ||
"LOCAL": env.str("POSTGREST_URL", "http://api:3000"), | ||
|
@@ -230,6 +230,7 @@ | |
|
||
STATIC_URL = "/static/" | ||
|
||
|
||
# Environment specific configurations | ||
DEBUG = False | ||
if ENVIRONMENT not in ["DEVELOPMENT", "PREVIEW", "STAGING", "PRODUCTION"]: | ||
|
@@ -400,16 +401,10 @@ | |
# Will not be enabled in cloud environments | ||
DISABLE_AUTH = False | ||
|
||
# Remove once all Census data has been migrated | ||
# Add these as env vars, look at the bucket for values | ||
AWS_CENSUS_ACCESS_KEY_ID = secret("AWS_CENSUS_ACCESS_KEY_ID", "") | ||
AWS_CENSUS_SECRET_ACCESS_KEY = secret("AWS_CENSUS_SECRET_ACCESS_KEY", "") | ||
AWS_CENSUS_STORAGE_BUCKET_NAME = secret("AWS_CENSUS_STORAGE_BUCKET_NAME", "") | ||
AWS_S3_CENSUS_REGION_NAME = secret("AWS_S3_CENSUS_REGION_NAME", "") | ||
|
||
|
||
ADMIN_URL = "admin/" | ||
|
||
|
||
# Default primary key field type | ||
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from django.test import TestCase | ||
from django.conf import settings | ||
|
||
|
||
import boto3 | ||
|
||
from census_historical_migration.models import ELECAUDITHEADER as Gen | ||
from audit.models import SingleAuditChecklist | ||
|
||
CENSUS_DB = [k for k in settings.DATABASES.keys() if "default" != k][0] | ||
|
||
|
||
class SettingsTestCase(TestCase): | ||
databases = {"default", CENSUS_DB} | ||
|
||
def test_models_are_in_appropriate_db(self): | ||
sacs = SingleAuditChecklist.objects.all() | ||
self.assertEqual(len(sacs), 0) | ||
try: | ||
gens = Gen.objects.using("default").all() | ||
except Exception: | ||
self.assertEqual(1, 1) | ||
gens = Gen.objects.using(CENSUS_DB).all() | ||
self.assertEqual(len(gens), 0) | ||
|
||
def test_private_s3(self): | ||
try: | ||
s3_client = boto3.client( | ||
"s3", | ||
aws_access_key_id=settings.AWS_PRIVATE_ACCESS_KEY_ID, | ||
aws_secret_access_key=settings.AWS_PRIVATE_SECRET_ACCESS_KEY, | ||
endpoint_url=settings.AWS_S3_ENDPOINT_URL, | ||
) | ||
self.assertIsNotNone(s3_client) | ||
items = s3_client.list_objects( | ||
Bucket=settings.AWS_PRIVATE_STORAGE_BUCKET_NAME, | ||
) | ||
self.assertIsNotNone(items) | ||
except Exception as e: | ||
self.fail(f"Unexpected exception: {e}") | ||
|
||
def test_c2g_s3(self): | ||
try: | ||
s3_client = boto3.client( | ||
"s3", | ||
aws_access_key_id=settings.AWS_PRIVATE_ACCESS_KEY_ID, | ||
aws_secret_access_key=settings.AWS_PRIVATE_SECRET_ACCESS_KEY, | ||
endpoint_url=settings.AWS_S3_ENDPOINT_URL, | ||
) | ||
self.assertIsNotNone(s3_client) | ||
items = s3_client.list_objects( | ||
Bucket=settings.AWS_CENSUS_TO_GSAFAC_BUCKET_NAME, | ||
) | ||
self.assertIsNotNone(items) | ||
except Exception as e: | ||
self.fail(f"Unexpected exception: {e}") |
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
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
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
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