42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
import argparse
|
|
import asyncio
|
|
import json
|
|
from os import path
|
|
|
|
from app.db import init_db, Entity, Order, Contract, User, AccessToken
|
|
|
|
|
|
models = [Entity, Order, Contract, User]
|
|
|
|
|
|
async def handle_migration(args):
|
|
await init_db()
|
|
if args.action == "import-fixtures":
|
|
for m in models:
|
|
fname = './fixtures/{}.json'.format(m.__name__)
|
|
if path.isfile(fname):
|
|
with open(fname, 'r') as f:
|
|
content = json.load(f)
|
|
for r in content:
|
|
obj = m(**r)
|
|
await obj.save()
|
|
|
|
elif args.action == "export-fixtures":
|
|
for m in models:
|
|
if await m.count() > 0:
|
|
with open('./fixtures/{}.json'.format(m.__name__), 'w') as f:
|
|
body = [o.json() for o in await m.find_all().to_list()]
|
|
f.write('[\n' + ',\n'.join(body) + '\n]\n')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(
|
|
prog='migration.py',
|
|
description='Migrate data and schemas on the crud project',
|
|
epilog='Bisou')
|
|
|
|
parser.add_argument('action', type=str, help="The action to perform")
|
|
args = parser.parse_args()
|
|
|
|
asyncio.run(handle_migration(args))
|