Skip to content

Commit

Permalink
Bugfix: Entrypoint created citext ext in wrong db
Browse files Browse the repository at this point in the history
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
oyvindhagberg committed Mar 29, 2023
1 parent 1195e64 commit b73ed70
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion entrypoint-test.sh
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
25 changes: 23 additions & 2 deletions mreg/management/commands/create_citext_extension.py
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.')

0 comments on commit b73ed70

Please sign in to comment.