我正在尝试让alembic为我做数据库迁移。我对我的模型进行了更改,添加了两个新列,这些列将具有基于表中当前行计算的数据。我真正想要做的是弄清楚如何在实际迁移之前测试迁移的内容。我的理解是--sql参数允许我这样做。
当我尝试使用--sql进行升级以在实际执行之前测试迁移时,使用以下命令行:
alembic -c dev_alembic.ini upgrade --sql 3e0
我收到了来自alembic的错误:
BEGIN TRANSACTION;
CREATE TABLE alembic_version (
version_num VARCHAR(32) NOT NULL
);
GO
-- Running upgrade -> 3e076afb70e1
ALTER TABLE sipendpoint ADD extension VARCHAR(50) NULL;
GO
ALTER TABLE sipendpoint ADD home_site VARCHAR(5) NULL;
GO
Traceback (most recent call last):
File "/home/pgrace/venv/pyramid27/bin/alembic", line 9, in <module>
load_entry_point('alembic==0.7.6', 'console_scripts', 'alembic')()
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/alembic/config.py", line 439, in main
CommandLine(prog=prog).main(argv=argv)
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/alembic/config.py", line 433, in main
self.run_cmd(cfg, options)
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/alembic/config.py", line 416, in run_cmd
**dict((k, getattr(options, k)) for k in kwarg)
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/alembic/command.py", line 165, in upgrade
script.run_env()
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/alembic/script.py", line 390, in run_env
util.load_python_file(self.dir, 'env.py')
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/alembic/util.py", line 243, in load_python_file
module = load_module_py(module_id, path)
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/alembic/compat.py", line 79, in load_module_py
mod = imp.load_source(module_id, path, fp)
File "alembic/env.py", line 65, in <module>
run_migrations_offline()
File "alembic/env.py", line 44, in run_migrations_offline
context.run_migrations()
File "<string>", line 7, in run_migrations
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/alembic/environment.py", line 738, in run_migrations
self.get_context().run_migrations(**kw)
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/alembic/migration.py", line 309, in run_migrations
step.migration_fn(**kw)
File "/home/pgrace/repo/stackcallrouterserver/alembic/versions/3e076afb70e1_add_extension_field_and_home_site_to_.py", line 48, in upgrade
for entry in DBSession.query(SIPEndpoint):
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2515, in __iter__
return self._execute_and_instances(context)
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2528, in _execute_and_instances
close_with_result=True)
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2519, in _connection_from_session
**kw)
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 882, in connection
execution_options=execution_options)
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 887, in _connection_for_bind
engine, execution_options)
File "/home/pgrace/venv/pyramid27/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 344, in _connection_for_bind
transaction = conn.begin()
AttributeError: 'MockConnection' object has no attribute 'begin'
以下是相关的迁移文件:
# revision identifiers, used by Alembic.
revision = '3e076afb70e1'
down_revision = None
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
import re
from scrserver.models import SIPEndpoint
from sqlalchemy.orm import (
scoped_session,
sessionmaker,
)
DBSession = scoped_session(sessionmaker(bind=op.get_bind()))
Base = declarative_base()
Base.metadata.bind = op.get_bind()
def sitecheck(x):
try:
return {
'1.2.3.4': "FOO",
'5.6.7.8': "BAR",
'9.10.11.12': "BAZ"
}
except KeyError:
return None
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('sipendpoint', sa.Column('extension', sa.String(length=50), nullable=True))
op.add_column('sipendpoint', sa.Column('home_site', sa.String(length=5), nullable=True))
### end Alembic commands ###
for entry in DBSession.query(SIPEndpoint):
update=sa.update(SIPEndpoint).where(SIPEndpoint.addr==entry.addr).values(extension=entry.addr)
op.execute(update)
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_column('sipendpoint', 'home_site')
op.drop_column('sipendpoint', 'extension')
### end Alembic commands ###
答案 0 :(得分:4)
你无法运行:
for entry in DBSession.query(SIPEndpoint):
在--sql模式下,因为这是一个SELECT语句。在--sql模式下没有可用于SELECT的数据库。
没有办法对希望与数据库进行对话的脚本进行“干运行”,例如:依赖于从数据库中检索数据的脚本。 --sql模式仅限于那些根本不需要交互的命令,例如: CREATE,DROP和ALTER语句,以及极其有限的度,INSERT,UPDATE和DELETE语句,它们必须以非常特殊的方式发出,以便它们不会尝试请求有关操作的信息。对于INSERT,建议使用bulk insert样式,因为它被设计为与--sql模式兼容。