-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bugfix: Entrypoint created citext ext in wrong db
When using entrypoint-test.sh, the CITEXT extension ended up being created in the regular mreg database, when it needs to be created in the template1 database so it propagates to the temporary database created by manage.py test.
- Loading branch information
1 parent
1195e64
commit b73ed70
Showing
2 changed files
with
24 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/bin/sh | ||
set -e | ||
cd /app | ||
./manage.py create_citext_extension | ||
./manage.py create_citext_extension --database template1 | ||
./manage.py test --noinput --failfast |
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,38 @@ | ||
from django.core.management.base import BaseCommand, CommandError | ||
from django.db import connection | ||
from sys import stdout | ||
from django.conf import settings | ||
from psycopg2 import connect | ||
|
||
class Command(BaseCommand): | ||
help = 'Create the CITEXT extension in the database.' | ||
|
||
def handle(self, *args, **kwargs): | ||
def add_arguments(self, parser): | ||
# optional argument | ||
parser.add_argument( | ||
'--database', | ||
type=str, | ||
help='Database name', | ||
) | ||
|
||
def handle(self, *args, **options): | ||
stdout.write("Attempting to create the CITEXT extension in the database...\n") | ||
stdout.flush() | ||
try: | ||
with connection.cursor() as cursor: | ||
con = connection | ||
if options['database']: | ||
stdout.write(f"Connecting to database {options['database']}\n") | ||
stdout.flush() | ||
con = connect(host=settings.DATABASES['default']['HOST'], | ||
user=settings.DATABASES['default']['USER'], | ||
password=settings.DATABASES['default']['PASSWORD'], | ||
database=options['database']) | ||
with con.cursor() as cursor: | ||
cursor.execute("CREATE EXTENSION IF NOT EXISTS citext") | ||
stdout.write(cursor.statusmessage+"\n") | ||
stdout.flush() | ||
con.commit() | ||
except Exception as e: | ||
stdout.write(e.__str__()) | ||
stdout.flush() | ||
raise CommandError('Failed to create the CITEXT extension in the database.') |