From 5a7a122e2dbf16dc0a976c87a693719a83e527ee Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 6 Aug 2019 18:25:14 -0500 Subject: [PATCH] Add `mysql.table_exists()` convenience function --- rattail_fabric2/mysql.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rattail_fabric2/mysql.py b/rattail_fabric2/mysql.py index dba8d6a..9e3e8e4 100644 --- a/rattail_fabric2/mysql.py +++ b/rattail_fabric2/mysql.py @@ -93,6 +93,16 @@ def grant_access(c, dbname, username): sql(c, 'grant all on `{}`.* to {}'.format(dbname, username)) +def table_exists(c, tblname, dbname): + """ + Determine if given table exists in given DB. + """ + # TODO: should avoid sql injection here... + query = "SELECT TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA = '{}' AND TABLE_NAME = '{}'".format(dbname, tblname) + name = sql(c, query, database='information_schema').stdout.strip() + return name == tblname + + def sql(c, sql, database=''): """ Execute some SQL.