0
# Core Extension
1
2
The Migrate class is the main Flask extension that integrates database migration functionality into Flask applications. It provides the foundation for all migration operations and configuration management.
3
4
## Capabilities
5
6
### Migrate Class
7
8
Main migration manager class that integrates with Flask applications to provide database migration functionality.
9
10
```python { .api }
11
class Migrate:
12
def __init__(self, app=None, db=None, directory='migrations', command='db',
13
compare_type=True, render_as_batch=True, **kwargs):
14
"""
15
Initialize migration manager.
16
17
Parameters:
18
- app: Flask application instance (optional for deferred initialization)
19
- db: SQLAlchemy database instance (optional for deferred initialization)
20
- directory: Migration directory path (default: 'migrations')
21
- command: CLI command name (default: 'db')
22
- compare_type: Enable type comparison in migrations (default: True)
23
- render_as_batch: Render operations in batch mode (default: True)
24
- **kwargs: Additional alembic context arguments
25
"""
26
```
27
28
### Application Integration
29
30
Initialize the extension with a Flask application, registering the CLI commands and setting up the migration environment.
31
32
```python { .api }
33
def init_app(self, app, db=None, directory=None, command=None,
34
compare_type=None, render_as_batch=None, **kwargs):
35
"""
36
Initialize extension with Flask app.
37
38
Parameters:
39
- app: Flask application instance
40
- db: SQLAlchemy database instance (optional, uses constructor value if None)
41
- directory: Migration directory path (optional, uses constructor value if None)
42
- command: CLI command name (optional, uses constructor value if None)
43
- compare_type: Enable type comparison (optional, uses constructor value if None)
44
- render_as_batch: Render operations in batch mode (optional, uses constructor value if None)
45
- **kwargs: Additional alembic context arguments
46
"""
47
```
48
49
### Configuration Management
50
51
Register callback functions that will be called to configure the Alembic environment before running migration operations.
52
53
```python { .api }
54
def configure(self, f):
55
"""
56
Decorator to register configuration callback functions.
57
58
Parameters:
59
- f: Callback function that takes and returns an Alembic Config object
60
61
Returns:
62
The decorated function (for use as decorator)
63
"""
64
65
def call_configure_callbacks(self, config):
66
"""
67
Execute all registered configuration callbacks.
68
69
Parameters:
70
- config: Alembic Config object
71
72
Returns:
73
Modified Config object after all callbacks
74
"""
75
```
76
77
### Configuration Retrieval
78
79
Get an Alembic configuration object with Flask-Migrate specific settings and options applied.
80
81
```python { .api }
82
def get_config(self, directory=None, x_arg=None, opts=None):
83
"""
84
Get Alembic configuration object.
85
86
Parameters:
87
- directory: Migration directory path (optional, uses instance directory if None)
88
- x_arg: Additional arguments for env.py scripts (list or single value)
89
- opts: Options to set on config.cmd_opts (list of option names)
90
91
Returns:
92
Configured Alembic Config object
93
"""
94
```
95
96
## Usage Examples
97
98
### Basic Initialization
99
100
```python
101
from flask import Flask
102
from flask_sqlalchemy import SQLAlchemy
103
from flask_migrate import Migrate
104
105
app = Flask(__name__)
106
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
107
108
db = SQLAlchemy(app)
109
migrate = Migrate(app, db)
110
```
111
112
### Deferred Initialization
113
114
```python
115
from flask import Flask
116
from flask_sqlalchemy import SQLAlchemy
117
from flask_migrate import Migrate
118
119
db = SQLAlchemy()
120
migrate = Migrate()
121
122
def create_app():
123
app = Flask(__name__)
124
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
125
126
db.init_app(app)
127
migrate.init_app(app, db)
128
129
return app
130
```
131
132
### Custom Configuration
133
134
```python
135
from flask_migrate import Migrate
136
137
migrate = Migrate(
138
directory='custom_migrations',
139
command='database',
140
compare_type=False,
141
render_as_batch=False
142
)
143
```
144
145
### Configuration Callbacks
146
147
```python
148
from flask_migrate import Migrate
149
150
migrate = Migrate(app, db)
151
152
@migrate.configure
153
def configure_alembic(config):
154
# Custom Alembic configuration
155
config.set_main_option('sqlalchemy.url', 'postgresql://user:pass@localhost/db')
156
return config
157
```