thewall/init_db.py
2026-02-07 16:34:22 +01:00

52 lines
1.5 KiB
Python

from sqlalchemy import create_engine, MetaData
from thewall.settings import config
import thewall.db as db
from thewall.helpers import get_pwd_hash
DSN = "postgresql://{user}:{password}@{host}:{port}/{database}"
def create_tables(engine):
meta = MetaData()
meta.create_all(bind=engine, tables=[db.entry, db.user])
def create_admin(engine):
conn = engine.connect()
conn.execute(db.user.insert(), {
'name': config['admin']['name'],
'pwd_hash': get_pwd_hash(config['admin']['password'].encode()),
'is_mod': True,
'is_admin': True
})
conn.close()
if __name__ == '__main__':
db_url = DSN.format(**config['postgres'])
engine = create_engine(db_url)
create_tables(engine)
create_admin(engine)
# conn.execute(question.insert(), [
# {'question_text': 'What\'s new?',
# 'pub_date': '2015-12-15 17:17:49.629+02'}
# ])
# conn.execute(choice.insert(), [
# {'choice_text': 'Not much', 'votes': 0, 'question_id': 1},
# {'choice_text': 'The sky', 'votes': 0, 'question_id': 1},
# {'choice_text': 'Just hacking again', 'votes': 0, 'question_id': 1},
# ])
#create first admin
# async with sa_session.begin():
# m = hashlib.sha256()
# m.update(app['config']['admin']['password'].encode())
# admin = User(
# name = app['config']['admin']['name'],
# pwd_hash = m.hexdigest(),
# is_mod = True,
# is_admin = True
# )
# sa_session.add(admin)
# await sa_session.commit()