Transfer dataset from one schema to another

def release(self, pub_ids=None, email=None, from_schema='upload',
                to_schema='public'):
        """ Transfer dataset from one schema to another"""

        assert pub_ids or email,\
            "Specify either pub_ids or email"
        assert not (pub_ids and email),\
            "Specify either pub_ids or email"

        con = self.connection or self._connect()
        cur = con.cursor()
        assert self.user in ['release', 'catroot', 'postgres'], \
            "You don't have permission to perform this operation"

        if email:
            cur.execute(
                """SELECT distinct pub_id
                FROM {from_schema}.reaction
                WHERE username = '{username}'"""
                .format(from_schema=from_schema,
                        username=email))

            pub_ids = [id[0] for id in cur.fetchall()]

        for pub_id in pub_ids:
            self.stdout.write(
                """Releasing publication {pub_id} from
                {from_schema} to {schema} \n"""
                .format(pub_id=pub_id, from_schema=from_schema,
                        schema=to_schema))

            mtime = now()
            cur.execute(
                """UPDATE {from_schema}.systems SET
                mtime = {mtime}
                WHERE unique_id in
                  (SELECT distinct ase_id
                   FROM {from_schema}.publication_system
                   WHERE pub_id = '{pub_id}')"""
                .format(from_schema=from_schema,
                        mtime=mtime, pub_id=pub_id))
            columns = get_key_str('systems', start_index=1)
            cur.execute(
                """INSERT INTO {schema}.systems ({columns})
                SELECT {columns}
                FROM {from_schema}.systems
                WHERE unique_id in
                  (SELECT distinct ase_id
                   FROM {from_schema}.publication_system
                   WHERE pub_id = '{pub_id}')"""
                .format(from_schema=from_schema,
                        schema=to_schema,
                        columns=columns,
                        pub_id=pub_id))
            columns = get_key_str('publication', start_index=1)  # new id
            cur.execute(
                """INSERT INTO {schema}.publication ({columns})
                SELECT {columns}
                FROM {from_schema}.publication
                WHERE pub_id = '{pub_id}'"""
                .format(from_schema=from_schema,
                        schema=to_schema, columns=columns,
                        pub_id=pub_id))
            cur.execute(
            """UPDATE {schema}.publication SET
                stime = {mtime}
                WHERE pub_id = '{pub_id}'"""
                .format(schema=to_schema,
                        mtime=mtime, pub_id=pub_id))

            cur.execute(
                """INSERT INTO {schema}.publication_system
                SELECT *
                FROM {from_schema}.publication_system
                WHERE pub_id = '{pub_id}'"""
                .format(from_schema=from_schema,
                        schema=to_schema, columns=columns, pub_id=pub_id))

            columns = get_key_str('reaction', start_index=1)  # new id

            cur.execute(
                """INSERT INTO {schema}.reaction ({columns})
                SELECT {columns}
                FROM {from_schema}.reaction
                WHERE pub_id = '{pub_id}'
                ORDER BY {from_schema}.reaction.id
                RETURNING id"""
                .format(from_schema=from_schema,
                        schema=to_schema, columns=columns, pub_id=pub_id))

            new_ids = [id[0] for id in cur.fetchall()]

            cur.execute(
                """SELECT * from {from_schema}.reaction_system
                WHERE ase_id in
                (SELECT distinct ase_id
                FROM {from_schema}.publication_system
                WHERE pub_id = '{pub_id}') ORDER BY id"""
                .format(from_schema=from_schema,
                        pub_id=pub_id))

            reaction_system0 = cur.fetchall()
            reaction_system_values = []
            id0 = reaction_system0[0][3]
            i = 0
            for row in reaction_system0:
                row = list(row)
                if not id0 == row[3]:
                    i += 1
                    id0 = row[3]
                row[3] = new_ids[i]
                reaction_system_values += [tuple(row)]
            key_str = get_key_str('reaction_system')
            insert_command = """
            INSERT INTO {schema}.reaction_system ({key_str})
            VALUES %s ON CONFLICT DO NOTHING;"""\
            .format(schema=to_schema, key_str=key_str)

            execute_values(cur=cur, sql=insert_command,
                           argslist=reaction_system_values, page_size=1000)
            self.stdout.write('Transfer complete\n')

        # if self.user == 'catroot':
        #    if self.connection is None:
        #        con.commit()
        #    self.delete_publication(pub_id, schema='upload')

        if self.connection is None:
            con.commit()
            con.close()

        return