-
-
Notifications
You must be signed in to change notification settings - Fork 64
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 #601 from hackforla/550-BACK-PersistingDBConnections
isolated db in its own module
- Loading branch information
Showing
13 changed files
with
73 additions
and
55 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
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
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
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
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
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,34 @@ | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
from sqlalchemy.sql import text | ||
|
||
|
||
class Database(object): | ||
def __init__(self, verbose=False): | ||
self.verbose = verbose | ||
|
||
def config(self, config): | ||
self.engine = create_engine(config['DB_CONNECTION_STRING']) | ||
self.Session = sessionmaker(bind=self.engine) | ||
|
||
if self.verbose: | ||
self.log_connection_events() | ||
|
||
def exec_sql(self, sql): | ||
with self.engine.connect() as conn: | ||
return conn.execute(text(sql)) | ||
|
||
def log_connection_events(self): | ||
def on_checkout(*args, **kwargs): | ||
print('process id {} checkout'.format(os.getpid()), flush=True) | ||
|
||
def on_checkin(*args, **kwargs): | ||
print('process id {} checkin'.format(os.getpid()), flush=True) | ||
|
||
from sqlalchemy import event | ||
import os | ||
event.listen(self.engine, 'checkout', on_checkout) | ||
event.listen(self.engine, 'checkin', on_checkin) | ||
|
||
|
||
db = Database() |
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,7 @@ | ||
import sys | ||
from os.path import dirname, abspath, join | ||
|
||
|
||
# add src directory to path so pytest can find modules | ||
src_dir = join(dirname(abspath(__file__)), '..', 'src') | ||
sys.path.append(src_dir) |
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