flaskmigrate的使用

编程

manager.py代码如下:

from flask_script import Manager, Shell, Server

from flask_migrate import Migrate, MigrateCommand

from app.ext import db

from app import create_app

app = create_app()

manager = Manager(app=app)

migrate = Migrate(app, db)

def make_shell_context():

return dict(app=app)

manager.add_command("shell", Shell(make_context=make_shell_context))

manager.add_command("run_dev", Server(use_debugger=True, use_reloader=True))

manager.add_command("runserver", Server())

manager.add_command("db", MigrateCommand)

@manager.command

def deploy():

"""run deployment tasks"""

pass

if __name__ == "__main__":

manager.run(default_command="runserver")

执行python manager.py db 查看参数说明

@MacBook-Pro-2 Garen % python manager.py db

usage: Perform database migrations

Perform database migrations

positional arguments:

{init,revision,migrate,edit,merge,upgrade,downgrade,show,history,heads,branches,current,stamp}

init Creates a new migration repository

revision Create a new revision file.

migrate Alias for "revision --autogenerate"

edit Edit current revision.

merge Merge two revisions together. Creates a new migration

file

upgrade Upgrade to a later version

downgrade Revert to a previous version

show Show the revision denoted by the given symbol.

history List changeset scripts in chronological order.

heads Show current available heads in the script directory

branches Show current branch points

current Display the current revision for each database.

stamp "stamp" the revision table with the given revision;

don"t run any migrations

创建数据库操作

  • 初始化数据库

init命令会创建一个新的版本仓库,并且会生成migrations文件夹.这点类似django.

(Garen) zhouwanghua@MacBook-Pro-2 Garen % python manager.py db init

Creating directory /Users/zhouwanghua/Code/Garen/migrations ... done

Creating directory /Users/zhouwanghua/Code/Garen/migrations/versions ... done

Generating /Users/zhouwanghua/Code/Garen/migrations/script.py.mako ... done

Generating /Users/zhouwanghua/Code/Garen/migrations/env.py ... done

Generating /Users/zhouwanghua/Code/Garen/migrations/README ... done

Generating /Users/zhouwanghua/Code/Garen/migrations/alembic.ini ... done

Please edit configuration/connection/logging settings in "/Users/zhouwanghua/Code/Garen/migrations/alembic.ini"

before proceeding.

  • 创建version表

执行upgrade之后会在数据中创建一个alembic_version表

(Garen) zhouwanghua@MacBook-Pro-2 Garen % python manager.py db upgrade

INFO [alembic.runtime.migration] Context impl MySQLImpl.

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

  • 生成models对象的迁移脚本

如果有新的表或字段改动都需要使用这个命令,类似django的makemigrations

(Garen) zhouwanghua@MacBook-Pro-2 Garen % python 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 table "reply"

Generating /Users/zhouwanghua/Code/Garen/migrations/versions/d5e95964b986_.py ... done

  • 在执行迁移命令upgrade

执行迁移脚本,创建/维护表

(Garen) zhouwanghua@MacBook-Pro-2 Garen % python 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 -> d5e95964b986, empty message

总结:

flask-migrate与django自带的orm的理念是一样的.migrate == django的makemigrations upgrade==django的migrate

以上是 flaskmigrate的使用 的全部内容, 来源链接: utcz.com/z/518789.html

回到顶部