Skip to content

Commit f107afe

Browse files
florimondmancaNiall McAndrew
andauthored
Fix migration 0004 when run against a non default database (updated) (#215)
* Fix migrations when run against a non default database. * Change test DB to SQLite as per PR review. Co-authored-by: Florimond Manca <florimond.manca@protonmail.com> * Format Co-authored-by: Niall McAndrew <niall@mediasuite.co.nz>
1 parent 6f76d19 commit f107afe

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

src/rest_framework_api_key/migrations/0004_prefix_hashed_key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
def populate_prefix_hashed_key(apps, schema_editor) -> None: # type: ignore
1111
model = apps.get_model(APP_NAME, MODEL_NAME)
1212

13-
for api_key in model.objects.all():
13+
for api_key in model.objects.using(schema_editor.connection.alias).all():
1414
prefix, _, hashed_key = api_key.id.partition(".")
1515
api_key.prefix = prefix
1616
api_key.hashed_key = hashed_key

test_project/heroes/migrations/0002_prefix_hashed_key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
def populate_prefix_hashed_key(apps, schema_editor): # type: ignore
1111
model = apps.get_model(APP_NAME, MODEL_NAME)
1212

13-
for api_key in model.objects.all():
13+
for api_key in model.objects.using(schema_editor.connection.alias).all():
1414
prefix, _, hashed_key = api_key.id.partition(".")
1515
api_key.prefix = prefix
1616
api_key.hashed_key = hashed_key

test_project/project/settings.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@
5555

5656
# Database
5757

58-
DATABASES = {"default": dj_database_url.config(default="sqlite:///db.sqlite3")}
58+
DATABASES = {
59+
"default": dj_database_url.config(default="sqlite:///db.sqlite3"),
60+
"test": dj_database_url.config(
61+
"TEST_DATABASE_URL", default="sqlite://test.sqlite3"
62+
),
63+
}
5964

6065

6166
# Password validation

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def pytest_configure() -> None:
4545
],
4646
"ROOT_URLCONF": "test_project.project.urls",
4747
"DATABASES": {
48-
"default": dj_database_url.config(default="sqlite://:memory:")
48+
"default": dj_database_url.config(default="sqlite://:memory:"),
49+
"test": dj_database_url.config(default="sqlite://:memory:"),
4950
},
5051
}
5152
)

0 commit comments

Comments
 (0)