-
Notifications
You must be signed in to change notification settings - Fork 13
/
create_tables.py
63 lines (53 loc) · 1.52 KB
/
create_tables.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import uuid
from logzero import logger
from flask import current_app
from sqlmodel import SQLModel, select
from application import server
from utils.config import get_session
from utils.user import add_user, show_users
from models.user import User
from models.password_change import PasswordChange
def main():
"""
Create all the tables in the current SQLModel metadata.
Clear the tables.
Re-create the test values.
"""
# engine is open to sqlite///users.db (or whatever is in the .env files)
SQLModel.metadata.create_all(current_app.engine)
# add a test user to the database
users = [
dict(
first="test",
last="test",
email="[email protected]",
password="test",
)
]
with get_session() as session:
# delete existing users
logger.info("DELETING USERS")
existing = session.exec(select(User)).all()
i = 0
for x in existing:
session.delete(x)
i += 1
session.commit()
logger.info(f"DELETED {i} USERS")
# add new users
logger.info("ADDING USERS")
i = 0
for vals in users:
add_user(**vals)
i += 1
session.commit()
logger.info(f"ADDED {i} USERS")
# show that the users exists
logger.info("USERS ARE:")
show_users()
# confirm that user exists
assert User.from_email(users[0]["email"])
logger.info(f"DONE")
if __name__ == "__main__":
with server.app_context():
main()