首先,我使用Flask-Migrate(Flask的Alembic迁移)创建模式迁移。之后,我创建了分离的数据迁移脚本(也使用Flask-Migrate)。但是,当我运行./manage db upgrade
以应用所有未应用的迁移时,我在控制台输出中看到应用了哪些模式迁移,但数据迁移没有,并处于挂起状态。杀死迁移过程后,我在DB中看到,既没有模式迁移,也没有应用数据迁移。但是当我使用/manage.py db upgrade <schema_migration_revision>
和./manage db upgrade <data_migration_revision>
等命令运行迁移时,所有迁移都会毫无问题地应用。
架构迁移:
"""empty message
Revision ID: 131f25f3304b
Revises: cdb88398744
Create Date: 2015-11-30 14:41:50.522876
"""
# revision identifiers, used by Alembic.
revision = '131f25f3304b'
down_revision = '4d422a836640'
from alembic import op
import sqlalchemy as sa
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('table_to_field', sa.Column('noagg_sql', sa.String()))
op.add_column('table_to_field', sa.Column('subquery_noagg_sql', sa.String()))
op.add_column('table_to_field', sa.Column('subquery_sql', sa.String()))
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_column('table_to_field', 'subquery_sql')
op.drop_column('table_to_field', 'subquery_noagg_sql')
op.drop_column('table_to_field', 'noagg_sql')
### end Alembic commands ###
数据迁移:
"""empty message
Revision ID: 543705754315
Revises: 131f25f3304b
Create Date: 2015-12-01 17:06:31.778978
"""
# revision identifiers, used by Alembic.
revision = '543705754315'
down_revision = '131f25f3304b'
from alembic import op
import sqlalchemy as sa
def upgrade():
from core.db import fixtures
from core.db.model import TableField, Table
from app import db
for tf in TableField.query.all():
tf.noagg_sql = 'some value'
tf.subquery_sql = 'some value'
tf.subquery_noagg_sql = 'some value'
db.session.commit()
def downgrade():
pass
如何使用一个命令升级数据库?