Browse Source

Update 'README.md'

天问 1 month ago
parent
commit
77346df336
1 changed files with 45 additions and 1 deletions
  1. 45 1
      README.md

+ 45 - 1
README.md

@@ -1,3 +1,47 @@
 # alembic
 
-数据库迁移的工具
+数据库迁移的工具
+
+## Usage
+
+
+```
+# 创建一个 SQLAlchemy 模型
+from sqlalchemy import Column, Integer, String
+from sqlalchemy.ext.declarative import declarative_base
+
+Base = declarative_base()
+
+class User(Base):
+    __tablename__ = 'users'
+
+    id = Column(Integer, primary_key=True)
+    username = Column(String(50), unique=True)
+    email = Column(String(100), unique=True)
+
+# 创建一个 Alembic 迁移脚本
+# 使用 Alembic 初始化一个迁移环境
+# alembic init alembic
+
+# 编辑 alembic.ini 文件,设置数据库连接字符串
+sqlalchemy.url = sqlite:///aa.db
+
+# 编辑 alembic/env.py 文件,导入数据库模型
+# from myapp.models import User
+# target_metadata = [User.metadata]
+
+# 创建一个初始迁移脚本
+# alembic revision --autogenerate -m "Initial migration"
+
+# 执行迁移脚本
+# alembic upgrade head
+
+# 添加新的数据库模型或修改现有的模型
+
+# 创建一个新的迁移脚本
+# alembic revision --autogenerate -m "Add new column to User model"
+
+# 执行迁移脚本
+# alembic upgrade head
+
+```