Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add method for asserting a table exists
- Loading branch information
David Besau
committed
Aug 27, 2021
1 parent
3d055ea
commit 2f59abb
Showing
2 changed files
with
22 additions
and
0 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 |
---|---|---|
@@ -1,11 +1,24 @@ | ||
from mysql.connector.abstracts import MySQLConnectionAbstract | ||
from mysql.connector.cursor import MySQLCursorAbstract | ||
|
||
import exception | ||
from clui import log | ||
|
||
SHOW_TABLES = "SHOW TABLES" | ||
|
||
|
||
def test_connection(cnx: MySQLConnectionAbstract) -> None: | ||
log("Testing database connection") | ||
if cnx.is_connected(): | ||
log(f"Successfully connected to MySQL {cnx.get_server_info()} on {cnx.server_host}.") | ||
else: | ||
log("Connection not working!") | ||
|
||
|
||
def assert_table_exists(cursor: MySQLCursorAbstract, table: str) -> None: | ||
cursor.execute(SHOW_TABLES) | ||
tables = cursor.fetchall() | ||
for row in tables: | ||
if table in row: | ||
return | ||
raise exception.TableNotFoundError(table) |
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 @@ | ||
class Error(Exception): | ||
def __init__(self, message: str): | ||
self.msg = message | ||
|
||
|
||
class TableNotFoundError(Error): | ||
def __init__(self, table: str): | ||
message = f"Could not find table {table}" | ||
super().__init__(message) |