-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ECE444-2020Fall/CPP-30
Cpp 30 - Set up RDS and table schemas
- Loading branch information
Showing
30 changed files
with
959 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Setting up our database | ||
|
||
Our database is a MYSQL DB hosted on an RDS instance. | ||
It should interact with our backend using SQLAlchemy as the db interface. SQLAlchemy is an ORM that allows us to treat our relational database like an object -- basically we query it using pythonic 'code-like' commands as opposed to creating SQL queries.... | ||
|
||
## DB related files: | ||
|
||
### models.py : | ||
this is where you specify tables in the database. Each table is it's own class extended from db.Model. In these classes you must specify all column attributes including attribute types, primary keys, etc. | ||
|
||
### config.py : | ||
This contains a config object used when intializing our app. This specifies our db endpoint, login and configs for setting up the migrations folder. | ||
|
||
### migrations/ : | ||
This is how we track changes to our database. Everytime we want to change the schema of our db, we run a migration and update the db. | ||
|
||
### app/__init__.py: | ||
During initialization we create an SQLAlchemy object and our migrations object. the SQLAlchemy object (db) is what we use in server.py to query and call the database. | ||
|
||
## Setup | ||
|
||
Initialize database (I've already done this for us ): `flask db init` | ||
|
||
Create a migration: `flask db migrate -m "message specifying what you changed"` | ||
|
||
Update the database: `flask db upgrade` | ||
|
||
*Note: making the migration will generate migration files in the migration folder specifying the new version of our database. However, this does not modify our real database. To actually 'push' the change, you must run the upgrade command. | ||
|
||
|
||
## Examples | ||
|
||
I've created 3 endpoints in server.py to show how you can add/remove entries in a database. The examples use our User table; each example should be fairly straight forward. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from flask import Flask | ||
from flask_sqlalchemy import SQLAlchemy | ||
from flask_migrate import Migrate | ||
|
||
# ~ Databases ~ # | ||
db = SQLAlchemy() #<-Initialize database object | ||
migrate = Migrate() #<-Initialize migration object | ||
|
||
def create_app(): | ||
"""Construct core application""" | ||
application = Flask(__name__) | ||
|
||
# Pull from config file | ||
application.config.from_object('config.Config') | ||
|
||
# Initailize database | ||
db.init_app(application) #<- This will get called in our models.py file | ||
migrate.init_app(application, db) #<- Migration directory | ||
|
||
return application | ||
|
||
app = create_app() | ||
# ~ Import database schemas ~ # | ||
from app import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from .favourites_list import FavouritesList | ||
from .pantry_list import PantryList | ||
from .recipe import Recipe | ||
from .recipe_cart import RecipeCart | ||
from .recipe_ingredient import RecipeIngredient | ||
from .shopping_list import ShoppingList | ||
from .user import User | ||
from .user_notes import UserNotes | ||
from .user_rating import UserRating |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from app import db | ||
from sqlalchemy_utils import UUIDType | ||
|
||
class FavouritesList(db.Model): | ||
__tablename__ = 'favourites_list' | ||
user_id = db.Column( | ||
UUIDType(), | ||
db.ForeignKey('user.user_id'), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
recipe_id = db.Column( | ||
UUIDType(), | ||
db.ForeignKey('recipe.recipe_id'), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
|
||
def __init__(self, user_id, recipe_id): | ||
self.user_id = user_id | ||
self.recipe_id = recipe_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from app import db | ||
from constants import CONSTANTS | ||
from sqlalchemy_utils import UUIDType | ||
|
||
class PantryList(db.Model): | ||
__tablename__ = 'pantry_list' | ||
user_id = db.Column( | ||
UUIDType(), | ||
db.ForeignKey('user.user_id'), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
ingredient_name = db.Column( | ||
db.String(CONSTANTS['DB_SCHEMA']['MAX_INGREDIENT_NAME_LEN']), | ||
primary_key=True, | ||
nullable=False | ||
) | ||
|
||
def __init__(self, user_id, ingredient_name): | ||
self.user_id = user_id | ||
self.ingredient_name = ingredient_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from app import db | ||
import uuid | ||
from constants import CONSTANTS | ||
from sqlalchemy_utils import UUIDType | ||
|
||
class Recipe(db.Model): | ||
__tablename__ = 'recipe' | ||
recipe_id = db.Column( | ||
UUIDType(), | ||
primary_key=True, | ||
default=uuid.uuid4, | ||
nullable=False | ||
) | ||
|
||
recipe_name = db.Column( | ||
db.String(CONSTANTS['DB_SCHEMA']['MAX_RECIPE_NAME_LEN']), | ||
nullable=False | ||
) | ||
|
||
cuisine = db.Column( | ||
db.String(CONSTANTS['DB_SCHEMA']['MAX_CUISINE_LEN']), | ||
nullable=False | ||
) | ||
|
||
instructions = db.Column( | ||
db.String(CONSTANTS['DB_SCHEMA']['MAX_INSTRUCTION_LEN']), | ||
nullable=False | ||
) | ||
|
||
time_to_cook_in_minutes = db.Column( | ||
db.Integer, | ||
nullable=False | ||
) | ||
|
||
servings = db.Column( | ||
db.Integer, | ||
nullable=False | ||
) | ||
|
||
calories = db.Column( | ||
db.Float, | ||
nullable=False | ||
) | ||
|
||
protein = db.Column( | ||
db.Float, | ||
nullable=False | ||
) | ||
|
||
|
||
carbs = db.Column( | ||
db.Float, | ||
nullable=False | ||
) | ||
|
||
fat = db.Column( | ||
db.Float, | ||
nullable=False | ||
) | ||
|
||
def __init__( | ||
self, recipe_id, recipe_name, cuisine, instructions, | ||
time_to_cook_in_minutes, servings, calories, protein, carbs, fat): | ||
self.recipe_id = recipe_id | ||
self.recipe_name = recipe_name | ||
self.cuisine = cuisine | ||
self. instructions = instructions | ||
self.time_to_cook_in_minutes = time_to_cook_in_minutes | ||
self.servings = servings | ||
self.calories = calories | ||
self.protein = protein | ||
self.carbs = carbs | ||
self.fat = fat | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from app import db | ||
from sqlalchemy_utils import UUIDType | ||
|
||
class RecipeCart(db.Model): | ||
__tablename__ = 'recipe_cart' | ||
user_id = db.Column( | ||
UUIDType(), | ||
db.ForeignKey('user.user_id'), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
recipe_id = db.Column( | ||
UUIDType(), | ||
db.ForeignKey('recipe.recipe_id'), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
|
||
def __init__(self, user_id, recipe_id): | ||
self.user_id = user_id | ||
self.recipe_id = recipe_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from app import db | ||
from constants import CONSTANTS | ||
from sqlalchemy_utils import UUIDType | ||
|
||
class RecipeIngredient(db.Model): | ||
__tablename__ = 'recipe_ingredient' | ||
user_id = db.Column( | ||
UUIDType(), | ||
db.ForeignKey('user.user_id'), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
ingredient_name = db.Column( | ||
db.String(CONSTANTS['DB_SCHEMA']['MAX_INGREDIENT_NAME_LEN']), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
amount = db.Column( | ||
db.Float, | ||
nullable=False | ||
) | ||
unit_of_measurement = db.Column( | ||
db.String(CONSTANTS['DB_SCHEMA']['MAX_MEASUREMENT_UNIT_LEN']) | ||
) | ||
|
||
def __init__(self, user_id, ingredient_name, amount, unit_of_measurement): | ||
self.user_id = user_id | ||
self.ingredient_name = ingredient_name | ||
self.amount = amount | ||
self.unit_of_measurement = unit_of_measurement | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from app import db | ||
from constants import CONSTANTS | ||
from sqlalchemy_utils import UUIDType | ||
|
||
class ShoppingList(db.Model): | ||
__tablename__ = 'shopping_list' | ||
user_id = db.Column( | ||
UUIDType(), | ||
db.ForeignKey('user.user_id'), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
item_name = db.Column( | ||
db.String(CONSTANTS['DB_SCHEMA']['MAX_INGREDIENT_NAME_LEN']), | ||
primary_key=True, | ||
nullable=False | ||
) | ||
|
||
def __init__(self, user_id, item_name): | ||
self.user_id = user_id | ||
self.item_name = item_name | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from app import db | ||
import uuid | ||
from constants import CONSTANTS | ||
from sqlalchemy_utils import UUIDType | ||
|
||
class User(db.Model): | ||
__tablename__ = 'user' | ||
user_id = db.Column( | ||
UUIDType(), | ||
primary_key=True, | ||
default=uuid.uuid4, | ||
nullable=False | ||
) | ||
|
||
username = db.Column( | ||
db.String(CONSTANTS['DB_SCHEMA']['MAX_USERNAME_LEN']), | ||
nullable=False | ||
) | ||
|
||
password = db.Column( | ||
db.String(CONSTANTS['DB_SCHEMA']['MAX_PASSWORD_LEN']), | ||
nullable=False | ||
) | ||
|
||
def __init__(self, username, password): | ||
self.username = username | ||
self.password = password | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from app import db | ||
from constants import CONSTANTS | ||
from sqlalchemy_utils import UUIDType | ||
|
||
class UserNotes(db.Model): | ||
__tablename__ = 'user_notes' | ||
user_id = db.Column( | ||
UUIDType(), | ||
db.ForeignKey('user.user_id'), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
recipe_id = db.Column( | ||
UUIDType(), | ||
db.ForeignKey('recipe.recipe_id'), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
user_notes = db.Column( | ||
db.String(CONSTANTS['DB_SCHEMA']['MAX_INSTRUCTION_LEN']), | ||
nullable=False | ||
) | ||
|
||
def __init__(self, user_id, recipe_id, user_notes): | ||
self.user_id = user_id | ||
self.recipe_id = recipe_id | ||
self.user_notes = user_notes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from app import db | ||
from constants import CONSTANTS | ||
from sqlalchemy_utils import UUIDType | ||
|
||
class UserRating(db.Model): | ||
__tablename__ = 'user_rating' | ||
user_id = db.Column( | ||
UUIDType(), | ||
db.ForeignKey('user.user_id'), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
recipe_id = db.Column( | ||
UUIDType(), | ||
db.ForeignKey('recipe.recipe_id'), | ||
nullable=False, | ||
primary_key=True | ||
) | ||
user_rating = db.Column( | ||
db.Float, | ||
nullable=False | ||
) | ||
|
||
def __init__(self, user_id, recipe_id, user_rating): | ||
self.user_id = user_id | ||
self.recipe_id = recipe_id | ||
self.user_rating = user_rating |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from app.model_schemas import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import os | ||
from constants import CONSTANTS | ||
|
||
# Absolute directory path | ||
basedir = os.path.abspath(os.path.dirname(__file__)) | ||
|
||
# ~ Create config object ~ # | ||
class Config(object): | ||
# ~~ Migration Repository ~~ # | ||
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repo') | ||
SQLALCHEMY_DATABASE_URI = CONSTANTS['DATABASE_URL'] | ||
SQLALCHEMY_TRACK_MODIFICATIONS = False, | ||
JSONIFY_PRETTYPRINT_REGULAR = True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.