sql_filename => sql_dir

This commit is contained in:
Emmanuel Garette 2020-03-10 14:01:21 +01:00
parent 5103b7bd28
commit 892e052969
2 changed files with 12 additions and 9 deletions

View File

@ -1,6 +1,7 @@
import asyncpg
import asyncio
from os.path import isfile
from os import listdir
from os.path import isdir, join
from sys import exit
@ -8,16 +9,20 @@ from risotto.config import get_config
async def main():
sql_filename = get_config()['global']['sql_filename']
if not isfile(sql_filename):
sql_dir = get_config()['global']['sql_dir']
if not isdir(sql_dir):
print('no sql file to import')
exit()
db_conf = get_config()['database']['dsn']
pool = await asyncpg.create_pool(db_conf)
async with pool.acquire() as connection:
async with connection.transaction():
with open(sql_filename, 'r') as sql:
await connection.execute(sql.read())
for filename in listdir(sql_dir):
if filename.endswith('.sql'):
sql_filename = join(sql_dir, filename)
with open(sql_filename, 'r') as sql:
await connection.execute(sql.read())
if __name__ == '__main__':
loop = asyncio.get_event_loop()

View File

@ -7,8 +7,7 @@ DEFAULT_USER = environ.get('DEFAULT_USER', 'Anonymous')
DEFAULT_DSN = environ.get('RISOTTO_DSN', 'postgres:///risotto?host=/var/run/postgresql/&user=risotto')
DEFAULT_TIRAMISU_DSN = environ.get('DEFAULT_TIRAMISU_DSN', 'postgres:///tiramisu?host=/var/run/postgresql/&user=tiramisu')
MESSAGE_PATH = environ.get('MESSAGE_PATH', '/root/risotto-message/messages')
MODULE_NAME = environ.get('MODULE_NAME', 'test')
SQL_FILENAME = f'/root/risotto-{MODULE_NAME}/sql/init.sql'
SQL_DIR = environ.get('SQL_DIR', './sql')
def get_config():
@ -22,8 +21,7 @@ def get_config():
'internal_user': 'internal',
'check_role': True,
'admin_user': DEFAULT_USER,
'module_name': MODULE_NAME,
'sql_filename': SQL_FILENAME},
'sql_dir': SQL_DIR},
'source': {'root_path': '/srv/seed'},
'cache': {'root_path': '/var/cache/risotto'},
'servermodel': {'internal_source': 'internal',