Skip to content

Commit 348c9d0

Browse files
committed
Migration tests pass locally
1 parent 118c5c6 commit 348c9d0

File tree

2 files changed

+51
-26
lines changed

2 files changed

+51
-26
lines changed

cli/tests/test_cli.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,34 @@
22

33
from pathlib import Path
44

5-
from typer.testing import CliRunner
6-
75
from macrostrat.utils import override_environment
6+
from pytest import fixture, mark
7+
from typer.testing import CliRunner
88

99
runner = CliRunner()
1010

1111
__here__ = Path(__file__).parent
1212

1313

14-
def test_cli_help():
14+
@fixture
15+
def cfg():
1516
cfg_file = __here__ / "macrostrat.test.toml"
1617
with override_environment(MACROSTRAT_CONFIG=str(cfg_file), NO_COLOR="1"):
17-
from macrostrat.cli import main
1818
from macrostrat.core.config import settings
1919

2020
assert cfg_file == settings.config_file
21+
yield settings
2122

22-
result = runner.invoke(main, [])
23-
assert result.exit_code == 0
24-
# assert "Macrostrat control interface" in result.output
2523

24+
def test_cli_help(cfg):
25+
from macrostrat.cli import main
2626

27-
def test_cli_database():
28-
cfg_file = __here__ / "macrostrat.test.toml"
29-
with override_environment(MACROSTRAT_CONFIG=str(cfg_file), NO_COLOR="1"):
30-
from macrostrat.core.config import settings
27+
result = runner.invoke(main, [])
28+
assert result.exit_code == 0
3129

32-
assert (
33-
settings.pg_database
34-
== "postgresql://user:password@localhost:5432/macrostrat"
35-
)
30+
31+
def test_cli_database(cfg):
32+
assert cfg.pg_database == "postgresql://user:password@localhost:5432/macrostrat"
3633

3734

3835
def test_cli_no_config():
@@ -43,3 +40,13 @@ def test_cli_no_config():
4340
assert result.exit_code == 0
4441
# assert "Macrostrat control interface" in result.output
4542
# assert "Active environment: None" in result.output
43+
44+
45+
@mark("migrations")
46+
def test_database_migrations(cfg):
47+
from macrostrat.core.migrations import _dry_run_migrations
48+
49+
res = _dry_run_migrations(legacy=False)
50+
51+
assert res.n_migrations > 0
52+
assert res.n_remaining == 0

core/macrostrat/core/migrations/__init__.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from macrostrat.database import Database
1010
from macrostrat.database.utils import OutputMode
1111
from macrostrat.dinosaur.upgrade_cluster.utils import database_cluster
12+
from pydantic import BaseModel
1213
from rich import print
1314
from time import time
1415

@@ -166,7 +167,29 @@ def run_migrations(
166167
)
167168

168169

170+
class MigrationResult(BaseModel):
171+
n_migrations: int
172+
n_remaining: int
173+
duration: float
174+
175+
169176
def dry_run_migrations(wait=False, legacy=False):
177+
res = _dry_run_migrations(legacy=legacy)
178+
179+
print(f"Applied {n_total} migrations in {res.duration:.1f} seconds")
180+
if res.n_remaining == 0:
181+
print("[bold green]No more migrations to apply!")
182+
else:
183+
print(f"{n_remaining} migrations remaining")
184+
185+
if wait:
186+
print(res)
187+
input("Press Enter to continue...")
188+
189+
return res
190+
191+
192+
def _dry_run_migrations(legacy=False):
170193
# Spin up a docker container with a temporary database
171194
image = settings.get("pg_database_container", "postgres:15")
172195

@@ -185,7 +208,7 @@ def dry_run_migrations(wait=False, legacy=False):
185208
url = f"postgresql://postgres@localhost:{port}/postgres"
186209
db = Database(url)
187210

188-
tstart = time()
211+
t_start = time()
189212

190213
_migrations = applyable_migrations(db, allow_destructive=True, legacy=legacy)
191214
_next_migrations = None
@@ -195,7 +218,7 @@ def dry_run_migrations(wait=False, legacy=False):
195218

196219
if _migrations == _next_migrations:
197220
print("No changes in applyable migrations, exiting")
198-
return
221+
break
199222

200223
_migrations = _next_migrations
201224
n_applied = _run_migrations(
@@ -208,16 +231,11 @@ def dry_run_migrations(wait=False, legacy=False):
208231
)
209232
n_migrations = len(_next_migrations)
210233

211-
if n_migrations == 0:
212-
print("No more migrations to apply!")
213-
214-
tend = time()
215-
216-
print(f"Applied {n_total} migrations in {tend - tstart:.2f} seconds")
234+
t_end = time()
217235

218-
if wait:
219-
print(url)
220-
input("Press Enter to continue...")
236+
return MigrationResult(
237+
n_migrations=n_total, n_remaining=n_migrations, duration=t_end - t_start
238+
)
221239

222240

223241
@lru_cache(10)

0 commit comments

Comments
 (0)