Newer
Older
##############################################
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="A script to port an existing synapse SQLite database to"
Amber Brown
committed
" a new PostgreSQL database."
parser.add_argument("-v", action="store_true")
parser.add_argument(
Amber Brown
committed
"--sqlite-database",
required=True,
help="The snapshot of the SQLite database file. This must not be"
Amber Brown
committed
" currently used by a running synapse server",
)
parser.add_argument(
Amber Brown
committed
"--postgres-config",
type=argparse.FileType("r"),
Amber Brown
committed
required=True,
help="The database config file for the PostgreSQL database",
)
parser.add_argument(
"--curses", action="store_true", help="display a curses based progress UI"
parser.add_argument(
Amber Brown
committed
"--batch-size",
type=int,
default=1000,
help="The number of rows to select from the SQLite table each"
Amber Brown
committed
" iteration [default=1000]",
Erik Johnston
committed
args = parser.parse_args()
Erik Johnston
committed
logging_config = {
"level": logging.DEBUG if args.v else logging.INFO,
Amber Brown
committed
"format": "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(message)s",
Erik Johnston
committed
}
if args.curses:
logging_config["filename"] = "port-synapse.log"
logging.basicConfig(**logging_config)
sqlite_config = {
"name": "sqlite3",
"args": {
"database": args.sqlite_database,
"cp_min": 1,
"cp_max": 1,
"check_same_thread": False,
},
}
hs_config = yaml.safe_load(args.postgres_config)
if "database" not in hs_config:
sys.stderr.write("The configuration file must have a 'database' section.\n")
sys.exit(4)
postgres_config = hs_config["database"]
if "name" not in postgres_config:
sys.stderr.write("Malformed database config: no 'name'\n")
sys.exit(2)
if postgres_config["name"] != "psycopg2":
sys.stderr.write("Database must use the 'psycopg2' connector.\n")
sys.exit(3)
config = HomeServerConfig()
config.parse_config_dict(hs_config, "", "")
Erik Johnston
committed
def start(stdscr=None):
if stdscr:
progress = CursesProgress(stdscr)
else:
progress = TerminalProgress()
porter = Porter(
sqlite_config=sqlite_config,
progress=progress,
batch_size=args.batch_size,
hs_config=config,
Erik Johnston
committed
)
@defer.inlineCallbacks
def run():
with LoggingContext("synapse_port_db_run"):
yield defer.ensureDeferred(porter.run())
reactor.callWhenRunning(run)
Erik Johnston
committed
reactor.run()
if args.curses:
curses.wrapper(start)
else:
start()
if end_error:
if end_error_exec_info:
exc_type, exc_value, exc_traceback = end_error_exec_info
traceback.print_exception(exc_type, exc_value, exc_traceback)
sys.stderr.write(end_error)
sys.exit(5)