Skip to content

Commit c548261

Browse files
committed
Rename build_database.py to init_database.py
1 parent 55053d3 commit c548261

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

build-a-rest-api-frontend/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ Then you can navigate into the folder `source_code_start/` and create a new data
2121

2222
```sh
2323
(venv) $ cd source_code_start
24-
(venv) $ python build_database.py
24+
(venv) $ python init_database.py
2525
```
2626

27-
This will delete any existing database and create a new database named `people.db` that you can use with your project.
27+
This will create or update the `people.db` database that you can use with your project.
2828

2929
After building the database, you can start the Flask server:
3030

build-a-rest-api-frontend/source_code_start/build_database.py renamed to build-a-rest-api-frontend/source_code_start/init_database.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from datetime import datetime
2+
from sqlalchemy.exc import OperationalError
3+
from sqlalchemy.ext.serializer import loads, dumps
24

35
from config import app, db
46
from models import Note, Person
@@ -43,8 +45,15 @@
4345
},
4446
]
4547

46-
with app.app_context():
47-
db.drop_all()
48+
def get_data_from_table(model):
49+
try:
50+
data = db.session.query(model).all()
51+
db.session.close()
52+
return data
53+
except OperationalError:
54+
return []
55+
56+
def create_database(db):
4857
db.create_all()
4958
for data in PEOPLE_NOTES:
5059
new_person = Person(lname=data.get("lname"), fname=data.get("fname"))
@@ -59,3 +68,26 @@
5968
)
6069
db.session.add(new_person)
6170
db.session.commit()
71+
print("Created new database")
72+
73+
def update_database(db, existing_people, existing_notes):
74+
db.drop_all()
75+
db.create_all()
76+
for person in existing_people:
77+
db.session.merge(person)
78+
for note in existing_notes:
79+
db.session.merge(note)
80+
db.session.commit()
81+
print("Updated existing database")
82+
83+
with app.app_context():
84+
existing_people = get_data_from_table(Person)
85+
existing_notes = get_data_from_table(Note)
86+
87+
if not existing_people:
88+
create_database(db)
89+
else:
90+
update_database(db, existing_people, existing_notes)
91+
92+
93+

build-a-rest-api-frontend/source_code_working/build_database.py renamed to build-a-rest-api-frontend/source_code_working/init_database.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from datetime import datetime
2+
from sqlalchemy.exc import OperationalError
3+
from sqlalchemy.ext.serializer import loads, dumps
24

35
from config import app, db
46
from models import Note, Person
@@ -43,8 +45,15 @@
4345
},
4446
]
4547

46-
with app.app_context():
47-
db.drop_all()
48+
def get_data_from_table(model):
49+
try:
50+
data = db.session.query(model).all()
51+
db.session.close()
52+
return data
53+
except OperationalError:
54+
return []
55+
56+
def create_database(db):
4857
db.create_all()
4958
for data in PEOPLE_NOTES:
5059
new_person = Person(lname=data.get("lname"), fname=data.get("fname"))
@@ -59,3 +68,26 @@
5968
)
6069
db.session.add(new_person)
6170
db.session.commit()
71+
print("Created new database")
72+
73+
def update_database(db, existing_people, existing_notes):
74+
db.drop_all()
75+
db.create_all()
76+
for person in existing_people:
77+
db.session.merge(person)
78+
for note in existing_notes:
79+
db.session.merge(note)
80+
db.session.commit()
81+
print("Updated existing database")
82+
83+
with app.app_context():
84+
existing_people = get_data_from_table(Person)
85+
existing_notes = get_data_from_table(Note)
86+
87+
if not existing_people:
88+
create_database(db)
89+
else:
90+
update_database(db, existing_people, existing_notes)
91+
92+
93+

0 commit comments

Comments
 (0)