def copy_table(db, cursor, table): src_cursor.execute('SELECT * FROM ' + src_db.quote(table)) columns = get_column_names(src_cursor) n_rows = 100 if multirows_insert and max_parameters: n_rows = min(n_rows, int(max_parameters // len(columns))) quoted_table = db.quote(table) holders = '(%s)' % ','.join(['%s'] * len(columns)) count = 0 cursor.execute('DELETE FROM ' + quoted_table) while True: rows = src_cursor.fetchmany(n_rows) if not rows: break count += len(rows) if progress: printfout("%d records\r %s table... ", count, table, newline=False) if replace_cast is not None and table == 'report': rows = replace_report_query(rows, columns, replace_cast) query = 'INSERT INTO %s (%s) VALUES ' % \ (quoted_table, ','.join(map(db.quote, columns))) if multirows_insert: cursor.execute(query + ','.join([holders] * len(rows)), sum(rows, ())) else: cursor.executemany(query + holders, rows) return count
def _copy_directories(self, dst_env): printfout("Copying directories:") for src in (self.env.files_dir, self.env.htdocs_dir, self.env.templates_dir, self.env.plugins_dir): name = os.path.basename(src) dst = os.path.join(dst_env.path, name) printfout(" %s directory... ", name, newline=False) if os.path.isdir(dst): shutil.rmtree(dst) if os.path.isdir(src): shutil.copytree(src, dst) printfout("done.")
def copy_tables(src_env, dst_env, src_db, dst_db, src_dburi, dst_dburi): printfout("Copying tables:") if src_dburi.startswith('sqlite:'): src_db.cnx._eager = False # avoid uses of eagar cursor src_cursor = src_db.cursor() if src_dburi.startswith('sqlite:'): if type(src_cursor.cursor) is not sqlite_backend.PyFormatCursor: raise AssertionError('src_cursor.cursor is %r' % src_cursor.cursor) src_tables = set(DatabaseManager(src_env).get_table_names()) cursor = dst_db.cursor() dst_dbm = DatabaseManager(dst_env) tables = set(dst_dbm.get_table_names()) & src_tables sequences = set(dst_dbm.get_sequence_names()) progress = sys.stdout.isatty() and sys.stderr.isatty() replace_cast = get_replace_cast(src_db, dst_db, src_dburi, dst_dburi) # speed-up copying data with SQLite database if dst_dburi.startswith('sqlite:'): sqlite_backend.set_synchronous(cursor, 'OFF') multirows_insert = sqlite_backend.sqlite_version >= (3, 7, 11) max_parameters = 999 else: multirows_insert = True max_parameters = None def copy_table(db, cursor, table): src_cursor.execute('SELECT * FROM ' + src_db.quote(table)) columns = get_column_names(src_cursor) n_rows = 100 if multirows_insert and max_parameters: n_rows = min(n_rows, int(max_parameters // len(columns))) quoted_table = db.quote(table) holders = '(%s)' % ','.join(['%s'] * len(columns)) count = 0 cursor.execute('DELETE FROM ' + quoted_table) while True: rows = src_cursor.fetchmany(n_rows) if not rows: break count += len(rows) if progress: printfout("%d records\r %s table... ", count, table, newline=False) if replace_cast is not None and table == 'report': rows = replace_report_query(rows, columns, replace_cast) query = 'INSERT INTO %s (%s) VALUES ' % \ (quoted_table, ','.join(map(db.quote, columns))) if multirows_insert: cursor.execute(query + ','.join([holders] * len(rows)), sum(rows, ())) else: cursor.executemany(query + holders, rows) return count try: cursor = dst_db.cursor() for table in sorted(tables): printfout(" %s table... ", table, newline=False) count = copy_table(dst_db, cursor, table) printfout("%d records.", count) for table in tables & sequences: dst_db.update_sequence(cursor, table) dst_db.commit() except: dst_db.rollback() raise