Skip to content

Commit

Permalink
integrate alembic to GeoNature
Browse files Browse the repository at this point in the history
GeoNature database schema is not yet managed by alembic,
but packaged geonature modules can rely on alembic to manage their schema.
To do so, add in the setup.py:
    entry_point = {
        'gn_module': [
            'migrations = gn_module_sample.migrations:versions',
        ],
    },
  • Loading branch information
bouttier committed Mar 30, 2021
1 parent a9bbab5 commit a7cdeaf
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 1 deletion.
21 changes: 20 additions & 1 deletion backend/geonature/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,38 @@
from flask_cors import CORS
from sqlalchemy import exc as sa_exc
from flask_sqlalchemy import before_models_committed
from pkg_resources import iter_entry_points

from geonature.utils.config import config
from geonature.utils.env import MAIL, DB, MA
from geonature.utils.env import MAIL, DB, MA, migrate
from geonature.utils.module import import_backend_enabled_modules


@migrate.configure
def configure_alembic(alembic_config):
"""
This function add to the 'version_locations' parameter of the alembic config the
'migrations' entry point value of the 'gn_module' group for all modules having such entry point.
Thus, alembic will find migrations of all installed geonature modules.
"""
version_locations = alembic_config.get_main_option('version_locations', default='').split()
for entry_point in iter_entry_points('gn_module', 'migrations'):
# TODO: define enabled module in configuration (skip disabled module, raise error on missing module)
_, migrations = str(entry_point).split('=', 1)
version_locations += [ migrations.strip() ]
alembic_config.set_main_option('version_locations', ' '.join(version_locations))
return alembic_config


def create_app(with_external_mods=True, with_flask_admin=True):
app = Flask(__name__)
app.config.update(config)

# Bind app to DB
DB.init_app(app)

migrate.init_app(app, DB)

MAIL.init_app(app)

# For deleting files on "delete" media
Expand Down
1 change: 1 addition & 0 deletions backend/geonature/migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
50 changes: 50 additions & 0 deletions backend/geonature/migrations/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# A generic, single database configuration.

[alembic]
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

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


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

[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

[logger_flask_migrate]
level = INFO
handlers =
qualname = flask_migrate

[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
90 changes: 90 additions & 0 deletions backend/geonature/migrations/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from __future__ import with_statement

import logging
from logging.config import fileConfig

from flask import current_app

from alembic import context

# 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.
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
target_metadata = current_app.extensions['migrate'].db.metadata

# 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 run_migrations_offline():
"""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, target_metadata=target_metadata, literal_binds=True
)

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


def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""

# this callback is used to prevent an auto-migration from being generated
# when there are no changes to the schema
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
def process_revision_directives(context, revision, directives):
if getattr(config.cmd_opts, 'autogenerate', False):
script = directives[0]
if script.upgrade_ops.is_empty():
directives[:] = []
logger.info('No changes in schema detected.')

connectable = current_app.extensions['migrate'].db.engine

with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
process_revision_directives=process_revision_directives,
**current_app.extensions['migrate'].configure_args
)

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 backend/geonature/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():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}
2 changes: 2 additions & 0 deletions backend/geonature/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sqlalchemy.orm.exc import NoResultFound
from flask_marshmallow import Marshmallow
from flask_mail import Mail
from flask_migrate import Migrate


# Must be at top of this file. I don't know why (?)
Expand All @@ -31,6 +32,7 @@
os.environ['FLASK_SQLALCHEMY_DB'] = 'geonature.utils.env.DB'
DB = SQLAlchemy()
MA = Marshmallow()
migrate = Migrate()

GN_MODULE_FILES = (
"manifest.toml",
Expand Down
1 change: 1 addition & 0 deletions backend/requirements-common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Flask-Admin==1.5.6
flask-cors==3.0.3
Flask-Mail==0.9.1
flask-marshmallow==0.12.0
flask-migrate==2.7.0
flask-script==2.0.5
flask-sqlalchemy==2.4.4
Flask-WeasyPrint==0.6
Expand Down
1 change: 1 addition & 0 deletions backend/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ flask-sqlalchemy
flask-cors
flask-mail
flask-marshmallow
flask-migrate
WTForms-SQLAlchemy

0 comments on commit a7cdeaf

Please sign in to comment.