Skip to content

Commit

Permalink
Simplify db setup
Browse files Browse the repository at this point in the history
DB creation is handled nicely in the Alembic script, so remove from CI workflow steps and local scripts.
  • Loading branch information
steventux committed Jan 27, 2025
1 parent 4f9921b commit a304f78
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 51 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/stage-2-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ jobs:
ENV_FILE: ".env.test"

steps:
- name: "Create test db"
run: |
psql -U postgres -h 127.0.0.1 -d postgres -tc "CREATE DATABASE communication_management_test;"
- name: "Checkout code"
uses: actions/checkout@v4

Expand Down Expand Up @@ -129,10 +125,6 @@ jobs:
ENV_FILE: ".env.test"

steps:
- name: "Create test db"
run: |
psql -U postgres -h 127.0.0.1 -d postgres -tc "CREATE DATABASE communication_management_test;"
- name: "Checkout code"
uses: actions/checkout@v4

Expand Down
78 changes: 39 additions & 39 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,64 @@
# Load environment variables
dotenv.load_dotenv(dotenv_path=os.getenv("ENV_FILE", ".env.local"))

# Get logger
logger = logging.getLogger("alembic")

# Construct the DB connection string
DATABASE_NAME = os.environ["DATABASE_NAME"]
DATABASE_USER = os.environ["DATABASE_USER"]
DATABASE_HOST = os.environ["DATABASE_HOST"]
DATABASE_PASSWORD = os.environ["DATABASE_PASSWORD"]
DATABASE_SSLMODE = os.getenv("DATABASE_SSLMODE", "require")

connection_string = (
f"postgresql+psycopg2://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}/{DATABASE_NAME}"
f"?sslmode={DATABASE_SSLMODE}"
)

# Check and create the database if it doesn't exist
try:
engine = create_engine(connection_string.rsplit("/", 1)[0]) # Remove DB name
if not database_exists(connection_string):
create_database(connection_string)
logger.info(f"Database '{DATABASE_NAME}' created successfully.")
except Exception as e:
logger.error(f"Error checking or creating database: {e}")

# Pass the connection string to Alembic
config = context.config
config.set_main_option("sqlalchemy.url", connection_string)

# Logging setup
if config.config_file_name is not None:
fileConfig(config.config_file_name)
def connection_string() -> str:
return (
f"postgresql+psycopg2://{os.environ['DATABASE_USER']}"
f":{os.environ['DATABASE_PASSWORD']}"
f"@{os.environ['DATABASE_HOST']}"
f"/{os.environ['DATABASE_NAME']}"
f"?sslmode={os.environ.get('DATABASE_SSLMODE', 'require')}"
)

target_metadata = Base.metadata

def run_migrations_offline():
url = config.get_main_option("sqlalchemy.url")
def run_migrations_offline(connection_string: str):
context.configure(
url=url,
target_metadata=target_metadata,
url=connection_string,
target_metadata=Base.metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()

def run_migrations_online():

def run_migrations_online(connection_string: str):
connectable = create_engine(
connection_string,
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
connection=connection, target_metadata=Base.metadata
)
with context.begin_transaction():
context.run_migrations()


def create_database_if_not_exists(connection_string: str) -> None:
logger = logging.getLogger("alembic")
try:
if not database_exists(connection_string):
create_database(connection_string)
logger.info(f"Database '{os.environ['DATABASE_NAME']}' created successfully.")
except Exception as e:
logger.error(f"Error checking or creating database: {e}")


def configure_alembic(connection_string: str) -> None:
context.config.set_main_option("sqlalchemy.url", connection_string)

if context.config.config_file_name is not None:
fileConfig(context.config.config_file_name)


# Perform steps to run migrations
connection_string: str = connection_string()
configure_alembic(connection_string)
create_database_if_not_exists(connection_string)

if context.is_offline_mode():
run_migrations_offline()
run_migrations_offline(connection_string)
else:
run_migrations_online()
run_migrations_online(connection_string)
4 changes: 1 addition & 3 deletions dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
ENV_FILE=".env.local"

source ${ENV_FILE}
echo "Creating database ${DATABASE_NAME}..."
psql -U ${DATABASE_USER} -c "CREATE DATABASE ${DATABASE_NAME};"

echo "Migrating database to latest version..."
echo "Set up database and migrate to latest version..."
ENV_FILE=${ENV_FILE} alembic upgrade head

echo "Starting the API function app..."
Expand Down
1 change: 0 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
ENV_FILE=".env.test"

source ${ENV_FILE}
psql -U ${DATABASE_USER} -c "CREATE DATABASE ${DATABASE_NAME};"

ENV_FILE=${ENV_FILE} alembic upgrade head

Expand Down

0 comments on commit a304f78

Please sign in to comment.