title: How do I test an alembic migration when I wish to alter the data via Session objects inside the upgrade?
body: <p>I am trying to get alembic to do a database migration for me.  I have made changes to my model which add two new columns, which will have data that is calculated based on the current rows in the table.  What I really want to do is figure out how to test what the migration is going to do before it actually does it.  My understanding is the --sql argument would allow me to do that.</p>
  
  <p>When I try to do the upgrade with --sql to test the migration before actually doing it, using the following command line:</p>
  
  <p><code>alembic -c dev_alembic.ini upgrade --sql 3e0</code></p>
  
  <p>I am receiving an error from alembic: </p>
  
  <pre><code>BEGIN TRANSACTION;
  
  CREATE TABLE alembic_version (
      version_num VARCHAR(32) NOT NULL
  );
  
  GO
  
  -- Running upgrade  -&gt; 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 &lt;module&gt;
      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 &lt;module&gt;
      run_migrations_offline()
    File "alembic/env.py", line 44, in run_migrations_offline
      context.run_migrations()
    File "&lt;string&gt;", 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'
  </code></pre>
  
  <p>Here is the migration file in question:</p>
  
  <pre><code># 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 ###
  </code></pre>