Skip to content

Commit

Permalink
run alembic migrations instead of using sqlalchemy to migrate test db (
Browse files Browse the repository at this point in the history
…#169)

* run alembic migrations instead of creating sqlalchemy models directly

* formatting

* empty

* fix path

* rename some variables

---------

Co-authored-by: Peter Dudfield <[email protected]>
  • Loading branch information
mduffin95 and peterdudfield authored Nov 25, 2024
1 parent 2a79d67 commit a477dfb
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import datetime as dt
import json
import os.path
import uuid
from pathlib import Path
from typing import List

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from testcontainers.postgres import PostgresContainer

from alembic import command
from alembic.config import Config
from pvsite_datamodel import ClientSQL, GenerationSQL, SiteSQL, StatusSQL
from pvsite_datamodel.sqlmodels import Base
from pvsite_datamodel.write.user_and_site import create_site_group, create_user

PROJECT_PATH = Path(__file__).parent.parent.resolve()


@pytest.fixture(scope="session")
def engine():
Expand All @@ -22,7 +27,14 @@ def engine():
# TODO need to setup postgres database with docker
url = postgres.get_connection_url()
engine = create_engine(url)
Base.metadata.create_all(engine)

# run alembic migrations
ini_path = os.path.join(PROJECT_PATH, "alembic.ini")
alembic_cfg = Config(file_=ini_path)
alembic_dir = os.path.join(PROJECT_PATH, "alembic")
alembic_cfg.set_main_option("script_location", alembic_dir)
os.environ["DB_URL"] = url
command.upgrade(alembic_cfg, "head")

yield engine

Expand Down

0 comments on commit a477dfb

Please sign in to comment.