-
Notifications
You must be signed in to change notification settings - Fork 1
/
init_dbs.py
57 lines (41 loc) · 1.8 KB
/
init_dbs.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
import os
import sqlalchemy as db
from dotenv import load_dotenv
load_dotenv()
engines = {
'database': db.create_engine(os.getenv("DB_ENGINE")),
'test_database': db.create_engine(os.getenv("DB_ENGINE_TEST"))
}
metadata = db.MetaData()
def remove_existing_tables(engine):
sql = 'DROP TABLE IF EXISTS users;DROP TABLE IF EXISTS watched_movies;DROP TABLE IF EXISTS followed_movies;'
engine.execute(sql)
def create_user_table():
db.Table('users', metadata,
db.Column('user_id', db.String(36), nullable=False, primary_key=True),
db.Column('username', db.String(255), nullable=False),
db.Column('password', db.String(255), nullable=False),
db.Column('first_name', db.String(255), nullable=False),
db.Column('last_name', db.String(255), nullable=False),
db.Column('email', db.String(255), nullable=False),
db.Column('country', db.String(255), nullable=False),
db.Column('city', db.String(255), nullable=False),
)
def create_following_movies_table():
db.Table('followed_movies', metadata,
db.Column('user_id', db.String(36), nullable=False, primary_key=True),
db.Column('movie_id', db.Integer(), nullable=False, primary_key=True)
)
def create_watched_movies_table():
db.Table('watched_movies', metadata,
db.Column('user_id', db.String(36), nullable=False, primary_key=True),
db.Column('movie_id', db.Integer(), nullable=False, primary_key=True)
)
if __name__ == "__main__":
for engine in engines.values():
remove_existing_tables(engine)
create_user_table()
create_watched_movies_table()
create_following_movies_table()
for engine in engines.values():
metadata.create_all(engine)