Skip to content

Commit dc50470

Browse files
authored
Merge pull request #8 from k2bd/feat/initial-cli
WIP CLI and tweaks
2 parents b09a487 + 503e228 commit dc50470

15 files changed

+542
-101
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
venv/
2+
.vscode/
23

34
# Byte-compiled / optimized / DLL files
45
__pycache__/

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ Backends are loaded as plugins through Python's entry point system.
3434
This means that you can add a new backend by simply installing a package that provides the backend as a plugin.
3535

3636
To create a new backend in your package, you need to subclass ``flux.MigrationBackend`` and implement its abstract methods.
37-
Then register that class under the ``flux.backends`` entry point group in your package setup.
37+
Then register that class under the ``flux.backend`` entry point group in your package setup.
3838

3939
For example, in ``pyproject.toml``:
4040

4141
```toml
42-
[project.entry-points."flux.backends"]
42+
[project.entry-points."flux.backend"]
4343
cooldb = "my_package.my_module:CoolDbBackend"
4444
```
4545

poetry.lock

+11-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ authors = ["Kevin Duff <[email protected]>"]
66
readme = "README.md"
77
packages = [{include = "flux", from = "src"}]
88

9+
[tool.poetry.scripts]
10+
flux = "flux.cli:app"
11+
12+
[tool.poetry.plugins."flux.backend"]
13+
postgres = "flux.builtins.postgres:FluxPostgresBackend"
14+
915
[tool.poetry.dependencies]
1016
python = "^3.10"
1117
typer = "^0.12.3"
1218
toml = "^0.10.2"
19+
aiopg = { version = "^1.4.0", optional = true }
20+
databases = { version = "^0.9.0", optional = true }
1321

1422
[tool.poetry.group.dev.dependencies]
1523
black = "^24.4.2"
@@ -20,8 +28,10 @@ poethepoet = "^0.26.1"
2028
pytest = "^8.2.0"
2129
pytest-cov = "^5.0.0"
2230
pytest-asyncio = "^0.23.6"
23-
aiopg = "^1.4.0"
24-
databases = "^0.9.0"
31+
32+
[tool.poetry.extras]
33+
postgres = ["aiopg", "databases"]
34+
2535

2636
[tool.isort]
2737
profile = "black"

src/flux/__main__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .cli import app
2+
3+
if __name__ == "__main__":
4+
app()

src/flux/backend/base.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class MigrationBackend(ABC):
1010

1111
@classmethod
12-
def from_config(cls, config: FluxConfig) -> "MigrationBackend":
12+
def from_config(cls, config: FluxConfig, connection_uri: str) -> "MigrationBackend":
1313
"""
1414
Create a MigrationBackend from a configuration
1515
@@ -28,29 +28,27 @@ async def connection(self):
2828

2929
@asynccontextmanager
3030
@abstractmethod
31-
async def transaction(self):
31+
async def migration_lock(self):
3232
"""
33-
Create a transaction that lasts as long as the context manager is
34-
active.
35-
36-
The transaction is committed when the context manager exits.
33+
Create a lock that prevents other migration processes from running
34+
concurrently.
3735
38-
If an exception is raised inside the context manager, the transaction
39-
is rolled back.
36+
This lock should last as long as the context manager and should operate
37+
will be within ``connection`` but outside of ``transaction``.
4038
"""
4139
yield
4240

4341
@asynccontextmanager
4442
@abstractmethod
45-
async def migration_lock(self):
43+
async def transaction(self):
4644
"""
47-
Create a lock that prevents other migration processes from running
48-
concurrently.
45+
Create a transaction that lasts as long as the context manager is
46+
active.
47+
48+
The transaction is committed when the context manager exits.
4949
50-
The acceptable unlock conditions are:
51-
- The context manager exits
52-
- The transaction ends
53-
- The connection ends
50+
If an exception is raised inside the context manager, the transaction
51+
is rolled back.
5452
"""
5553
yield
5654

tests/integration/postgres/backend.py src/flux/builtins/postgres.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
from contextlib import asynccontextmanager
33
from dataclasses import dataclass, field
44

5-
from databases import Database
6-
from databases.core import Connection, Transaction
5+
try:
6+
from databases import Database
7+
from databases.core import Connection, Transaction
8+
except ImportError as e:
9+
raise ImportError(
10+
"Please install the postgres dependency group of flux-migrations to use the builtin Postgres backend. For example: pip install 'flux-migrations[postgres]'" # noqa: E501
11+
) from e
712

813
from flux.backend.applied_migration import AppliedMigration
914
from flux.backend.base import MigrationBackend
@@ -18,7 +23,7 @@
1823

1924

2025
@dataclass
21-
class ExamplePostgresBackend(MigrationBackend):
26+
class FluxPostgresBackend(MigrationBackend):
2227
database_url: str
2328
migrations_table: str = DEFAULT_MIGRATIONS_TABLE
2429
migrations_schema: str = DEFAULT_MIGRATIONS_SCHEMA
@@ -33,15 +38,14 @@ def qualified_migrations_table(self) -> str:
3338
return f"{self.migrations_schema}.{self.migrations_table}"
3439

3540
@classmethod
36-
def from_config(cls, config: FluxConfig) -> "ExamplePostgresBackend":
41+
def from_config(
42+
cls, config: FluxConfig, connection_uri: str
43+
) -> "FluxPostgresBackend":
3744
"""
3845
Create a MigrationBackend from a configuration
3946
4047
This config appears in the config.toml file in the "backend" section.
4148
"""
42-
database_url = config.backend_config.get("database_url")
43-
if not database_url:
44-
raise ValueError("No database_url provided.")
4549
migrations_table = config.backend_config.get(
4650
"migrations_table", DEFAULT_MIGRATIONS_TABLE
4751
)
@@ -54,7 +58,7 @@ def from_config(cls, config: FluxConfig) -> "ExamplePostgresBackend":
5458
"migrations_schema", DEFAULT_MIGRATIONS_SCHEMA
5559
)
5660
return cls(
57-
database_url=database_url,
61+
database_url=connection_uri,
5862
migrations_table=migrations_table,
5963
migrations_lock_id=migrations_lock_id,
6064
migrations_schema=migrations_schema,

0 commit comments

Comments
 (0)