Skip to content

Commit

Permalink
Added single sessionmaker object to connection constructor for callba…
Browse files Browse the repository at this point in the history
…ck transactors
  • Loading branch information
ericharmeling committed Jul 24, 2020
1 parent 3965016 commit 1528f50
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions movr/movr.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class MovR:
"""
def __init__(self, conn_string):
"""
Establish a connection to the database, creating an Engine instance.
Establish a connection to the database, creating Engine and Sessionmaker objects.
Arguments:
conn_string {String} -- CockroachDB connection string.
"""
self.engine = create_engine(conn_string, convert_unicode=True)
self.sessionmaker = sessionmaker(bind=self.engine)

def start_ride(self, city, rider_id, rider_city, vehicle_id):
"""
Expand All @@ -31,7 +32,7 @@ def start_ride(self, city, rider_id, rider_city, vehicle_id):
vehicle_id {UUID} -- The vehicle's unique ID.
"""
return run_transaction(
sessionmaker(bind=self.engine), lambda session: start_ride_txn(
self.sessionmaker, lambda session: start_ride_txn(
session, city, rider_id, rider_city, vehicle_id))

def end_ride(self, city, ride_id, location):
Expand All @@ -44,7 +45,7 @@ def end_ride(self, city, ride_id, location):
location {String} -- The vehicle's last location.
"""
return run_transaction(
sessionmaker(bind=self.engine),
self.sessionmaker,
lambda session: end_ride_txn(session, city, ride_id, location))

def add_user(self, city, first_name, last_name, email, username, password):
Expand All @@ -60,7 +61,7 @@ def add_user(self, city, first_name, last_name, email, username, password):
password {String} -- The user's unhashed password.
"""
return run_transaction(
sessionmaker(bind=self.engine),
self.sessionmaker,
lambda session: add_user_txn(session, city, first_name, last_name,
email, username, password))

Expand All @@ -73,7 +74,7 @@ def remove_user(self, city, user_id):
id {UUID} -- The user's unique ID.
"""
return run_transaction(
sessionmaker(bind=self.engine),
self.sessionmaker,
lambda session: remove_user_txn(session, city, user_id))

def remove_vehicle(self, city, vehicle_id):
Expand All @@ -85,7 +86,7 @@ def remove_vehicle(self, city, vehicle_id):
id {UUID} -- The vehicle's unique ID.
"""
return run_transaction(
sessionmaker(bind=self.engine),
self.sessionmaker,
lambda session: remove_vehicle_txn(session, city, vehicle_id))

def add_vehicle(self,
Expand Down Expand Up @@ -113,7 +114,7 @@ def add_vehicle(self,
is_owner {bool} -- The owner status of the user, before the vehicle is added. (default: {False})
"""
return run_transaction(
sessionmaker(bind=self.engine), lambda session: add_vehicle_txn(
self.sessionmaker, lambda session: add_vehicle_txn(
session, city, owner_id, last_location, type, color, brand,
status, is_owner))

Expand All @@ -127,7 +128,7 @@ def get_users(self, city):
Returns:
List -- A list of dictionaries containing user data.
"""
return run_transaction(sessionmaker(bind=self.engine),
return run_transaction(self.sessionmaker,
lambda session: get_users_txn(session, city))

def get_user(self, username=None, user_id=None):
Expand All @@ -142,7 +143,7 @@ def get_user(self, username=None, user_id=None):
User -- A User object.
"""
return run_transaction(
sessionmaker(bind=self.engine),
self.sessionmaker,
lambda session: get_user_txn(session, username, user_id))

def get_vehicles(self, city):
Expand All @@ -155,7 +156,7 @@ def get_vehicles(self, city):
Returns:
List -- A list of dictionaries containing vehicle data.
"""
return run_transaction(sessionmaker(bind=self.engine),
return run_transaction(self.sessionmaker,
lambda session: get_vehicles_txn(session, city))

def get_rides(self, rider_id):
Expand All @@ -169,5 +170,5 @@ def get_rides(self, rider_id):
List -- A list of dictionaries containing ride data.
"""
return run_transaction(
sessionmaker(bind=self.engine),
self.sessionmaker,
lambda session: get_rides_txn(session, rider_id))

0 comments on commit 1528f50

Please sign in to comment.