Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup alembic #441

Closed
wants to merge 64 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
e613320
alembic setup
nrjadkry May 28, 2023
32d6cf8
Merge branch 'development' of github.com:hotosm/fmtm into setup-alembic
nrjadkry May 30, 2023
7a7882c
alembic.ini file moved to app directory
nrjadkry Jun 1, 2023
bd40e16
Base.metadata.create_all(bind=engine) removed since alembic is used …
nrjadkry Jun 1, 2023
c96af13
env.py for alembic is moved inside app directory
nrjadkry Jun 1, 2023
b0bfb9b
script.py.mako is moved inside app directory
nrjadkry Jun 1, 2023
ac24b56
readme for alembic moved in app directory
nrjadkry Jun 1, 2023
c9906ec
updated alembic.ini file to exclude postgis exxtension
nrjadkry Jun 4, 2023
e81bd03
updated env.py in migrations to exclude postgis extension
nrjadkry Jun 4, 2023
d125256
sqlalchemy url obtained from settings for alembic migrations
nrjadkry Jun 4, 2023
330cbad
sqlalchemy url obtained from env in config settings
nrjadkry Jun 4, 2023
9f5d771
first migration file
nrjadkry Jun 4, 2023
c543d2c
fix: create project single AOI
varun2948 Sep 11, 2023
bd1ecc1
fix: commented pagination for future
varun2948 Sep 11, 2023
cddc460
fix: edit project symbol changes on regex
varun2948 Sep 11, 2023
3cba915
fix: remove data extract on different dropdown selection
varun2948 Sep 11, 2023
75ad914
ci: move pytest and jest workflows to actions
spwoodcock Sep 11, 2023
5871741
ci: reusable test actions --> workflows
spwoodcock Sep 11, 2023
9c6229c
ci: reusable action --> workflow (pytest, jest)
spwoodcock Sep 11, 2023
fa0406a
fix: set underpass api url via env var
spwoodcock Sep 11, 2023
8f812f5
docs: add UNDERPASS_API_URL to env example
spwoodcock Sep 11, 2023
59da130
Merge pull request #826 from hotosm/feat-projectinfo-bug
robsavoye Sep 12, 2023
66e5659
Merge pull request #828 from hotosm/fix/underpass-env-var
robsavoye Sep 12, 2023
15a32d8
feat: remove microfrontend (#831)
varun2948 Sep 14, 2023
0356f78
build: fix label newline in prod.dockerfile
spwoodcock Sep 14, 2023
890c268
Overhaul CI, use reusable workflows (#830)
spwoodcock Sep 20, 2023
82ef69b
ci: fix flow, remove frontend-map refs
spwoodcock Sep 20, 2023
53a3e36
ci: set paths on parent workflows, not reusable workflows
spwoodcock Sep 20, 2023
6a554b4
ci: tweak workflows needs ordering
spwoodcock Sep 20, 2023
1010e20
ci: remove all refs to frontend-map or ui-map
spwoodcock Sep 20, 2023
53f033b
docs: remove all refs to microfrontend
spwoodcock Sep 20, 2023
962b2c8
build: fix add prod stage to frontend dockerfile
spwoodcock Sep 20, 2023
51aa885
build: remove APP_NAME from frontend (microfrontend)
spwoodcock Sep 20, 2023
cddb028
ci: some secret vars via secrets, env via vars
spwoodcock Sep 20, 2023
3338abd
ci: fix workflow secret --> secrets
spwoodcock Sep 20, 2023
9026b15
ci: set secret vars in env first (pytest)
spwoodcock Sep 20, 2023
6cfbc08
ci: add GIT_BRANCH to deploy workflow
spwoodcock Sep 20, 2023
e1525a2
ci: add GIT_BRANCH to backend docker compose cmds
spwoodcock Sep 20, 2023
60022de
ci: add extract-versions to needs to smoke & deploy
spwoodcock Sep 20, 2023
4e84e68
ci: update backend smoke test manual containers
spwoodcock Sep 20, 2023
d0ac9de
ci: backend smoke test use multiline
spwoodcock Sep 20, 2023
5edb754
ci: backend smoke test port mapping
spwoodcock Sep 20, 2023
cad4f63
ci: comment backend smoke tests until db init fixed
spwoodcock Sep 20, 2023
b140834
Feat New Create Project and Docker Build Changes (#840)
varun2948 Sep 22, 2023
67fe116
refactor: update refs to frontend/main --> frontend
spwoodcock Sep 22, 2023
1217fc6
ci: run pre-commit hooks on all files
spwoodcock Sep 22, 2023
98f0349
changed single polygon to merged polygon POI if uploaded multi polygo…
Sujanadh Sep 25, 2023
3bc4fde
ci: upgrade pre-commit versions
spwoodcock Sep 25, 2023
8139e34
style: run pre-commit hooks on all
spwoodcock Sep 25, 2023
1946e39
alembic setup
nrjadkry May 28, 2023
62a9e3d
alembic.ini file moved to app directory
nrjadkry Jun 1, 2023
24aa0a8
fix conflict in main.py file
nrjadkry Sep 26, 2023
d1d8aa2
env.py for alembic is moved inside app directory
nrjadkry Jun 1, 2023
be9127b
script.py.mako is moved inside app directory
nrjadkry Jun 1, 2023
cadce01
readme for alembic moved in app directory
nrjadkry Jun 1, 2023
b122d6d
updated alembic.ini file to exclude postgis exxtension
nrjadkry Jun 4, 2023
ddfca4e
updated env.py in migrations to exclude postgis extension
nrjadkry Jun 4, 2023
3a97024
sqlalchemy url obtained from settings for alembic migrations
nrjadkry Jun 4, 2023
5ce1d76
fix: conflict in config file
nrjadkry Sep 26, 2023
04977e2
first migration file
nrjadkry Jun 4, 2023
5fd4d5a
fix: merge conflicts
nrjadkry Sep 26, 2023
f29e347
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 26, 2023
5b029a2
fix:default value for sqlalchemy url in config file
nrjadkry Sep 26, 2023
a48d003
Merge branch 'setup-alembic' of github.com:hotosm/fmtm into setup-ale…
nrjadkry Sep 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions src/backend/app/alembic.ini
spwoodcock marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = migrations

# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
# for all available tokens
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s

# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory.
prepend_sys_path = .

# timezone to use when rendering the date within the migration file
# as well as the filename.
# If specified, requires the python-dateutil library that can be
# installed by adding `alembic[tz]` to the pip requirements
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =

# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false

# version location specification; This defaults
# to migrations/versions. When using multiple version
# directories, initial revisions must be specified with --version-path.
# The path separator used here should be the separator specified by "version_path_separator" below.
# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions

# version path separator; As mentioned above, this is the character used to split
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
# Valid values for version_path_separator are:
#
# version_path_separator = :
# version_path_separator = ;
# version_path_separator = space
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.

# set to 'true' to search source files recursively
# in each "version_locations" directory
# new in Alembic version 1.10
# recursive_version_locations = false

# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8

sqlalchemy.url =

[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples

# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks = black
# black.type = console_scripts
# black.entrypoint = black
# black.options = -l 79 REVISION_SCRIPT_FILENAME

# Custom param that enables us to specify tables to ignore when determining migrations
[alembic:exclude]
tables = spatial_ref_sys

# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S


1 change: 1 addition & 0 deletions src/backend/app/config.py
nrjadkry marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def assemble_db_connection(cls, v: str, values: dict[str, Any]) -> Any:
OSM_LOGIN_REDIRECT_URI: AnyUrl
OSM_SECRET_KEY: str
OAUTHLIB_INSECURE_TRANSPORT: Optional[str] = 1
SQLALCHEMY_URL: Optional[str]

class Config:
"""Pydantic settings config."""
Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async def startup_event():
"""Commands to run on server startup."""
logger.debug("Starting up FastAPI server.")
logger.debug("Connecting to DB with SQLAlchemy")
Base.metadata.create_all(bind=engine)
# Base.metadata.create_all(bind=engine)

# Read in XLSForms
read_xlsforms(next(get_db()), xlsforms_path)
Expand Down
1 change: 1 addition & 0 deletions src/backend/app/migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
84 changes: 84 additions & 0 deletions src/backend/app/migrations/env.py
spwoodcock marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool
from config import settings
from geoalchemy2 import alembic_helpers

from alembic import context

config = context.config
config.set_main_option("sqlalchemy.url", settings.SQLALCHEMY_URL)

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

from db.db_models import Base
target_metadata = Base.metadata


exclude_tables = config.get_section("alembic:exclude").get("tables", "").split(",")

def include_object(object, name, type_, reflected, compare_to):
"""
Custom helper function that enables us to ignore our excluded tables in the autogen sweep
"""
if type_ == "table" and name in exclude_tables:
return False
else:
return alembic_helpers.include_object(
object, name, type_, reflected, compare_to
)


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.

"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
include_object=include_object,
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.

"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

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

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


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
24 changes: 24 additions & 0 deletions src/backend/app/migrations/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}


def upgrade() -> None:
${upgrades if upgrades else "pass"}


def downgrade() -> None:
${downgrades if downgrades else "pass"}
Loading