Python - Alembic migration packages using Setuptools

What is the correct way to batch Alembic migration files in Setuptools file setup.py? Everything in my root repo is like alembic/.

This is a Python application, not a library.

My desired installation thread is that someone can pip installwheel, which is my application. Then they will be able to initialize the application database by doing something like <app> alembic upgrade --sqlalchemy.url=<db_url>. Modifications will then be required pip install -U, after which they can run the Alembic command again.

Is this unorthodox?

If not, how would this be done? Of course console_scripts entry_points. But beyond that?

+10
source share
2

, , :

-, alembic, -x, , . db_url config.ini.

, alembic.ini alembic :

<project root>
β”œβ”€β”€ src
β”‚   └── <top-level package dir>
β”‚       β”œβ”€β”€ alembic
β”‚       β”‚   β”œβ”€β”€ env.py
β”‚       β”‚   β”œβ”€β”€ README
β”‚       β”‚   β”œβ”€β”€ script.py.mako
β”‚       β”‚   └── versions
β”‚       β”‚       β”œβ”€β”€ 58c8dcd5fbdc_revision_1.py
β”‚       β”‚       └── ec385b47da23_revision_2.py
β”‚       β”œβ”€β”€ alembic.ini
β”‚       β”œβ”€β”€ __init__.py
β”‚       └── <other files and dirs>
└── <other files and dirs>

setuptools package_data setup.py:

setup(
    name=<package_name>,
    package_dir={'': 'src'},
    packages=find_packages(where='src'),
    package_data={
        '<top-level package dir>': ['alembic.ini', 'alembic/*', 'alembic/**/*'],
    },
    [...]
)  

alembic , alembic.ini , . %(here)s , alembic.ini:

# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = %(here)s/alembic

[...]

# version location specification; this defaults
# to alembic/versions.  When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat alembic/versions
version_locations = %(here)s/alembic/versions

[...]

, alembic alembic -c -c :

alembic -c <path to alembic.ini> ...
+3

, alembic , - alembic , .

, ( alembic, , - migrations), __init__.py alembic .

- - , .

, :

<project root>
β”œβ”€β”€ setup.py
β”œβ”€β”€ mypackage
β”‚   └── <project source files...>
β”‚
β”œβ”€β”€ migrations
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ alembic.ini
β”‚   β”œβ”€β”€ apply.py
β”‚   β”œβ”€β”€ env.py
β”‚   β”œβ”€β”€ README
β”‚   β”œβ”€β”€ script.py.mako
β”‚   └── versions
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ 58c8dcd5fbdc_revision_1.py
β”‚       └── ec385b47da23_revision_2.py
β”‚
└── <other files and dirs>

setup.py:

from setuptools import find_packages
from setuptools import setup


setup(
    name='mypackage',
    packages=find_packages(exclude=('tests',)),
    package_data={'migrations': ['alembic.ini']},
    entry_points={
        'console_scripts': ['apply-migrations=migrations.apply:main'],
    },
    install_requires=[
        "SQLAlchemy==1.3.0",
        "alembic==1.0.10",
        # ...
    ]
)

, , migrations/apply.py:

# Python script that will apply the migrations up to head
import alembic.config
import os

here = os.path.dirname(os.path.abspath(__file__))

alembic_args = [
    '-c', os.path.join(here, 'alembic.ini'),
    'upgrade', 'head'
]


def main():
    alembic.config.main(argv=alembic_args)

apply-migrations . , , , - , , . --sqlalchemy.url alembic_args.

migrations/env.py URL migrations/env.py , SQLACLHEMYURL migrations/env.py:

import os
config.set_main_options(os.getenv('SQLALCHEMYURL'))

:

SQLALCHEMYURL=... apply-migrations

.

0

Source: https://habr.com/ru/post/1670493/


All Articles