Skip to content

Commit ed38802

Browse files
committed
feat(commands): add management command for database setup
- creates an admin connection - resets database users and databases - validates the database configuration - checks if a user/database exists - creates a user/database - checks schema permissions - runs migrations - ensures a superuser exists
1 parent 9d70d3f commit ed38802

File tree

9 files changed

+1077
-1
lines changed

9 files changed

+1077
-1
lines changed

pems/core/management/__init__.py

Whitespace-only changes.

pems/core/management/commands/__init__.py

Whitespace-only changes.

pems/core/management/commands/ensure_db.py

Lines changed: 309 additions & 0 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ preserve_blank_lines = true
4848
use_gitignore = true
4949

5050
[tool.pytest.ini_options]
51-
DJANGO_SETTINGS_MODULE = "pems.settings"
51+
DJANGO_SETTINGS_MODULE = "tests.pytest.settings"
5252

5353
[tool.setuptools.packages.find]
5454
include = ["pems*"]

tests/pytest/pems/core/management/__init__.py

Whitespace-only changes.

tests/pytest/pems/core/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import psycopg
2+
import pytest
3+
4+
5+
@pytest.fixture
6+
def mock_psycopg_cursor(mocker):
7+
cursor = mocker.MagicMock(spec=psycopg.Cursor)
8+
cursor.fetchone.return_value = None
9+
cursor.close = mocker.MagicMock()
10+
return cursor
11+
12+
13+
@pytest.fixture
14+
def mock_admin_connection(mocker, mock_psycopg_cursor):
15+
connection = mocker.MagicMock(spec=psycopg.Connection)
16+
connection.cursor.return_value = mock_psycopg_cursor
17+
connection.closed = False
18+
19+
def mock_close():
20+
connection.closed = True
21+
22+
connection.close = mock_close
23+
return connection
24+
25+
26+
@pytest.fixture
27+
def mock_os_environ(mocker):
28+
env_dict = {}
29+
mocker.patch("os.environ", env_dict)
30+
return env_dict
31+
32+
33+
@pytest.fixture
34+
def mock_psycopg_connect(mocker):
35+
return mocker.patch("psycopg.connect")

0 commit comments

Comments
 (0)