Skip to content

Commit

Permalink
Merge pull request #282 from OCHA-DAP/feature/upgrade-req
Browse files Browse the repository at this point in the history
Feature/upgrade req
  • Loading branch information
danmihaila authored Jan 29, 2025
2 parents 4509239 + 0f55eb7 commit 7309f3e
Show file tree
Hide file tree
Showing 35 changed files with 1,162 additions and 1,094 deletions.
236 changes: 115 additions & 121 deletions alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,121 +1,115 @@
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context
from hdx_hapi.config.helper import create_pg_uri_from_env_without_protocol

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata

from hdx_hapi.db.models.base import Base
from hapi_schema.db_admin1 import DBAdmin1
from hapi_schema.db_admin2 import DBAdmin2
from hapi_schema.db_age_range import DBAgeRange
from hapi_schema.db_dataset import DBDataset
from hapi_schema.db_food_security import DBFoodSecurity
from hapi_schema.db_gender import DBGender
from hapi_schema.db_humanitarian_needs import DBHumanitarianNeeds
from hapi_schema.db_ipc_phase import DBIpcPhase
from hapi_schema.db_ipc_type import DBIpcType
from hapi_schema.db_location import DBLocation
from hapi_schema.db_national_risk import DBNationalRisk
from hapi_schema.db_operational_presence import DBOperationalPresence
from hapi_schema.db_org import DBOrg
from hapi_schema.db_org_type import DBOrgType
from hapi_schema.db_patch import DBPatch
from hapi_schema.db_population import DBPopulation
from hapi_schema.db_population_group import DBPopulationGroup
from hapi_schema.db_population_status import DBPopulationStatus
from hapi_schema.db_resource import DBResource
from hapi_schema.db_sector import DBSector

target_metadata = Base.metadata
# target_metadata = None

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def _get_db_uri() -> str:
db_url_dict = context.get_x_argument("sqlalchemy.url")
db_url = db_url_dict.get('sqlalchemy.url') if db_url_dict else None
if not db_url:
db_url = f'postgresql+psycopg2://{create_pg_uri_from_env_without_protocol()}'
# print(f'My db url is {db_url}')
return db_url


def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
custom_db_url = _get_db_uri()
url = custom_db_url if custom_db_url else config.get_main_option("sqlalchemy.url")
# print(f'My db url is {custom_db_url}')
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
# x_url = context.get_x_argument("sqlalchemy.url")
# db_engine_config = x_url if x_url else config.get_section(config.config_ini_section, {})
db_engine_config = config.get_section(config.config_ini_section, {})
custom_db_uri = _get_db_uri()
if custom_db_uri:
db_engine_config['sqlalchemy.url'] = custom_db_uri
# print(f'My db url is {db_engine_config}')
connectable = engine_from_config(
db_engine_config,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)

with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
# from logging.config import fileConfig

# from sqlalchemy import engine_from_config
# from sqlalchemy import pool

# from alembic import context
# from hdx_hapi.config.helper import create_pg_uri_from_env_without_protocol

# # this is the Alembic Config object, which provides
# # access to the values within the .ini file in use.
# config = context.config

# # Interpret the config file for Python logging.
# # This line sets up loggers basically.
# if config.config_file_name is not None:
# fileConfig(config.config_file_name)

# # add your model's MetaData object here
# # for 'autogenerate' support
# # from myapp import mymodel
# # target_metadata = mymodel.Base.metadata

# from hdx_hapi.db.models.base import Base
# from hapi_schema.db_admin1 import DBAdmin1
# from hapi_schema.db_admin2 import DBAdmin2
# from hapi_schema.db_dataset import DBDataset
# from hapi_schema.db_food_security import DBFoodSecurity
# from hapi_schema.db_humanitarian_needs import DBHumanitarianNeeds
# from hapi_schema.db_location import DBLocation
# from hapi_schema.db_national_risk import DBNationalRisk
# from hapi_schema.db_operational_presence import DBOperationalPresence
# from hapi_schema.db_org import DBOrg
# from hapi_schema.db_org_type import DBOrgType
# from hapi_schema.db_patch import DBPatch
# from hapi_schema.db_population import DBPopulation
# from hapi_schema.db_resource import DBResource
# from hapi_schema.db_sector import DBSector

# target_metadata = Base.metadata
# # target_metadata = None

# # other values from the config, defined by the needs of env.py,
# # can be acquired:
# # my_important_option = config.get_main_option("my_important_option")
# # ... etc.


# def _get_db_uri() -> str:
# db_url_dict = context.get_x_argument("sqlalchemy.url")
# db_url = db_url_dict.get('sqlalchemy.url') if db_url_dict else None
# if not db_url:
# db_url = f'postgresql+psycopg2://{create_pg_uri_from_env_without_protocol()}'
# # print(f'My db url is {db_url}')
# return db_url


# def run_migrations_offline() -> None:
# """Run migrations in 'offline' mode.

# This configures the context with just a URL
# and not an Engine, though an Engine is acceptable
# here as well. By skipping the Engine creation
# we don't even need a DBAPI to be available.

# Calls to context.execute() here emit the given string to the
# script output.

# """
# custom_db_url = _get_db_uri()
# url = custom_db_url if custom_db_url else config.get_main_option("sqlalchemy.url")
# # print(f'My db url is {custom_db_url}')
# context.configure(
# url=url,
# target_metadata=target_metadata,
# literal_binds=True,
# dialect_opts={"paramstyle": "named"},
# )

# with context.begin_transaction():
# context.run_migrations()


# def run_migrations_online() -> None:
# """Run migrations in 'online' mode.

# In this scenario we need to create an Engine
# and associate a connection with the context.

# """
# # x_url = context.get_x_argument("sqlalchemy.url")
# # db_engine_config = x_url if x_url else config.get_section(config.config_ini_section, {})
# db_engine_config = config.get_section(config.config_ini_section, {})
# custom_db_uri = _get_db_uri()
# if custom_db_uri:
# db_engine_config['sqlalchemy.url'] = custom_db_uri
# # print(f'My db url is {db_engine_config}')
# connectable = engine_from_config(
# db_engine_config,
# prefix="sqlalchemy.",
# poolclass=pool.NullPool,
# )

# with connectable.connect() as connection:
# context.configure(
# connection=connection, target_metadata=target_metadata
# )

# with context.begin_transaction():
# context.run_migrations()


# if context.is_offline_mode():
# run_migrations_offline()
# else:
# run_migrations_online()
134 changes: 61 additions & 73 deletions alembic/versions/2d6db74775b5_create_views.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,61 @@
"""create views
Revision ID: 2d6db74775b5
Revises: 927d2ce143cc
Create Date: 2024-04-25 15:02:49.678672
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

from hapi_schema.db_admin1 import view_params_admin1
from hapi_schema.db_admin2 import view_params_admin2
from hapi_schema.db_age_range import view_params_age_range
from hapi_schema.db_dataset import view_params_dataset
from hapi_schema.db_food_security import view_params_food_security
from hapi_schema.db_gender import view_params_gender
from hapi_schema.db_humanitarian_needs import view_params_humanitarian_needs
from hapi_schema.db_ipc_phase import view_params_ipc_phase
from hapi_schema.db_ipc_type import view_params_ipc_type
from hapi_schema.db_location import view_params_location
from hapi_schema.db_national_risk import view_params_national_risk
from hapi_schema.db_operational_presence import view_params_operational_presence
from hapi_schema.db_org import view_params_org
from hapi_schema.db_org_type import view_params_org_type
from hapi_schema.db_population import view_params_population
from hapi_schema.db_population_group import view_params_population_group
from hapi_schema.db_population_status import view_params_population_status
from hapi_schema.db_resource import view_params_resource
from hapi_schema.db_sector import view_params_sector

from hdx_hapi.db.models.views.util.util import CreateView, DropView


# revision identifiers, used by Alembic.
revision: str = '2d6db74775b5'
down_revision: Union[str, None] = '927d2ce143cc'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


VIEW_LIST = [
view_params_admin1,
view_params_admin2,
view_params_age_range,
view_params_dataset,
view_params_food_security,
view_params_gender,
view_params_humanitarian_needs,
view_params_ipc_phase,
view_params_ipc_type,
view_params_location,
view_params_national_risk,
view_params_operational_presence,
view_params_org_type,
view_params_org,
view_params_population_group,
view_params_population_status,
view_params_population,
view_params_resource,
view_params_sector,
]


def upgrade() -> None:
for v in VIEW_LIST:
op.get_bind().execute(CreateView(v.name, v.selectable))


def downgrade() -> None:
for v in VIEW_LIST:
op.get_bind().execute(DropView(v.name))
# """create views

# Revision ID: 2d6db74775b5
# Revises: 927d2ce143cc
# Create Date: 2024-04-25 15:02:49.678672

# """
# from typing import Sequence, Union

# from alembic import op
# import sqlalchemy as sa

# from hapi_schema.db_admin1 import view_params_admin1
# from hapi_schema.db_admin2 import view_params_admin2
# from hapi_schema.db_dataset import view_params_dataset
# from hapi_schema.db_food_security import view_params_food_security
# from hapi_schema.db_humanitarian_needs import view_params_humanitarian_needs
# from hapi_schema.db_location import view_params_location
# from hapi_schema.db_national_risk import view_params_national_risk
# from hapi_schema.db_operational_presence import view_params_operational_presence
# from hapi_schema.db_org import view_params_org
# from hapi_schema.db_org_type import view_params_org_type
# from hapi_schema.db_population import view_params_population
# from hapi_schema.db_resource import view_params_resource
# from hapi_schema.db_sector import view_params_sector

# from hdx_hapi.db.models.views.util.util import CreateView, DropView


# # revision identifiers, used by Alembic.
# revision: str = '2d6db74775b5'
# down_revision: Union[str, None] = '927d2ce143cc'
# branch_labels: Union[str, Sequence[str], None] = None
# depends_on: Union[str, Sequence[str], None] = None


# VIEW_LIST = [
# view_params_admin1,
# view_params_admin2,
# view_params_dataset,
# view_params_food_security,
# view_params_humanitarian_needs,
# view_params_location,
# view_params_national_risk,
# view_params_operational_presence,
# view_params_org_type,
# view_params_org,
# view_params_population,
# view_params_resource,
# view_params_sector,
# ]


# def upgrade() -> None:
# for v in VIEW_LIST:
# op.get_bind().execute(CreateView(v.name, v.selectable))


# def downgrade() -> None:
# for v in VIEW_LIST:
# op.get_bind().execute(DropView(v.name))
Loading

0 comments on commit 7309f3e

Please sign in to comment.