|
1 |
| -# docs-flask-db-migrations |
| 1 | +# Flask Database Migrations with SQLAlchemy & Flask-Migrate |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +**Flask-Migrate** is an extension that handles database migrations for Flask applications using **Alembic**. It works alongside **SQLAlchemy**, which is an ORM (Object Relational Mapper) that maps Python objects to database tables. |
| 6 | + |
| 7 | +Using Flask-Migrate, you can easily handle version control for your database schema, allowing for smooth database changes and rollbacks over time. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## Setup for Different DBMS |
| 12 | + |
| 13 | +Flask-Migrate supports different database management systems. Below are the steps to configure SQLite, MySQL, and PostgreSQL. |
| 14 | + |
| 15 | +### SQLite |
| 16 | + |
| 17 | +SQLite is a lightweight, file-based database suitable for small projects or testing. |
| 18 | + |
| 19 | +1. **Install dependencies**: |
| 20 | + |
| 21 | + ```bash |
| 22 | + pip install Flask-SQLAlchemy Flask-Migrate |
| 23 | + ``` |
| 24 | + |
| 25 | +2. **Update `config.py`**: |
| 26 | + |
| 27 | + ```python |
| 28 | + # config.py |
| 29 | + class Config: |
| 30 | + SQLALCHEMY_DATABASE_URI = 'sqlite:///app.db' # SQLite URI |
| 31 | + SQLALCHEMY_TRACK_MODIFICATIONS = False |
| 32 | + ``` |
| 33 | + |
| 34 | +3. **Initialize Flask-Migrate** in `app.py`: |
| 35 | + |
| 36 | + ```python |
| 37 | + from flask import Flask |
| 38 | + from flask_sqlalchemy import SQLAlchemy |
| 39 | + from flask_migrate import Migrate |
| 40 | + |
| 41 | + app = Flask(__name__) |
| 42 | + app.config.from_object('config.Config') |
| 43 | + |
| 44 | + db = SQLAlchemy(app) |
| 45 | + migrate = Migrate(app, db) |
| 46 | + ``` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +### MySQL |
| 51 | + |
| 52 | +1. **Install the MySQL client**: |
| 53 | + |
| 54 | + ```bash |
| 55 | + pip install mysqlclient |
| 56 | + ``` |
| 57 | + |
| 58 | +2. **Update `config.py`**: |
| 59 | + |
| 60 | + ```python |
| 61 | + # config.py |
| 62 | + class Config: |
| 63 | + SQLALCHEMY_DATABASE_URI = 'mysql://username:password@localhost/db_name' # MySQL URI |
| 64 | + SQLALCHEMY_TRACK_MODIFICATIONS = False |
| 65 | + ``` |
| 66 | + |
| 67 | +3. **Ensure MySQL is running** and the database exists: |
| 68 | + |
| 69 | + ```bash |
| 70 | + mysql -u username -p |
| 71 | + CREATE DATABASE db_name; |
| 72 | + ``` |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +### PostgreSQL |
| 77 | + |
| 78 | +1. **Install the PostgreSQL client**: |
| 79 | + |
| 80 | + ```bash |
| 81 | + pip install psycopg2 |
| 82 | + ``` |
| 83 | + |
| 84 | +2. **Update `config.py`**: |
| 85 | + |
| 86 | + ```python |
| 87 | + # config.py |
| 88 | + class Config: |
| 89 | + SQLALCHEMY_DATABASE_URI = 'postgresql://username:password@localhost/db_name' # PostgreSQL URI |
| 90 | + SQLALCHEMY_TRACK_MODIFICATIONS = False |
| 91 | + ``` |
| 92 | + |
| 93 | +3. **Ensure PostgreSQL is running** and create the database: |
| 94 | + |
| 95 | + ```bash |
| 96 | + psql -U username |
| 97 | + CREATE DATABASE db_name; |
| 98 | + ``` |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## Flask Migrate Full Kickoff |
| 103 | + |
| 104 | +To set up **Flask-Migrate** and apply migrations to your database, follow these steps: |
| 105 | + |
| 106 | +1. **Clone the Git repository**: |
| 107 | + |
| 108 | + ```bash |
| 109 | + git clone https://github.com/app-generator/docs-flask-db-migrations |
| 110 | + cd docs-flask-db-migrations |
| 111 | + ``` |
| 112 | + |
| 113 | +2. **Create and activate a virtual environment**: |
| 114 | + |
| 115 | + ```bash |
| 116 | + python3 -m venv venv |
| 117 | + source venv/bin/activate # On Windows, use venv\Scripts\activate |
| 118 | + ``` |
| 119 | + |
| 120 | +3. **Install required packages**: |
| 121 | + |
| 122 | + ```bash |
| 123 | + pip install -r requirements.txt |
| 124 | + ``` |
| 125 | + |
| 126 | +4. **Configure your database** in `config.py` (choose SQLite, MySQL, or PostgreSQL). |
| 127 | + |
| 128 | +5. **Initialize Flask-Migrate**: |
| 129 | + |
| 130 | + ```bash |
| 131 | + flask db init |
| 132 | + ``` |
| 133 | + |
| 134 | +6. **Create your models** in `models.py`: |
| 135 | + |
| 136 | + ```python |
| 137 | + from app import db |
| 138 | + |
| 139 | + class User(db.Model): |
| 140 | + id = db.Column(db.Integer, primary_key=True) |
| 141 | + username = db.Column(db.String(150), unique=True, nullable=False) |
| 142 | + email = db.Column(db.String(150), unique=True, nullable=False) |
| 143 | + ``` |
| 144 | + |
| 145 | +7. **Create the initial migration**: |
| 146 | + |
| 147 | + ```bash |
| 148 | + flask db migrate -m "Initial migration" |
| 149 | + ``` |
| 150 | + |
| 151 | +8. **Apply the migration**: |
| 152 | + |
| 153 | + ```bash |
| 154 | + flask db upgrade |
| 155 | + ``` |
| 156 | + |
| 157 | +9. **Run the application**: |
| 158 | + |
| 159 | + ```bash |
| 160 | + flask run |
| 161 | + ``` |
| 162 | + |
| 163 | + Your Flask app will be running at `http://127.0.0.1:5000`. |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## Common Flask-Migrate Commands |
| 168 | + |
| 169 | +- **`flask db init`**: Initializes the migration directory (`migrations/`). |
| 170 | +- **`flask db migrate -m "message"`**: Generates a new migration script. |
| 171 | +- **`flask db upgrade`**: Applies the migration to the database. |
| 172 | +- **`flask db downgrade`**: Rolls back the last migration. |
| 173 | +- **`flask db history`**: Shows the migration history. |
| 174 | +- **`flask db stamp head`**: Marks the current database as being at the latest migration. |
| 175 | + |
| 176 | +--- |
| 177 | + |
| 178 | +## Conclusion |
| 179 | + |
| 180 | +In this guide, we’ve covered how to set up **Flask-Migrate** with **SQLAlchemy** for managing database migrations in Flask applications. We’ve also shown how to configure different databases (SQLite, MySQL, PostgreSQL) and run the Flask application with migrations. |
| 181 | + |
| 182 | +By using Flask-Migrate, you can handle database schema changes in a more structured and controlled way. This approach simplifies managing database evolution over time, especially as your project grows. |
| 183 | + |
| 184 | +Feel free to explore the code in the repository and follow the steps to integrate Flask-Migrate into your own Flask applications. |
| 185 | + |
| 186 | +--- |
0 commit comments