Skip to content

Commit

Permalink
Initialize the database automatically when starting mquery #402 (#421)
Browse files Browse the repository at this point in the history
Automatically init database with alembic upgrade head

Co-authored-by: Michał Jura <[email protected]>
Co-authored-by: msm <[email protected]>
  • Loading branch information
3 people authored Oct 15, 2024
1 parent 10351e6 commit 33cd906
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 15 deletions.
2 changes: 0 additions & 2 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ mkdir samples
# expect files in ./samples directory, and keep index in ./index.
vim .env
docker compose up --scale daemon=3 # this will take a while
docker compose exec web python3 -m mquery.db
```

- Good for testing mquery and production deployments on a single server
Expand All @@ -38,7 +37,6 @@ cd mquery
# expect files in ./samples directory, and keep index in ./index.
vim .env
docker compose -f docker-compose.dev.yml up # this will take a while
docker compose exec dev-web python3 -m mquery.db
```

- Good for development - all file changes will be picked up automatically.
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
graft src/mqueryfront/dist
include src/alembic.ini
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ git clone https://github.com/CERT-Polska/mquery.git
cd mquery
vim .env # optional - change samples and index directory locations
docker compose up --scale daemon=3 # building the images will take a while
docker compose exec web python3 -m mquery.db
```

The web interface should be available at `http://localhost`.
Expand Down
1 change: 0 additions & 1 deletion docs/how-to/install-native.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ Now you need to create and configure a database
```shell
psql -c "CREATE DATABASE mquery"
source /opt/mquery/venv/bin/activate # remember, we need virtualenv
python3 -m mquery.db # initialize the mquery database
```

### Start everything
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"mquery.lib",
"mquery.plugins",
"mquery.models",
"mquery.migrations",
"mquery.migrations.versions",
],
package_dir={"mquery": "src"},
include_package_data=True,
Expand Down
2 changes: 1 addition & 1 deletion src/alembic.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[alembic]
script_location = migrations
script_location = %(here)s/migrations
prepend_sys_path = .
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.

Expand Down
10 changes: 9 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
import os

import uvicorn # type: ignore
Expand Down Expand Up @@ -44,8 +45,15 @@
ServerSchema,
)


@asynccontextmanager
async def lifespan(app: FastAPI):
db.alembic_upgrade()
yield


db = Database(app_config.redis.host, app_config.redis.port)
app = FastAPI()
app = FastAPI(lifespan=lifespan)


def with_plugins() -> Iterable[PluginManager]:
Expand Down
16 changes: 7 additions & 9 deletions src/db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from alembic.config import Config
from alembic import command
from pathlib import Path
from collections import defaultdict
from contextlib import contextmanager
from typing import List, Optional, Dict, Any
Expand All @@ -9,7 +12,6 @@
from rq import Queue # type: ignore
from sqlmodel import (
Session,
SQLModel,
create_engine,
select,
and_,
Expand Down Expand Up @@ -410,11 +412,7 @@ def set_config_key(self, plugin_name: str, key: str, value: str) -> None:
session.add(entry)
session.commit()


def init_db() -> None:
engine = create_engine(app_config.database.url, echo=True)
SQLModel.metadata.create_all(engine)


if __name__ == "__main__":
init_db()
def alembic_upgrade(self) -> None:
config_file = Path(__file__).parent / "alembic.ini"
alembic_cfg = Config(str(config_file))
command.upgrade(alembic_cfg, "head")

0 comments on commit 33cd906

Please sign in to comment.