This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Implement fluent interface; fix mypy issues; rename set_up, tear_down as connect, close; update uses; set up default append tables * Remove metadata tables from env_blank.json * Remove page_num condition * Update APPEND_TABLE_NAMES * Remove close, connect; update var names; make other tweaks * Update DBCreator references * Remove conn instance variable * Remove page_num condition again * Remove unused imports * Update default append table names var name
- Loading branch information
Showing
6 changed files
with
37 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,6 @@ | |
"password": "" | ||
}, | ||
"APPEND_TABLE_NAMES": [ | ||
"job_run", "data_source_status", | ||
"mivideo_media_started_hourly" | ||
] | ||
} |
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 |
---|---|---|
@@ -1,70 +1,73 @@ | ||
# This is needed for type hinting with fluent interfaces | ||
from __future__ import annotations | ||
|
||
# standard libraries | ||
import logging, os | ||
from typing import Dict, Sequence | ||
from typing import Dict, List | ||
from urllib.parse import quote_plus | ||
|
||
# third-party libraries | ||
from sqlalchemy.engine import create_engine | ||
from sqlalchemy.engine import create_engine, Engine | ||
from yoyo import get_backend, read_migrations | ||
|
||
|
||
# Initialize settings and global variables | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
MIGRATIONS_PATH = os.path.join(ROOT_DIR, 'migrations') | ||
PARENT_PATH = os.path.dirname(os.path.abspath(__file__)) | ||
MIGRATIONS_PATH = os.path.join(PARENT_PATH, 'migrations') | ||
|
||
# The metadata tables are append tables by default. | ||
DEFAULT_APPEND_TABLE_NAMES = ['job_run', 'data_source_status'] | ||
|
||
|
||
class DBCreator: | ||
|
||
def __init__( | ||
self, | ||
db_params: Dict[str, str], | ||
append_table_names: Sequence[str] = [] | ||
append_table_names: List[str] = [] | ||
) -> None: | ||
|
||
self.db_name = db_params['dbname'] | ||
self.conn = None | ||
self.conn_str = ( | ||
self.db_name: str = db_params['dbname'] | ||
self.conn_str: str = ( | ||
'mysql+mysqldb' + | ||
f"://{db_params['user']}" + | ||
f":{quote_plus(db_params['password'])}" + | ||
f"@{db_params['host']}" + | ||
f":{db_params['port']}" + | ||
f"/{db_params['dbname']}?charset=utf8&ssl=true" | ||
) | ||
self.engine = create_engine(self.conn_str) | ||
self.append_table_names = append_table_names | ||
|
||
def set_up(self) -> None: | ||
logger.debug('set_up') | ||
self.conn = self.engine.connect() | ||
self.engine: Engine = create_engine(self.conn_str) | ||
|
||
def tear_down(self) -> None: | ||
logger.debug('tear_down') | ||
self.conn.close() | ||
self.append_table_names: List[str] = append_table_names | ||
self.append_table_names += DEFAULT_APPEND_TABLE_NAMES | ||
|
||
def get_table_names(self) -> Sequence[str]: | ||
def get_table_names(self) -> List[str]: | ||
logger.debug('get_table_names') | ||
return self.engine.table_names() | ||
|
||
def migrate(self) -> None: | ||
def migrate(self) -> DBCreator: | ||
logger.debug('migrate') | ||
backend = get_backend(self.conn_str) | ||
migrations = read_migrations(MIGRATIONS_PATH) | ||
with backend.lock(): | ||
backend.apply_migrations(backend.to_apply(migrations)) | ||
return self | ||
|
||
def drop_records(self) -> None: | ||
def drop_records(self) -> DBCreator: | ||
logger.debug('drop_records') | ||
self.conn.execute('SET FOREIGN_KEY_CHECKS=0;') | ||
conn = self.engine.connect() | ||
conn.execute('SET FOREIGN_KEY_CHECKS=0;') | ||
for table_name in self.get_table_names(): | ||
if 'yoyo' not in table_name and table_name not in self.append_table_names: | ||
logger.debug(f'Table Name: {table_name}') | ||
self.conn.execute(f'DELETE FROM {table_name};') | ||
conn.execute(f'DELETE FROM {table_name};') | ||
logger.info(f'Dropped records in {table_name} in {self.db_name}') | ||
self.conn.execute('SET FOREIGN_KEY_CHECKS=1;') | ||
conn.execute('SET FOREIGN_KEY_CHECKS=1;') | ||
return self | ||
|
||
def set_up_database(self) -> None: | ||
self.set_up() | ||
self.drop_records() | ||
self.migrate() | ||
self.tear_down() | ||
def set_up_database(self) -> DBCreator: | ||
self.drop_records().migrate() | ||
return self |
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