alembic 数据库管理工具的使用


安装方式:

  1.  com进行安装
    pip install alembic  -i 国内源地址
  2.  编辑器安装
    在设置里面找到三方库的安装目录,然后搜索alembic,点击安装

生成管理仓库:

  在项目目录中输入

alembic init alembic

项目目录中会自动生成 alembic 版本管理文件夹以及 alembic.ini 的配置文件

创建模型:

在创建模型的时候需要构建一个数据表的基类

eg:

# Copyright (c) 2020—2021 Toplinker Corporation. All rights reserved.

"""
数据表基类
"""

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative.api import DeclarativeMeta
from app.core.settings import settings


class PrefixMeta(DeclarativeMeta):
    """
    为表名添加前缀
    """

    def __init__(cls, classname, bases, dict_, **kw):
        if '__tablename__' in dict_:
            cls.__tablename__ = dict_['__tablename__'] = \
                settings.table_prefix + dict_['__tablename__']
        return super().__init__(classname, bases, dict_, **kw)


# 数据表基类
Base = declarative_base(metaclass=PrefixMeta)

然后创建的模型的时候需要导入上面创建的基类

eg:

# Copyright (c) 2020—2021 Toplinker Corporation. All rights reserved.

"""
用户表
"""

from sqlalchemy import Column, Integer, String, Boolean, Text
from app.models import Base


class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(255), unique=True, index=True, nullable=False)
    fullname = Column(String(255))
    email = Column(String(255))
    password = Column(String(512), nullable=False)
    is_active = Column(Boolean(), default=True)
    token_policy = Column(Integer, default=0)
    roles = Column(Text)
    remark = Column(Text)

修改alembic配置:

修改版本管理里面的env.py

将‘target_metadata = None’ 改为上面的基类,

修改alembic.ini文件

注释掉:

prepend_sys_path = .
version_path_separator = os  # default: use os.pathsep

更换sqlalchemy.url

postgresql+psycopg2://数据库用户:数据库密码@数据库IP地址:端口/数据库名称

生成迁移文件

alembic revision --autogenerate -m "message"

异常:

Traceback (most recent call last):
  File "G:\python_V\python3.8\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "G:\python_V\python3.8\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "G:\python_object\object_fastapi\venv\Scripts\alembic.exe\__main__.py", line 7, in 
  File "g:\python_object\object_fastapi\venv\lib\site-packages\alembic\config.py", line 588, in main
    CommandLine(prog=prog).main(argv=argv)
  File "g:\python_object\object_fastapi\venv\lib\site-packages\alembic\config.py", line 582, in main
    self.run_cmd(cfg, options)
  File "g:\python_object\object_fastapi\venv\lib\site-packages\alembic\config.py", line 559, in run_cmd
    fn(
  File "g:\python_object\object_fastapi\venv\lib\site-packages\alembic\command.py", line 227, in revision
    script_directory.run_env()
  File "g:\python_object\object_fastapi\venv\lib\site-packages\alembic\script\base.py", line 563, in run_env
    util.load_python_file(self.dir, "env.py")
  File "g:\python_object\object_fastapi\venv\lib\site-packages\alembic\util\pyfiles.py", line 92, in load_python_file
    module = load_module_py(module_id, path)
  File "g:\python_object\object_fastapi\venv\lib\site-packages\alembic\util\pyfiles.py", line 108, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "", line 848, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "G:\python_object\object_fastapi\backend\alembic\env.py", line 20, in 
    from app.models.base import Base
ModuleNotFoundError: No module named 'app.models.base'

原因: 没有找到对应的文件地方
解决方式:将目录临时加入环境变量种

import os
import sys; sys.path.append(os.getcwd())

然后再执行生成迁移文件命令:成功

alembic模块案例:

env.py:

from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context
import os
import sys; sys.path.append(os.getcwd())

# 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)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from app.models.base import Base
target_metadata = Base.metadata
# target_metadata = None

# 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,
        dialect_opts={"paramstyle": "named"},
    )

    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.

    """
    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()

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 = driver://user:pass@localhost/dbname
sqlalchemy.url = postgresql+psycopg2://账号:密码@127.0.0.1:5432/数据库名


[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

更新数据库:

将迁移文件推送收到数据库:alembic upgrade head
回滚版本(将head指向上一版本,然后再推到数据库):alembic downgrade head

  成功,

注:此处需要先创建一个配置中的数据库

更新模型代码之后更新数据库:

执行代码

更新迁移文件:alembic revision --autogenerate -m "message"
更新到数据库:alembic upgrade head

命令参数:

init:创建一个alembic仓库。

revision:创建一个新的版本文件。

--autogenerate:自动将当前模型的修改,生成迁移脚本。

-m:本次迁移做了哪些修改,用户可以指定这个参数,方便回顾。

upgrade:将指定版本的迁移文件映射到数据库中,会执行版本文件中的upgrade函数。如果有多个迁移脚本没有被映射到数据库中,那么会执行多个迁移脚本。

[head]:代表最新的迁移脚本的版本号。

downgrade:会执行指定版本的迁移文件中的downgrade函数。

heads:展示head指向的脚本文件版本号。

history:列出所有的迁移版本及其信息。

current:展示当前数据库中的版本号

 推送异常

1、 FAILED: Target databases is not up to data
  原因:head与current不同步了,current落后于head
  解决方式: 将current移动到head上面,让他们同步: alembic upgrade head
2、 FAILED:Can`t locate servision identified by '2576567567'
  原因:数据库种存的版本号不在迁移文件目录中
  解决方式:删除数据库里面的版本号(即alembic_version表中的数据),然后在执行 :alembic upgrade head

相关