title: How do I change column type on SQLAlchemy declarative model dynamically?
body: <p>I am running mysql in production but would like to run a simple tests in a sqlite in memory db.</p>
  
  <p>The legacy mysql db has tables with columns that are mysql specific types, Which are declared in declarative models (subclassing declarative_base). I would like to run some simple tests without going to mysql and so would need to swap out the columns of the model. </p>
  
  <p>How do I do this? I've tried writing a patcher/unpatcher to swap out <strong>table</strong> in my model, but when I run some tests, I get</p>
  
  <pre><code>OperationalError: (OperationalError) near ")": syntax error u'\nCREATE TABLE my_table (\n)\n\n' ()
  </code></pre>
  
  <p>Which makes my think that I am not patching the columns properly.</p>
  
  <p>Does anyone know how I can do this? What am I doing wrong?</p>
  
  <p>Currently, I create new columns and attach brand new Table object to <code>__table__</code> and save the old table.</p>
  
  <p>The DB is created, create_all() is and convert_columns is run in setUp. drop_all() and revert_columns is run during tearDown in my tests</p>
  
  <pre><code>mysql_sqlite_mapping = {INTEGER: Integer,
                          MEDIUMINT: Integer,
                          TEXT: text}    
  
  def convert_columns(self, my_class, mapping):
      for column in my_class.__table__.columns:
          if type(column.type) in mapping:
              replacement_col = Column(column.name,
                                       mapping[type(column.type)],
                                       primary_key=column.primary_key,
                                       nullable=column.nullable,
                                       key=column.key,
                                       unique=column.unique)
  
              converted_columns.append(replacement_col)
  
      self.registry[my_class] = my_class.__table__
  
      my_class.__table__.metadata.remove(my_class.__table__)
      my_class.__table__ = Table(my_class.__table__.name,
                                 my_class.__table__.metadata)
  
      for column in converted_columns:
          my_class.__table__.append_column(column)
  
      return my_class
  
  def revert_columns(self, my_class):
      saved_table = self.registry[my_class]
  
      metadata = my_class.__table__.metadata
      my_class.__table__.metadata.remove(my_class.__table__)
  
      model_class.__table__ = Table(saved_table.name,
                                    metadata)
  
      for column in saved_table.columns:
          column.table = None
          my_class.__table__.append_column(column)
  
      self.registry.pop(my_class)
  </code></pre>