最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【未解决】Flask中管理数据库:升级数据库

database crifan 3263浏览 0评论

之前已经有了一些折腾了:

[已解决]SQLAlchemy的数据库迁移工具:Flask-Migrate vs Alembic

[已解决]SQLAlchemy中添加枚举类型字段

[未解决]Flask中SQLAlchemy中更新数据库同时保持原有数据

好像是:

Flask-Migrate

比较好用,但是之前没有折腾成功。

现在换了个项目,继续去折腾试试。

参考:

flask开发restful api系列(3)–利用alembic进行数据库更改 – 月儿弯弯0204 – 博客园

先去安装:

(RunningFast) ➜  RunningFast pip install flask-migrate
Collecting flask-migrate
Requirement already satisfied (use –upgrade to upgrade): Flask-SQLAlchemy>=1.0 in /root/Envs/RunningFast/lib/python2.7/site-packages (from flask-migrate)
Requirement already satisfied (use –upgrade to upgrade): Flask>=0.9 in /root/Envs/RunningFast/lib/python2.7/site-packages (from flask-migrate)
Collecting alembic>=0.6 (from flask-migrate)
  Downloading alembic-0.8.8.tar.gz (970kB)
    100% |████████████████████████████████| 972kB 67kB/s 
Collecting Flask-Script>=0.6 (from flask-migrate)
Requirement already satisfied (use –upgrade to upgrade): SQLAlchemy>=0.7 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask-SQLAlchemy>=1.0->flask-migrate)
Requirement already satisfied (use –upgrade to upgrade): itsdangerous>=0.21 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask>=0.9->flask-migrate)
Requirement already satisfied (use –upgrade to upgrade): Jinja2>=2.4 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask>=0.9->flask-migrate)
Requirement already satisfied (use –upgrade to upgrade): Werkzeug>=0.7 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask>=0.9->flask-migrate)
Requirement already satisfied (use –upgrade to upgrade): click>=2.0 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask>=0.9->flask-migrate)
Collecting Mako (from alembic>=0.6->flask-migrate)
Collecting python-editor>=0.3 (from alembic>=0.6->flask-migrate)
Requirement already satisfied (use –upgrade to upgrade): MarkupSafe in /root/Envs/RunningFast/lib/python2.7/site-packages (from Jinja2>=2.4->Flask>=0.9->flask-migrate)
Building wheels for collected packages: alembic
  Running setup.py bdist_wheel for alembic … done
  Stored in directory: /root/.cache/pip/wheels/f9/49/bd/de09862fb2ac8d3058b4b2d19ea5dfeb7ac343f6244c30748e
Successfully built alembic
Installing collected packages: Mako, python-editor, alembic, Flask-Script, flask-migrate
Successfully installed Flask-Script-2.0.5 Mako-1.0.4 alembic-0.8.8 flask-migrate-2.0.0 python-editor-1.0.1

安装

flask-migrate-2.0.0

的同时,自动安装了依赖:

Flask-Script-2.0.5 Mako-1.0.4 alembic-0.8.8 python-editor-1.0.1

flask-migrate mysql

Welcome to Flask-Migrate’s documentation! — Flask-Migrate documentation

Flask-SQLAlchemy 和 Flask-Migrate 使用

Flask-Migrate实现数据库迁移 | LinuxHub

然后去执行:

【已解决】如何运行Flask-Migrate的db init

然后继续:

migrate和upgrade

(RunningFast) ➜  RunningFast python db_manager.py db migrate

<div–<——————————————————————————

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:40]:
redis_store=<flask_redis.FlaskRedis object at 0x7f36b7b2ff90>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:43]:
db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in models [/root/RunningFast/runningfast/models.py:13]:
prefix=user-, generatedUuid4Str=30c98629-a2e8-4ecb-b01a-c80b10f585be, newUuid=user-30c98629-a2e8-4ecb-b01a-c80b10f585be

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/RunningFast/runningfast/views.py:21]:
API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:11]:
migrate=<flask_migrate.Migrate object at 0x7f36b5e25710>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:14]:
manager=<flask_script.Manager object at 0x7f36b5e25790>

<div–<——————————————————————————

INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.env] No changes in schema detected.
(RunningFast) ➜  RunningFast python db_manager.py db upgrade

<div–<——————————————————————————

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:40]:
redis_store=<flask_redis.FlaskRedis object at 0x7f770376cf90>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:43]:
db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in models [/root/RunningFast/runningfast/models.py:13]:
prefix=user-, generatedUuid4Str=dd720aaf-a3c8-4715-a678-be8c5a6b23fb, newUuid=user-dd720aaf-a3c8-4715-a678-be8c5a6b23fb

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/RunningFast/runningfast/views.py:21]:
API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:11]:
migrate=<flask_migrate.Migrate object at 0x7f7701a62710>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:14]:
manager=<flask_script.Manager object at 0x7f7701a62790>

<div–<——————————————————————————

INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

参考:

Welcome to Flask-Migrate’s documentation! — Flask-Migrate documentation

“Then each time the database models change repeat the migrate and upgrade commands.”

和:

Flask-Migrate实现数据库迁移 | LinuxHub

当后续数据库字段有改动后,去:

python db_manager.py db migrate -m “add some comments here"
python db_manager.py db upgrade

现在去试试:

目前的字段是:

给User多加一个字段facebookToken试试

class User(db.Model):
    __tablename__ = ‘users’
    id = db.Column(db.String(64), primary_key=True, default = generateUUID("user-"), nullable=False)
    phone = db.Column(db.String(32), nullable=False, default = "")
    email = db.Column(db.String(64), nullable=False, default = "")
    password = db.Column(db.String(20), nullable=False, default = "")
    firstName = db.Column(db.String(64), nullable=False, default = "")
    lastName = db.Column(db.String(64), nullable=False, default = "")
    facebookUserId = db.Column(db.String(64), nullable=False, default = "")
    facebookToken = db.Column(db.String(64), nullable=False, default = "")
    created_at = db.Column(db.DateTime, nullable=False, default = datetime.now)
    modified_at = db.Column(db.DateTime, nullable=False, default = datetime.now, onupdate = datetime.now)
    def __repr__(self):
        return ‘<User:firstName=%r,lastName=%r,id=%s,phone=%s,email=%s,created_at=%s,modified_at=%s>’ % (self.firstName, self.lastName, self.id, self.phone, self.created_at, self.modified_at)

然后去升级试试:

(RunningFast) ➜  RunningFast python db_manager.py db migrate
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added column ‘users.facebookToken’
  Generating /root/RunningFast/migrations/versions/45fd973f6f12_.py … done
(RunningFast) ➜  RunningFast python db_manager.py db upgrade
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> 45fd973f6f12, empty message

可见:

python db_manager.py db migrate

检测到了:

新增了一个字段:

users.facebookToken

然后:

python db_manager.py db upgrade

去升级了对应的字段。

然后去看看数据库是否变化,是否新增了该字段:

果然变化了,新增了对应的字段:

然后再去看看对应的配置文件:

(RunningFast) ➜  RunningFast ll migrations 
total 24K
-rw-r–r– 1 root root  770 Oct 10 18:02 alembic.ini
-rwxr-xr-x 1 root root 2.9K Oct 10 18:02 env.py
-rw-r–r– 1 root root 2.5K Oct 10 18:08 env.pyc
-rwxr-xr-x 1 root root   38 Oct 10 18:02 README
-rwxr-xr-x 1 root root  412 Oct 10 18:02 script.py.mako
drwxr-xr-x 2 root root 4.0K Oct 10 19:46 versions
(RunningFast) ➜  RunningFast cat migrations/versions/45fd973f6f12_.py
"""empty message
Revision ID: 45fd973f6f12
Revises: None
Create Date: 2016-10-10 19:46:42.439605
"""
# revision identifiers, used by Alembic.
revision = ’45fd973f6f12′
down_revision = None
from alembic import op
import sqlalchemy as sa
def upgrade():
    ### commands auto generated by Alembic – please adjust! ###
    op.add_column(‘users’, sa.Column(‘facebookToken’, sa.String(length=64), nullable=False))
    ### end Alembic commands ###
def downgrade():
    ### commands auto generated by Alembic – please adjust! ###
    op.drop_column(‘users’, ‘facebookToken’)
    ### end Alembic commands ###

可以看到:

upgrade是调用op.add_column去添加了对应的字段

downgrade是调用了op.drop_column删除对应的字段

然后再去试试,downgrade:

(RunningFast) ➜  RunningFast python db_manager.py db downgrade
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running downgrade 45fd973f6f12 -> , empty message

果然,就又删除了对应的字段了:

看看当前支持哪些操作:

(RunningFast) ➜  RunningFast python db_manager.py db –help          
usage: Perform database migrations
Perform database migrations
positional arguments:
  {upgrade,heads,merge,migrate,stamp,show,current,edit,init,downgrade,branches,history,revision}
    upgrade             Upgrade to a later version
    heads               Show current available heads in the script directory
    merge               Merge two revisions together. Creates a new migration
                        file
    migrate             Alias for ‘revision –autogenerate’
    stamp               ‘stamp’ the revision table with the given revision;
                        don’t run any migrations
    show                Show the revision denoted by the given symbol.
    current             Display the current revision for each database.
    edit                Edit current revision.
    init                Creates a new migration repository
    downgrade           Revert to a previous version
    branches            Show current branch points
    history             List changeset scripts in chronological order.
    revision            Create a new revision file.
optional arguments:
  -?, –help            show this help message and exit

更多语法,详见:

Welcome to Flask-Migrate’s documentation! — Flask-Migrate documentation

  • flask db –help
    Shows a list of available commands.
  • flask db init [–multidb]
    Initializes migration support for the application. The optional <span style="font-size: 17px; color: rgb(62, 67, 73); font-family: Georgia, serif; font-style: normal; font-variant-caps: normal; font-weight: normal;"–<multidbenables migrations for multiple databases configured as Flask-SQLAlchemy binds.
  • flask db revision [–message MESSAGE] [–autogenerate] [–sql] [–head HEAD] [–splice] [–branch-labelBRANCH_LABEL] [–version-path VERSION_PATH] [–rev-idREV_ID]
    Creates an empty revision script. The script needs to be edited manually with the upgrade and downgrade changes. See Alembic’s documentation for instructions on how to write migration scripts. An optional migration message can be included.
  • flask db migrate [–message MESSAGE] [–sql] [–head HEAD][–splice] [–branch-label BRANCH_LABEL] [–version-pathVERSION_PATH] [–rev-id REV_ID]
    Equivalent to revision –autogenerate. The migration script is populated with changes detected automatically. The generated script should to be reviewed and edited as not all types of changes can be detected automatically. This command does not make any changes to the database, just creates the revision script.
  • flask db edit <revision>
    Edit a revision script using $EDITOR.
  • flask db upgrade [–sql] [–tag TAG] [–x-arg ARG]<revision>
    Upgrades the database. If revision isn’t given then "head" is assumed.
  • flask db downgrade [–sql] [–tag TAG] [–x-arg ARG]<revision>
    Downgrades the database. If revision isn’t given then -1 is assumed.
  • flask db stamp [–sql] [–tag TAG] <revision>
    Sets the revision in the database to the one given as an argument, without performing any migrations.
  • flask db current [–verbose]
    Shows the current revision of the database.
  • flask db history [–rev-range REV_RANGE] [–verbose]
    Shows the list of migrations. If a range isn’t given then the entire history is shown.
  • flask db show <revision>
    Show the revision denoted by the given symbol.
  • flask db merge [–message MESSAGE] [–branch-labelBRANCH_LABEL] [–rev-id REV_ID] <revisions>
    Merge two revisions together. Creates a new revision file.
  • flask db heads [–verbose] [–resolve-dependencies]
    Show current available heads in the revision script directory.
  • flask db branches [–verbose]
    Show current branch points.

转载请注明:在路上 » 【未解决】Flask中管理数据库:升级数据库

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
81 queries in 0.202 seconds, using 22.13MB memory