risotto/script/database_manager.py

25 lines
667 B
Python
Raw Normal View History

2019-12-13 13:55:30 +01:00
import asyncpg
import asyncio
from os.path import isfile
2020-03-06 07:33:20 +01:00
from sys import exit
2019-12-13 13:55:30 +01:00
from risotto.config import get_config
2019-12-27 15:09:38 +01:00
2019-12-13 13:55:30 +01:00
async def main():
sql_filename = get_config()['global']['sql_filename']
if not isfile(sql_filename):
print('no sql file to import')
exit()
2020-01-30 16:22:06 +01:00
db_conf = get_config()['database']['dsn']
pool = await asyncpg.create_pool(db_conf)
2019-12-13 13:55:30 +01:00
async with pool.acquire() as connection:
async with connection.transaction():
with open(sql_filename, 'r') as sql:
2020-03-06 07:33:20 +01:00
await connection.execute(sql.read())
2019-12-13 16:42:10 +01:00
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())