in migrations/versions/5297e76ff06b_initial_data.py [0:0]
def upgrade():
bulks = {
"order": (orders_table, []),
"customer": (customers_table, []),
"product": (products_table, []),
"producttype": (product_types_table, []),
"orderline": (order_lines_table, []),
}
renames = {
"order": "order_id",
"customer": "customer_id",
"product_type": "product_type_id",
"product": "product_id",
}
file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "initial_data.json")
subprocess.check_call(["bunzip2", "-k", file + ".bz2"])
with open(file) as f:
data = json.load(f)
for item in data:
key = item["model"].split(".")[1]
fields = {"id": item["pk"]}
fields.update(item["fields"])
for rename, to in renames.items():
if rename in fields:
fields[to] = fields.pop(rename)
if "created_at" in fields:
fields["created_at"] = dateutil_parser.parse(fields["created_at"])
bulks[key][1].append(fields)
for _, (t, items) in bulks.items():
op.bulk_insert(t, items)
os.unlink(file)