-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset-db.py
88 lines (82 loc) · 2.46 KB
/
reset-db.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import datetime
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from db import Base, Entry, Game, Player, User, engine
# open connection
# open connection
uri = os.getenv("DATABASE_URL")
if uri:
if uri.startswith("postgres://"):
uri = uri.replace("postgres://", "postgresql://", 1)
eng = create_engine(uri, connect_args={"sslmode": "require"})
else:
eng = engine("database.db")
Base.metadata.drop_all(bind=eng)
Base.metadata.create_all(bind=eng)
Session = sessionmaker(bind=eng)
ses = Session()
# add initial data
# 1. users
uA = User(name="A", password=User.hash_password("A"))
uB = User(name="B", password=User.hash_password("B"))
uC = User(name="C", password=User.hash_password("C"))
uD = User(name="D", password=User.hash_password("D"))
ses.add_all([uA, uB, uC, uD])
ses.commit()
# 2. games
gA1 = Game(
title="A1 running",
user_id=uA.id,
slug="gA1",
state="running",
ctime=datetime.datetime.now() - datetime.timedelta(minutes=65),
)
gB1 = Game(
title="B1 init",
user_id=uB.id,
slug="gB1",
state="init",
ctime=datetime.datetime.now() - datetime.timedelta(hours=25),
)
gC1 = Game(
title="C1 finished",
user_id=uC.id,
slug="gC1",
state="finished",
ctime=datetime.datetime.now() - datetime.timedelta(days=367),
)
gA2 = Game(title="A2 init", user_id=uA.id, slug="gA2", state="init")
ses.add_all([gA1, gB1, gC1, gA2])
ses.commit()
# 3. players + entries
ses.add_all(
[
# gA1
Player(position=0, user_id=uA.id, game_id=gA1.id),
Player(position=1, user_id=uB.id, game_id=gA1.id),
Player(position=2, user_id=uC.id, game_id=gA1.id),
# gB1
Player(position=0, user_id=uB.id, game_id=gB1.id),
Player(position=1, user_id=uA.id, game_id=gB1.id),
Player(position=2, user_id=uC.id, game_id=gB1.id),
# gC1
Player(position=0, user_id=uC.id, game_id=gC1.id),
Player(position=1, user_id=uA.id, game_id=gC1.id),
Player(position=2, user_id=uB.id, game_id=gC1.id),
# gA2
Player(position=0, user_id=uA.id, game_id=gA2.id),
Player(position=1, user_id=uB.id, game_id=gA2.id),
]
)
ses.add_all(
[
# gA1
Entry(body="bla", user_id=uA.id, game_id=gA1.id),
Entry(body="blabla", user_id=uB.id, game_id=gA1.id),
# gC1
Entry(body="this is", user_id=uC.id, game_id=gC1.id),
Entry(body="the end", user_id=uA.id, game_id=gC1.id),
]
)
ses.commit()