用 Flask 来写个轻博客 (8) — (M)VC_Alembic 管理数据库结构的升级和降级
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog
目录
前文列表
用 Flask 来写个轻博客 (1) — 创建项目
用 Flask 来写个轻博客 (2) — Hello World!
用 Flask 来写个轻博客 (3) — (M)VC_连接 MySQL 和 SQLAlchemy
用 Flask 来写个轻博客 (4) — (M)VC_创建数据模型和表
用 Flask 来写个轻博客 (5) — (M)VC_SQLAlchemy 的 CRUD 详解
用 Flask 来写个轻博客 (6) — (M)VC_models 的关系(one to many)
用 Flask 来写个轻博客 (7) — (M)VC_models 的关系(many to many)
扩展阅读
Openstack_SQLAlchemy 修改数据库的表结构
Openstack_SQLAlchemy_一对多关系表的多表插入实现
Alembic
大多数情况下,在开始实现一个新项目的代码之前都是先完成数据库的设计。但随着项目新功能的增加或需求变更,不可避免的数据模型的修改会贯穿项目开发的始终。这里会出现一个问题:当把这些数据模型的更新从开发环境迁移到生产环境时,怎样才能在保证原有数据完整性的情况下更新的数据库结构或将数据库回滚到之前的某一个时刻以便复现环境。
Alembic(Database migration 数据迁移跟踪记录) 提供的数据库升级和降级的功能,就可以帮我们解决上述的问题。它所能实现的效果有如 Git 管理项目代码一般。
- 在这里我们使用 Flask 的扩展 Flask-Migrate
NOTE:要在 virtualenv 的环境下安装
pip install Flask-Migrate
pip freeze > requirements.txt
- 生成 manager db 指令
在 manage.py 文件中 Create a new commands:manager.add_command("db", MigrateCommand)
from flask.ext.script import Manager, Server
from flask.ext.migrate import Migrate, MigrateCommand
import main
import models
# Init manager object via app object
manager = Manager(main.app)
# Init migrate object via app and db object
migrate = Migrate(main.app, models.db)
# Create some new commands
manager.add_command("server", Server())
manager.add_command("db", MigrateCommand)
@manager.shell
def make_shell_context():
"""Create a python CLI.
return: Default import object
type: `Dict`
"""
return dict(app=main.app,
db=models.db,
User=models.User,
Post=models.Post,
Comment=models.Comment,
Tag=models.Tag)
if __name__ == '__main__':
manager.run()
查看指令 manager db 的可用选项
(env) [root@flask-dev JmilkFan-s-Blog]# python manage.py db
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.
初始化 DB Migrate
(env) [root@flask-dev JmilkFan-s-Blog]# python manage.py db init
Creating directory /opt/JmilkFan-s-Blog/migrations ... done
Creating directory /opt/JmilkFan-s-Blog/migrations/versions ... done
Generating /opt/JmilkFan-s-Blog/migrations/README ... done
Generating /opt/JmilkFan-s-Blog/migrations/alembic.ini ... done
Generating /opt/JmilkFan-s-Blog/migrations/env.py ... done
Generating /opt/JmilkFan-s-Blog/migrations/script.py.mako ... done
Generating /opt/JmilkFan-s-Blog/migrations/env.pyc ... done
Please edit configuration/connection/logging settings in '/opt/JmilkFan-s-Blog/migrations/alembic.ini' before proceeding.
在初始化数据库更新任务之后会创建一个 migrations 目录,所有的更改记录文件(这个记录文件本来就是 Python 文件)都会被保存在该目录下。
开始第一次跟踪
(env) [root@flask-dev JmilkFan-s-Blog]# python manage.py db migrate -m "Initial migration"
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.env] No changes in schema detected.
该指令会让 Alembic 扫描所有的 SQLAlchemy 对象,并将没有记录过的行和列记录成为一个 python 文件并保存到 migrations/versions 路径下。
将记录文件应用到数据库中(实时升级数据库结构)
NOTE 1: 执行该指令的前提是 migrations/versions 目录下必须存在记录文件。
NOTE 2: 而且这些记录文件是我们能够手动修改和创建的,这也是实时更新数据库结构的方法。将需要修改的数据库结构的更新内容,手动的写入到记录文件中,然后执行 upgrade 指令就能够实现数据库的更新,而且这些更新是可以批量的作用于数据库中,不需要使用一条一条的 SQL 语句来进行修改。
(env) [root@flask-dev JmilkFan-s-Blog]# python manage.py db upgrade
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL
回滚到某一个记录环境中
用法于 Git 非常相似:
# 获取 History ID
(env) [root@flask-dev JmilkFan-s-Blog]# python manage.py db history
# 回滚到某个 history
(env) [root@flask-dev JmilkFan-s-Blog]# python manage.py db downgrade <history_id>
用 Flask 来写个轻博客 (8) — (M)VC_Alembic 管理数据库结构的升级和降级的更多相关文章
- 用 Flask 来写个轻博客
用 Flask 来写个轻博客 用 Flask 来写个轻博客 (1) — 创建项目 用 Flask 来写个轻博客 (2) — Hello World! 用 Flask 来写个轻博客 (3) — (M)V ...
- 用 Flask 来写个轻博客 (37) — 在 Github 上为第一阶段的版本打 Tag
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 第一阶段结语 打 Tag 前文列表 用 Flask 来写个轻博客 (1 ...
- 用 Flask 来写个轻博客 (36) — 使用 Flask-RESTful 来构建 RESTful API 之五
目录 目录 前文列表 PUT 请求 DELETE 请求 测试 对一条已经存在的 posts 记录进行 update 操作 删除一条记录 前文列表 用 Flask 来写个轻博客 (1) - 创建项目 用 ...
- 用 Flask 来写个轻博客 (35) — 使用 Flask-RESTful 来构建 RESTful API 之四
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 POST 请求 身份认证 测试 前文列表 用 Flask 来写个轻博客 ...
- 用 Flask 来写个轻博客 (34) — 使用 Flask-RESTful 来构建 RESTful API 之三
目录 目录 前文列表 应用请求中的参数实现 API 分页 测试 前文列表 用 Flask 来写个轻博客 (1) - 创建项目 用 Flask 来写个轻博客 (2) - Hello World! 用 F ...
- 用 Flask 来写个轻博客 (33) — 使用 Flask-RESTful 来构建 RESTful API 之二
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 构建 RESTful Flask API 定义资源路由 格式 ...
- 用 Flask 来写个轻博客 (32) — 使用 Flask-RESTful 来构建 RESTful API 之一
目录 目录 前文列表 扩展阅读 RESTful API REST 原则 无状态原则 面向资源 RESTful API 的优势 REST 约束 前文列表 用 Flask 来写个轻博客 (1) - 创建项 ...
- 用 Flask 来写个轻博客 (31) — 使用 Flask-Admin 实现 FileSystem 管理
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 编写 FileSystem Admin 页面 Flask-A ...
- 用 Flask 来写个轻博客 (30) — 使用 Flask-Admin 增强文章管理功能
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 实现文章管理功能 实现效果 前文列表 用 Flask 来写个 ...
随机推荐
- Batch批处理中的 参数 路径和字符串 处理详解
1.截取字符串 截取字符串可以说是字符串处理功能中最常用的一个子功能了,能够实现截取字符串中的特定位置的一个或多个字符.举例说明其基本功能: @echo off set ifo=abcdefghijk ...
- VSphere随笔 - vCenter6.5安装报错 “Failed to authenticate with the guest operating system using the supplied“
今天重新安装VCSA,安装多次一直卡在80%的画面不动,显示正在安装RPM包,同时log日志显示“Failed to authenticate with the guest operating sys ...
- 测开之路五十六:实现类似unittest的断言
import inspect class Case(object): """ 实现断言 """ def __init__(self): se ...
- 【Web API]无法添加AttributeRoutes的解决方案
1.按照微软官方文档,如果要使用AttributeRoutes,需要在APP_START里的WebApiConfig.cs的Register方法中添加一行:config.MapHttpAttribut ...
- setTimeout,clearTimeout,setInterval,clearInteral详解
设置定时器,在一段时间之后执行指定的代码,setTimeout与setInterval的区别在于setTimeout函数指定的代码仅执行一次 方法一: window.setTimeout(" ...
- selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取
代码要多敲 注释要清晰 哪怕再简单 #使用selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取 #地址:https://music.douban.com/chart #导入需要的模块 f ...
- linux下用户切换
Linux学习使用ubuntu17,ubuntu安装的时候没有超级用户root的密码. 设置系统root用户的密码,Ubuntu刚安装后,因为root没有默认密码,需要手动设定.以安装ubuntu时输 ...
- Ubuntu解压缩rar格式文件
解压缩rar文件时,出现问题 解决方法: sudo apt-get install unrar
- 57.Queue Reconstruction by Height(按身高重建对列)
Level: Medium 题目描述: Suppose you have a random list of people standing in a queue. Each person is d ...
- Python之随机选择 random
随机选择:random import random # 从一个序列中随机的抽取一个元素 values=[1,2,3,4,56] # 指定取出N个不同元素 print(random.sample(val ...