Skip to content
Snippets Groups Projects
Commit df844fdc authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Initial commit.

parents
No related branches found
No related tags found
No related merge requests found
*.pyc
*.pyo
.idea
.buildozer
bin
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
Persisted = declarative_base()
class Person(Persisted):
__tablename__ = 'people'
person_id = Column(Integer, primary_key=True)
name = Column(String(256), nullable=False)
supervisor_id = Column(Integer, ForeignKey('people.person_id', ondelete='CASCADE'))
mentor_id = Column(Integer, ForeignKey('people.person_id', ondelete='CASCADE'))
# Because there are multiple keys the relationship could use, we have to specify the foreign key to use and the key
# to match it to (in this case a primary key).
supervisor = relationship('Person', foreign_keys=[supervisor_id], remote_side=[person_id], back_populates='supervisees')
mentor = relationship('Person', foreign_keys=[mentor_id], remote_side=[person_id], back_populates='mentees')
# For the reverse relationships (the ones with uselist=True), the key to match to is implicitly the primary key.
supervisees = relationship('Person', foreign_keys=[supervisor_id], uselist=True, back_populates='supervisor')
mentees = relationship('Person', foreign_keys=[mentor_id], uselist=True, back_populates='mentor')
class Database(object):
@staticmethod
def construct_mysql_url(authority, port, database, username, password):
return 'mysql+mysqlconnector://{username}:{password}@{authority}:{port}/{database}' \
.format(authority=authority, port=port, database=database, username=username, password=password)
@staticmethod
def construct_in_memory_url():
return 'sqlite:///'
def __init__(self, url):
self.engine = create_engine(url)
self.Session = sessionmaker()
self.Session.configure(bind=self.engine)
def ensure_tables_exist(self):
Persisted.metadata.create_all(self.engine)
def create_session(self):
return self.Session()
# -*- coding: utf-8; -*-
from sys import stderr
from sqlalchemy.exc import SQLAlchemyError
from database import Database, Person
def add_starter_data(session):
alice = Person(name='Alice')
bob = Person(name='Bob', mentor=alice)
carol = Person(name='Carol', supervisor=alice, mentor=bob)
session.add(alice)
session.add(bob)
session.add(carol)
def main():
try:
url = Database.construct_mysql_url('localhost', 3306, 'people', 'root', 'cse')
database = Database(url)
database.ensure_tables_exist()
print('Tables created.')
session = database.create_session()
add_starter_data(session)
session.commit()
print('Records created.')
except SQLAlchemyError as exception:
print('Database setup failed!', file=stderr)
print('Cause: {exception}'.format(exception=exception), file=stderr)
exit(1)
if __name__ == '__main__':
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment