-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add facilities for managing dev Postgres instances (#354)
This helps the project move away from sqlite and in more of a "we use postgres" mindset with easy to use commands to make onboarding for new devs without docker as easy as possible.
- Loading branch information
Showing
8 changed files
with
396 additions
and
4 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 |
---|---|---|
|
@@ -19,5 +19,6 @@ venv3/ | |
.env | ||
htmlcov/ | ||
.DS_Store | ||
pgdb/ | ||
node_modules/ | ||
.python-version |
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from pathlib import Path | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
|
||
from registration.utils.database import DatabaseStatus, Postgres | ||
|
||
|
||
config_block = """ | ||
DATABASES = {{ | ||
'default': {{ | ||
'ENGINE': 'django.db.backends.postgresql', | ||
'HOST': '{db_path}', | ||
'NAME' : '{db_name}', | ||
}} | ||
}} | ||
""" | ||
|
||
config_block_with_test = """ | ||
DATABASES = {{ | ||
'default': {{ | ||
'ENGINE': 'django.db.backends.postgresql', | ||
'HOST': '{db_path}', | ||
'NAME' : '{db_name}', | ||
'TEST' : {{ | ||
'NAME' : '{test_db_name}', | ||
}} | ||
}} | ||
}} | ||
""" | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Creates a development postgresql database." | ||
|
||
def add_arguments(self, parser): | ||
base_dir = Path(settings.BASE_DIR) | ||
db_dir = (base_dir / "pgdb").absolute() | ||
|
||
parser.add_argument( | ||
"--db-path", | ||
type=str, | ||
default=str(db_dir), | ||
help=f"Path where the postgresql database will be created, default {db_dir}.", | ||
) | ||
parser.add_argument( | ||
"--db-name", | ||
type=str, | ||
default="apis_dev", | ||
help="The database name to create, default apis_dev.", | ||
) | ||
parser.add_argument( | ||
"--test-db-name", | ||
type=str, | ||
default="apis_test", | ||
help="The test database name to create, default apis_test.", | ||
) | ||
parser.add_argument( | ||
"--skip-test-db", | ||
type=bool, | ||
default=False, | ||
help="Whether or not to skip creating the test database, default False.", | ||
) | ||
parser.add_argument( | ||
"--silent", | ||
type=bool, | ||
default=False, | ||
help="Whether to suppress configuration information, default False." | ||
) | ||
|
||
def handle(self, *args, **options): | ||
postgres = Postgres(options["db_path"]) | ||
|
||
status = postgres.get_status() | ||
|
||
if status in (DatabaseStatus.UNKNOWN, DatabaseStatus.INVALID_DIRECTORY): | ||
postgres.delete() | ||
|
||
status = DatabaseStatus.DOES_NOT_EXIST | ||
|
||
if status in (DatabaseStatus.DOES_NOT_EXIST, DatabaseStatus.DIRECTORY_EMPTY): | ||
postgres.init() | ||
|
||
status = DatabaseStatus.STOPPED | ||
|
||
db_name = options["db_name"] | ||
postgres.create_db(db_name) | ||
|
||
skip_test_db = options["skip_test_db"] | ||
test_db_name = options["test_db_name"] | ||
if test_db_name and not skip_test_db: | ||
postgres.create_db(test_db_name) | ||
|
||
if options["silent"]: | ||
return | ||
|
||
if test_db_name and not skip_test_db: | ||
config_text = config_block_with_test.format( | ||
db_path=postgres.db_path, | ||
db_name=db_name, | ||
test_db_name=test_db_name, | ||
) | ||
else: | ||
config_text = config_block.format( | ||
db_path=postgres.db_path, | ||
db_name=db_name, | ||
) | ||
|
||
print(f"Created database {db_name} at {postgres.db_path}") | ||
print("Add the following to your settings.py") | ||
print(config_text) |
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,35 @@ | ||
from pathlib import Path | ||
import sys | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
|
||
from registration.utils.database import DatabaseStatus, Postgres | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Starts the development postgresql database." | ||
|
||
def add_arguments(self, parser): | ||
base_dir = Path(settings.BASE_DIR) | ||
db_dir = (base_dir / "pgdb").absolute() | ||
|
||
parser.add_argument( | ||
"--db-path", | ||
type=str, | ||
default=str(db_dir), | ||
help=f"Path where the postgresql database will be stopped, default {db_dir}.", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
postgres = Postgres(options["db_path"]) | ||
|
||
status = postgres.get_status() | ||
if status == DatabaseStatus.STOPPED: | ||
postgres.start() | ||
|
||
print("Postgres server started") | ||
elif status == DatabaseStatus.RUNNING: | ||
print("Postgres server is already running") | ||
else: | ||
print(f"Cannot start Postgres server, invalid status: {status}", file=sys.stderr) |
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,37 @@ | ||
from pathlib import Path | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
|
||
from registration.utils.database import DatabaseStatus, Postgres | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Stops the development postgresql database." | ||
|
||
def add_arguments(self, parser): | ||
base_dir = Path(settings.BASE_DIR) | ||
db_dir = (base_dir / "pgdb").absolute() | ||
|
||
parser.add_argument( | ||
"--db-path", | ||
type=str, | ||
default=str(db_dir), | ||
help=f"Path where the postgresql database will be stopped, default {db_dir}.", | ||
) | ||
parser.add_argument( | ||
"--delete", | ||
type=bool, | ||
default=False, | ||
help="Whether or not to delete the database after it has been stopped, default False." | ||
) | ||
|
||
def handle(self, *args, **options): | ||
postgres = Postgres(options["db_path"]) | ||
|
||
status = postgres.get_status() | ||
if status == DatabaseStatus.RUNNING: | ||
postgres.stop() | ||
|
||
if options["delete"]: | ||
postgres.delete() |
Empty file.
Oops, something went wrong.