def _dump_table(table: Model, directory: Path, format_: str): """Dump a single table.""" try: table.select().tuples() table.fields() dataset = tablib.Dataset(*table.select().tuples(), headers=table.fields()) except: print(table._meta.database.get_columns(table.table_name())) if directory is not None: print(f" Dumping {table.table_name()}...") out_file = Path(directory) / f"{table.table_name()}.{format_}" out_file.write_text(dataset.export(format_)) print(" Done.") print("=====================") else: print(dataset.export("csv"))
def _load_table(table: Model, directory: Path, format_: str): """Dump a single table.""" if directory is not None: print(f" Loading {table.table_name()}...") in_file = Path(directory) / f"{table.table_name()}.{format_}" dataset = tablib.Dataset(headers=table.fields()).load( in_file.read_text()) print(f" Importing {table.table_name()} into the database...") table.insert_many(dataset.dict).execute() print(" Done.") print("=====================") else: pass