[Python] Alembic Revisions for IBM DB2 not working

Discussão em 'Python' iniciado por Stack, Janeiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am about to develop a web application using flask and ibm db for z/os. As now I am implementing with flask. I tried with this below approach. I have installed alembic and created alembic folder with its command and made some changes. I ran the initial migration file command and generated a version file, while doing alembic upgrade command, it iOS not applying any schema to db. I debugged and put a pdf in run migration online method and execution is not coming there, what am I missing.

    I have modified my initial migration version to this. version/initial_migration.py

    """Initial migration

    Revision ID: 41d53738262c
    Revises:
    Create Date: 2025-01-24 12:52:13.477580

    """
    from alembic import op
    import sqlalchemy as sa
    # from src.utilities.all_envs import all_envs

    # revision identifiers, used by Alembic.
    revision = '41d53738262c'
    down_revision = None
    branch_labels = None
    depends_on = None

    def upgrade():
    # Create the employee table
    op.create_table(
    'employee_table',
    sa.Column('n_id', sa.Integer(), nullable=False),
    sa.Column('v_name', sa.String(length=128), nullable=False),
    sa.Column('v_description', sa.String(length=512), nullable=True),
    sa.Column('v_other_names', sa.String(length=1024), nullable=True),

    sa.PrimaryKeyConstraint('n_id'),
    sa.UniqueConstraint('v_name'),
    schema='sample_schema'
    )


    def downgrade():
    op.drop_table('employee_table', schema='sample_schema')


    this is my env.py of alembic.

    from alembic import context
    from sqlalchemy import create_engine, pool

    def run_migrations_online():
    # Retrieve the database URL from alembic.ini
    config = context.config
    url = config.get_main_option("sqlalchemy.url")

    # Create an engine from the database URL
    engine = create_engine(url, poolclass=pool.NullPool)

    # Connect to the database
    with engine.connect() as connection:
    # Configure Alembic context without passing target_metadata
    context.configure(
    connection=connection
    )

    # Use the connection to begin migrations
    with connection.begin():
    context.run_migrations()


    this is my alembic.ini

    # A generic, single database configuration.

    [alembic]
    # path to migration scripts
    script_location = alembic

    # template used to generate migration files
    # file_template = %%(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 alembic/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"
    # version_locations = %(here)s/bar:%(here)s/bat:alembic/versions

    # version path separator; As mentioned above, this is the character used to split
    # version_locations. Valid values are:
    #
    # version_path_separator = :
    # version_path_separator = ;
    # version_path_separator = space
    version_path_separator = os # default: use os.pathsep

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

    sqlalchemy.url = ibm_db_sa://user:pwd@host:port/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


    I am trying this POC. I am able to generate initial migration File with alembic revision command and it generated a version file. But when I try alembic upgrade command it is not applying any tables to my database. Any help here.

    libraries versions:

    alembic==1.9.0

    Flask==3.1.0
    Flask-Cors==5.0.0
    Flask-HTTPAuth==4.8.0
    Flask-JWT-Extended==4.7.1
    Flask-RESTful==0.3.10
    flask-swagger-ui==4.11.1
    greenlet==3.1.1
    gunicorn==23.0.0
    ibm_db==3.2.3
    ibm_db_sa==0.4.1
    idna==3.10
    importlib_metadata==8.6.1
    iniconfig==2.0.0

    Continue reading...

Compartilhe esta Página