From ecad14368e2c2347a5d7b6f745413347a6934e37 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Wed, 22 Mar 2023 17:04:09 +0000 Subject: [PATCH] db: Add initial alembic configuration No migrations yet: this is simply the output of 'alembic init' with some minor tweaks. A note about ordering of requirements is removed from requirements.txt: it is no longer true with the new dependency resolver introduced in pip 20.3. Signed-off-by: Stephen Finucane Change-Id: I7790222ab5eaf6c47dd386fe472275a3037a9898 --- doc/source/conf.py | 1 + heat/db/sqlalchemy/alembic.ini | 72 +++++++++++++++++ heat/db/sqlalchemy/migrations/README.rst | 15 ++++ heat/db/sqlalchemy/migrations/env.py | 82 ++++++++++++++++++++ heat/db/sqlalchemy/migrations/script.py.mako | 20 +++++ requirements.txt | 5 +- 6 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 heat/db/sqlalchemy/alembic.ini create mode 100644 heat/db/sqlalchemy/migrations/README.rst create mode 100644 heat/db/sqlalchemy/migrations/env.py create mode 100644 heat/db/sqlalchemy/migrations/script.py.mako diff --git a/doc/source/conf.py b/doc/source/conf.py index 913262c958..0cf74e0e19 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -178,6 +178,7 @@ apidoc_separate_modules = True apidoc_excluded_paths = [ 'cmd', 'cloudinit', + 'db/sqlalchemy/migrations/versions', 'db/sqlalchemy/migrate_repo/versions', 'engine/resources/aws', 'engine/resources/openstack', diff --git a/heat/db/sqlalchemy/alembic.ini b/heat/db/sqlalchemy/alembic.ini new file mode 100644 index 0000000000..b4a2f235a0 --- /dev/null +++ b/heat/db/sqlalchemy/alembic.ini @@ -0,0 +1,72 @@ +[alembic] +# path to migration scripts +script_location = %(here)s/migrations + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# version location specification; This defaults +# to heat/db/sqlalchemy/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:heat/db/sqlalchemy/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. + +sqlalchemy.url = sqlite:///heat.db + + +[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 + +# 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 diff --git a/heat/db/sqlalchemy/migrations/README.rst b/heat/db/sqlalchemy/migrations/README.rst new file mode 100644 index 0000000000..b2283fc82c --- /dev/null +++ b/heat/db/sqlalchemy/migrations/README.rst @@ -0,0 +1,15 @@ +Database migrations +=================== + +This directory contains migrations for the database. These are implemented +using `alembic`__, a lightweight database migration tool designed for usage +with `SQLAlchemy`__. + +The best place to start understanding Alembic is with its own `tutorial`__. You +can also play around with the :command:`alembic` command:: + + $ alembic --help + +.. __: https://alembic.sqlalchemy.org/en/latest/ +.. __: https://www.sqlalchemy.org/ +.. __: https://alembic.sqlalchemy.org/en/latest/tutorial.html diff --git a/heat/db/sqlalchemy/migrations/env.py b/heat/db/sqlalchemy/migrations/env.py new file mode 100644 index 0000000000..b0df5e9b38 --- /dev/null +++ b/heat/db/sqlalchemy/migrations/env.py @@ -0,0 +1,82 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool + +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. +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 +target_metadata = None + + +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, + 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, target_metadata=target_metadata + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/heat/db/sqlalchemy/migrations/script.py.mako b/heat/db/sqlalchemy/migrations/script.py.mako new file mode 100644 index 0000000000..120937fbcc --- /dev/null +++ b/heat/db/sqlalchemy/migrations/script.py.mako @@ -0,0 +1,20 @@ +"""${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"} diff --git a/requirements.txt b/requirements.txt index 18150cfc46..7794a608b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,11 +2,8 @@ # date but we do not test them so no guarantee of having them all correct. If # you find any incorrect lower bounds, let us know or propose a fix. -# The order of packages is significant, because pip processes them in the order -# of appearance. Changing the order has an impact on the overall integration -# process, which may cause wedges in the gate later. - pbr>=3.1.1 # Apache-2.0 +alembic>=1.8.0 # MIT Babel!=2.4.0,>=2.3.4 # BSD ddt>=1.4.1 # MIT croniter>=0.3.4 # MIT License