Simple guide for using standard Flask CLI commands for database migrations.
Flask CLI Commands:
flask db init- Set up migrations (run once)flask db migrate- Create new migrationsflask db upgrade- Apply migrationsflask db current- Show current statusflask db history- Show migration historyflask db downgrade- Rollback migrations
```bash export FLASK_APP=app.py export DATABASE_URL="postgresql://user:pass@host:port/db" ```
```bash flask db init ```
```bash flask db migrate -m "Initial migration" flask db upgrade ```
Step 1: Edit model in api/index.py:
```python
class User(db.Model):
# existing fields...
avatar_url = db.Column(db.String(255), nullable=True) # ADD THIS
```
Step 2: Create migration: ```bash flask db migrate -m "Add avatar_url to users" ```
Step 3: Apply migration: ```bash flask db upgrade ```
Step 4: Deploy: ```bash git add migrations/ git commit -m "Add avatar field" vercel --prod ```
Step 1: Add model in api/index.py:
```python
class Category(db.Model):
tablename = 'categories'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False, unique=True)
slug = db.Column(db.String(50), nullable=False, unique=True)
description = db.Column(db.Text, nullable=True)
color = db.Column(db.String(7), nullable=True) # Hex color code
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# Relationship
posts = db.relationship('Post', backref='category', lazy=True)
class Post(db.Model): # existing fields... category_id = db.Column(db.Integer, db.ForeignKey('categories.id'), nullable=True) # ADD THIS ```
Step 2: Create migration: ```bash flask db migrate -m "Create categories table and add category_id to posts" ```
Step 3: Generated migration file: ```python def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table('categories', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(length=50), nullable=False), sa.Column('slug', sa.String(length=50), nullable=False), sa.Column('description', sa.Text(), nullable=True), sa.Column('color', sa.String(length=7), nullable=True), sa.Column('created_at', sa.DateTime(), nullable=True), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('name'), sa.UniqueConstraint('slug') )
op.add_column('posts', sa.Column('category_id', sa.Integer(), nullable=True))
op.create_foreign_key(None, 'posts', 'categories', ['category_id'], ['id'])
# ### end Alembic commands ###
def downgrade(): # ### commands auto generated by Alembic - please adjust! ### op.drop_constraint(None, 'posts', type_='foreignkey') op.drop_column('posts', 'category_id') op.drop_table('categories') # ### end Alembic commands ### ```
Step 4: Apply migration: ```bash flask db upgrade ```
Step 5: Deploy: ```bash git add migrations/ git commit -m "Add categories table and post categorization" vercel --prod ```
Check current status: ```bash flask db current flask db history ```
Rollback to previous: ```bash flask db downgrade ```
```bash flask db migrate -m "Description here" ``` What it does: Compares models with database and creates migration
```bash flask db upgrade ``` What it does: Runs all pending migrations
```bash flask db current # Show current revision flask db history # Show all migrations ```
```bash flask db downgrade # Go back one migration flask db downgrade # Go to specific revision ```
```bash
export FLASK_APP=app.py
flask db migrate -m "Add new feature"
flask db upgrade
flask db current ```
```bash
git add migrations/versions/ git commit -m "Add migration"
vercel --prod
```
```bash
export FLASK_APP=app.py
pip install Flask-Migrate ```
```bash
flask db merge -m "Merge migrations" flask db upgrade ```
```bash
flask db current flask db heads
flask db upgrade ```
```bash
flask db migrate -m "Add user email verification" flask db migrate -m "Create posts table"
flask db migrate -m "Update" flask db migrate -m "Changes" ```
```bash
cat migrations/versions/latest_migration.py
flask db upgrade
flask db current ```
```bash
echo "FLASK_APP=app.py" >> .env echo "DATABASE_URL=your-database-url" >> .env
pip install python-dotenv ```
- Industry standard - Official Flask approach
- Well documented - Lots of tutorials available
- Simple syntax - Familiar
flask dbcommands - Auto-migration ready - Works with Vercel deployment
This approach follows Flask best practices and integrates seamlessly with the auto-migration system!