SQLAlchemy是一款很是優秀的ORM框架,可是自己沒有帶數據庫版本控制功能,這很不方便,進行開發過程當中不免修改數據模型,添加一個表,修改一個字段,都須要手動修改的話就比較費事了,還不如不用SQLAlchemy呢。python
在這裏介紹一款SQLAlchemy做者寫的數據庫版本控制工具---Alembic。另外還有一個工具叫作SQLAlchemy-Migrate,在使用過程當中感受Alembic更爲靈活。mysql
$ pip3 install alembic
使用以前,先在項目根目錄進行初始化。linux
$ alembic init alembic
初始化完成後,會生成一個alembic.ini配置文件及一個alembic目錄。sql
#!/usr/bin/env python # -*- coding:utf-8 -*- # File Name : sb.py # Author : hexm # Mail : xiaoming.unix@gmail.com # Created Time : 2017-03-29 20:03 from sqlalchemy import Column, Integer, String, ForeignKey, Date, Table, Text from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship Base = declarative_base() # 用戶id對應角色id 多對多 useridToRoleid = Table('useridToRoleid', Base.metadata, Column('userid', Integer, ForeignKey('users.id')), Column('roleid', Integer, ForeignKey('roles.id')), ) # 角色id對應權限id 多對多 roleidToIdentityid = Table('roleidToIdentityid', Base.metadata, Column('roleid', Integer, ForeignKey('roles.id')), Column('identityid', Integer, ForeignKey('identities.id')), ) # 文章id和標籤id 多對多 articleidToTagid = Table('articleidToTagid', Base.metadata, Column('articleid', Integer, ForeignKey('articles.id')), Column('tagid', Integer, ForeignKey('tags.id')), ) class User(Base): """ 用戶表 """ __tablename__ = 'users' id = Column(Integer, primary_key=True) username = Column(String(32), nullable=False, unique=True) password = Column(String(32), nullable=False) email = Column(String(32), nullable=False, unique=True) roles = relationship('Role', secondary=useridToRoleid, backref='users') def __repr__(self): return "<%s users.username: %s>" % (self.id, self.username) class Role(Base): """ 角色表 """ __tablename__ = 'roles' id = Column(Integer, primary_key=True) name = Column(String(16), nullable=False) identity = relationship('Identity', secondary=roleidToIdentityid, backref='roles') def __repr__(self): return "<%s roles.name: %s>" % (self.id, self.name) class Identity(Base): """ 權限表 """ __tablename__ = 'identities' id = Column(Integer, primary_key=True) name = Column(String(16)) def __repr__(self): return "<%s identities.name: %s>" % (self.id, self.name) class Category(Base): __tablename__ = "categories" id = Column(Integer, primary_key=True) name = Column(String(16), nullable=False) title = relationship('Article', backref='category') def __repr__(self): return "<%s categories.name: %s" % (self.id, self.name) class Tag(Base): __tablename__ = "tags" id = Column(Integer, primary_key=True) name = Column(String(16)) def __repr__(self): return "<%s categories.name: %s" % (self.id, self.name) class Article(Base): __tablename__ = "articles" id = Column(Integer, primary_key=True) title = Column(String(50)) description = Column(String(300)) content = Column(Text) click_count = Column(Integer, default=0) category_id = Column(Integer, ForeignKey('categories.id')) tags = relationship('Tag', secondary=articleidToTagid, backref='articles') def __repr__(self): return "<%s categories.name: %s" % (self.id, self.title)
修改alembic.ini配置文件,只修改數據庫鏈接部分便可,數據庫
將app
sqlalchemy.url = driver://user:pass@localhost:port/dbname
修改成框架
sqlalchemy.url = mysql+pymysql://root:@localhost/linux_study
修改alembic/env.pyide
將工具
target_metadata = None
修改成url
import sys from os.path import abspath, dirname sys.path.append(dirname(dirname(abspath(__file__)))) from modules.models import Base target_metadata = Base.metadata
使用alembic revision -m "註釋" 建立數據庫版本,上面咱們修改了配置文件alembic/env.py,指定了target_metadata,這裏可使用--autogenerate參數自動生成遷移腳本。
$ alembic revision --autogenerate -m "initdb"
更新數據庫
$ alembic upgrade 版本號
更新到最新版
alembic upgrade head
降級數據庫
$ alembic downgrade 版本號
更新到最第一版
alembic downgrade head
離線更新(生成sql)
alembic upgrade 版本號 --sql > migration.sql
從特定起始版本生成sql
alembic upgrade 版本一:版本二 --sql > migration.sql
查詢當前數據庫版本號
查看alembic_version表。
清除全部版本
將versions刪掉,並刪除alembic_version表