-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_database.py
More file actions
executable file
·71 lines (64 loc) · 1.63 KB
/
create_database.py
File metadata and controls
executable file
·71 lines (64 loc) · 1.63 KB
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
import os
from datetime import datetime
from config import db
from models import User, ContactDetail
from datetime import datetime, timedelta
created_on = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f%z')
# sample data
USERS = [
{
'username': 'pkerry',
'first_name': 'Paul',
'surname': 'Kerry',
'contact_details': [
('pkerry@mail.com'),
('pkerry@yahoo.com'),
('pkerry@gmail.com'),
],
},
{
'username': 'tdean',
'first_name': 'Tom',
'surname': 'Dean',
'contact_details': [
('tdean@mail.com'),
('tdean@yahoo.com'),
('tdean@gmail.com'),
],
},
{
'username': 'adavies',
'first_name': 'Anna',
'surname': 'Davies',
'contact_details': [
('adavies@mail.com'),
('adavies@yahoo.com'),
('adavies@gmail.com'),
],
},
]
# delete old database file
print('deleting existing database..')
if os.path.exists('users.db'):
os.remove('users.db')
# create new database
db.create_all()
# insert sample records
print('inserting a list of new records...')
for user in USERS:
db_user = User(
username=user.get('username'),
first_name=user.get('first_name'),
surname=user.get('surname')
)
# add emails
contact_details = user.get('contact_details')
for contact_detail in contact_details:
db_user.contact_details.append(
ContactDetail(
email=contact_detail,
)
)
db.session.add(db_user)
db.session.commit()
print('database is ready')