labpack/databases/sql.py
def _rebuild_table(self, new_name, old_name, new_columns, old_columns):

        ''' a helper method for rebuilding table (by renaming & migrating) '''

        # verbosity
        self.printer('Rebuilding %s table in %s database' % (self.table_name, self.database_name), flush=True)

        from sqlalchemy import Table, MetaData
        metadata_object = MetaData()

        # construct old table
        old_table_args = [old_name, metadata_object]
        old_column_args = self._construct_columns(old_columns)
        old_table_args.extend(old_column_args)
        old_table = Table(*old_table_args)

        # construct new table
        new_table_args = [new_name, metadata_object]
        new_column_args = self._construct_columns(new_columns)
        new_table_args.extend(new_column_args)
        new_table = Table(*new_table_args)

        # determine differences between tables
        add_columns, remove_columns, rename_columns, retype_columns, resize_columns = self._compare_columns(new_columns, old_columns)

        # rename table and recreate table if it doesn't already exist
        table_list = self.engine.table_names()
        if not old_name in table_list:
            self.engine.execute('ALTER TABLE %s RENAME TO %s' % (new_name, old_name))
            new_table.create(self.engine)

        # wait for renamed table to be responsive

        # migrate records from old to new
        list_statement = old_table.select()
        count = 0
        for record in self.session.execute(list_statement).fetchall():
            create_kwargs = self._construct_inserts(record, new_columns, rename_columns, retype_columns, resize_columns)
            insert_statement = new_table.insert().values(**create_kwargs)
            self.session.execute(insert_statement)
            delete_statement = old_table.delete(old_table.c.id == record.id)
            self.session.execute(delete_statement)
            if not count % 10:
                self.printer('.', flush=True)
            count += 1

        # drop old table
        record_list = self.session.execute(list_statement).first()
        if not record_list:
            self.session.close()
            old_table.drop(self.engine)
            self.session = self.engine.connect()

        # handle verbosity   
        self.printer(' done.')

        return True
 a helper method for rebuilding table (by renaming & migrating) 
a helper method for rebuilding table (by renaming & migrating)
a helper method for rebuilding table ( by renaming & migrating )
def _rebuild_table(self, new_name, old_name, new_columns, old_columns):
        self.printer('Rebuilding %s table in %s database' % (self.table_name, self.database_name), flush=True)
        from sqlalchemy import Table, MetaData
        metadata_object = MetaData()
        old_table_args = [old_name, metadata_object]
        old_column_args = self._construct_columns(old_columns)
        old_table_args.extend(old_column_args)
        old_table = Table(*old_table_args)
        new_table_args = [new_name, metadata_object]
        new_column_args = self._construct_columns(new_columns)
        new_table_args.extend(new_column_args)
        new_table = Table(*new_table_args)
        add_columns, remove_columns, rename_columns, retype_columns, resize_columns = self._compare_columns(new_columns, old_columns)
        table_list = self.engine.table_names()
        if not old_name in table_list:
            self.engine.execute('ALTER TABLE %s RENAME TO %s' % (new_name, old_name))
            new_table.create(self.engine)
        list_statement = old_table.select()
        count = 0
        for record in self.session.execute(list_statement).fetchall():
            create_kwargs = self._construct_inserts(record, new_columns, rename_columns, retype_columns, resize_columns)
            insert_statement = new_table.insert().values(**create_kwargs)
            self.session.execute(insert_statement)
            delete_statement = old_table.delete(old_table.c.id == record.id)
            self.session.execute(delete_statement)
            if not count % 10:
                self.printer('.', flush=True)
            count += 1
        record_list = self.session.execute(list_statement).first()
        if not record_list:
            self.session.close()
            old_table.drop(self.engine)
            self.session = self.engine.connect()
        self.printer(' done.')
        return True
def _rebuild_table ( self , new_name , old_name , new_columns , old_columns ) : self . printer ( 'Rebuilding %s table in %s database' % ( self . table_name , self . database_name ) , flush = True ) from sqlalchemy import Table , MetaData metadata_object = MetaData ( ) old_table_args = [ old_name , metadata_object ] old_column_args = self . _construct_columns ( old_columns ) old_table_args . extend ( old_column_args ) old_table = Table ( * old_table_args ) new_table_args = [ new_name , metadata_object ] new_column_args = self . _construct_columns ( new_columns ) new_table_args . extend ( new_column_args ) new_table = Table ( * new_table_args ) add_columns , remove_columns , rename_columns , retype_columns , resize_columns = self . _compare_columns ( new_columns , old_columns ) table_list = self . engine . table_names ( ) if not old_name in table_list : self . engine . execute ( 'ALTER TABLE %s RENAME TO %s' % ( new_name , old_name ) ) new_table . create ( self . engine ) list_statement = old_table . select ( ) count = 0 for record in self . session . execute ( list_statement ) . fetchall ( ) : create_kwargs = self . _construct_inserts ( record , new_columns , rename_columns , retype_columns , resize_columns ) insert_statement = new_table . insert ( ) . values ( ** create_kwargs ) self . session . execute ( insert_statement ) delete_statement = old_table . delete ( old_table . c . id == record . id ) self . session . execute ( delete_statement ) if not count % 10 : self . printer ( '.' , flush = True ) count += 1 record_list = self . session . execute ( list_statement ) . first ( ) if not record_list : self . session . close ( ) old_table . drop ( self . engine ) self . session = self . engine . connect ( ) self . printer ( ' done.' ) return True
a helper method for rebuilding table (by renaming & migrating)
a helper method for rebuilding table ( by renaming & migrating )
''' a helper method for rebuilding table (by renaming & migrating) ''' # verbosity # construct old table # construct new table # determine differences between tables # rename table and recreate table if it doesn't already exist # wait for renamed table to be responsive # migrate records from old to new # drop old table # handle verbosity   
self
new_name
old_name
new_columns
old_columns