实践操作alembic数据库迁移增删改查

前言:开发过程中需要用到,这边实践操作alembic数据库迁移增删改查

根据模型model类生成数据表

alembic revision --autogenerate -m "initial migration"

执行完上面的命令之后,alembic目录中会多出对应commit的文件,如下图所示

应用当前数据表alembic commit更新操作

alembic upgrade head

此时的数据库结构已经同步到最新情况,如下图所示

当执行完revision操作之后,对应的数据库中会有一个alembic_version表来记录版本号信息,如下图所示

注意点,如果在执行alembic upgrade head的时候提示type "vector" does not exist情况,如下图所示

记得在pg数据库中安装相关扩展即可

CREATE EXTENSION IF NOT EXISTS vector;

现有数据库中添加新模型model数据表

此时有一个新的业务需求,需要编写一个新数据表对应的model,如下所示

class ModelTest(Base):
__tablename__ = "modeltest"
__table_args__ = {'comment': '指纹测试表'}
id = Column(Integer, primary_key=True, index=True, comment="指纹ID")
product_id = Column(Integer, ForeignKey("products.id"), comment="产品ID")
content = Column(Text, comment="指纹内容")
updated_at = Column(DateTime(timezone=True), onupdate=func.now(),server_default=func.now(),comment="更新时间")
created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="记录创建时间")
deleted_at = Column(DateTime(timezone=True), comment="记录删除时间")
product = relationship("Product", back_populates="fingerprint")

重新进行revision操作,执行如下命令

alembic revision --autogenerate -m "add modeltest demo"

应用当前数据表alembic commit更新操作

alembic upgrade head

重新查看数据库,可以看到对应的modeltest已经出现了,如下图所示

修改现在数据表字段

原modeltest类的定义如下所示

class ModelTest(Base):
__tablename__ = "modeltest"
__table_args__ = {'comment': '指纹测试表'}
id = Column(Integer, primary_key=True, index=True, comment="指纹ID")
product_id = Column(Integer, ForeignKey("products.id"), comment="产品ID")
content = Column(Text, comment="指纹内容")
updated_at = Column(DateTime(timezone=True), onupdate=func.now(),server_default=func.now(),comment="更新时间")
created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="记录创建时间")
deleted_at = Column(DateTime(timezone=True), comment="记录删除时间")
product = relationship("Product", back_populates="fingerprint")

添加一个新的content2字段来进行定义,如下所示

class ModelTest(Base):
__tablename__ = "modeltest"
__table_args__ = {'comment': '指纹测试表'}
id = Column(Integer, primary_key=True, index=True, comment="指纹ID")
product_id = Column(Integer, ForeignKey("products.id"), comment="产品ID")
content = Column(Text, comment="指纹内容")
content2 = Column(Text, comment="指纹内容2")
updated_at = Column(DateTime(timezone=True), onupdate=func.now(),server_default=func.now(),comment="更新时间")
created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="记录创建时间")
deleted_at = Column(DateTime(timezone=True), comment="记录删除时间")
product = relationship("Product", back_populates="fingerprint")

接着执行revision命令,如下所示

alembic revision --autogenerate -m "alter content2 from ModelTest"

alembic upgrade head

重新观察数据表结构,可以看到多了对应的content2字段信息,如下图所示

alembic history记录追溯

查看迁移记录信息

alembic history

当我把587444e5486f (head)对应文件删除之后,可以看到由于head记录无法找到会出现无法定位报错情况

alembic revision --autogenerate -m "add modeltest demo2"

接着把删除的head文件重新放回,然后重新执行,可以看到此时已经可以重新进行迁移了,由此证明alembic追溯history是通过versions目录来进行实现的

alembic revision --autogenerate -m "add modeltest demo2"

历史记录丢失

当alembic_version表中对不上当前的的版本号的时候,就会出现下面的问题

alembic revision --autogenerate -m "initial migration"

此时解决的方法只能是重新映射数据库

alembic stamp head

alembic revision --autogenerate -m "initial migration2222222"

posted @   zpchcbd  阅读(151)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2021-12-10 hyscan 基于内网信息搜集/利用工具
点击右上角即可分享
微信分享提示