Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/sharedra.../cms_tool...
File: db.py
#! /opt/imh-python/bin/python3
[0] Fix | Delete
""" Database functions for working with CMS. """
[1] Fix | Delete
# Author: Daniel K
[2] Fix | Delete
[3] Fix | Delete
import os
[4] Fix | Delete
import re
[5] Fix | Delete
import logging
[6] Fix | Delete
import pymysql
[7] Fix | Delete
import glob
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
from rads import prompt_y_n
[11] Fix | Delete
from cms_tools.helpers import (
[12] Fix | Delete
common_get_string,
[13] Fix | Delete
make_valid_db_name,
[14] Fix | Delete
import_db,
[15] Fix | Delete
dump_db,
[16] Fix | Delete
db_exists,
[17] Fix | Delete
db_user_exists,
[18] Fix | Delete
create_db,
[19] Fix | Delete
create_db_user,
[20] Fix | Delete
change_db_pass,
[21] Fix | Delete
associate_db_user,
[22] Fix | Delete
get_mysql_err,
[23] Fix | Delete
)
[24] Fix | Delete
[25] Fix | Delete
from cms_tools.cms import CMSStatus, CMSError
[26] Fix | Delete
[27] Fix | Delete
[28] Fix | Delete
LOGGER = logging.getLogger(__name__)
[29] Fix | Delete
[30] Fix | Delete
[31] Fix | Delete
def import_cms_db(the_cms, database_file):
[32] Fix | Delete
'''
[33] Fix | Delete
Import database, using known credentials.
[34] Fix | Delete
Also, make backup if necessary.
[35] Fix | Delete
'''
[36] Fix | Delete
[37] Fix | Delete
LOGGER.debug(
[38] Fix | Delete
"Attempting to import %s into %s", database_file, the_cms.db_name
[39] Fix | Delete
)
[40] Fix | Delete
[41] Fix | Delete
database = the_cms.db_name
[42] Fix | Delete
[43] Fix | Delete
if database not in the_cms.modified_dbs:
[44] Fix | Delete
dump_file = dump_db(
[45] Fix | Delete
the_cms.db_user,
[46] Fix | Delete
the_cms.db_pass,
[47] Fix | Delete
the_cms.db_name,
[48] Fix | Delete
the_cms.directory_root,
[49] Fix | Delete
'cms_tools_backup',
[50] Fix | Delete
)
[51] Fix | Delete
if not dump_file:
[52] Fix | Delete
the_cms.set_status(
[53] Fix | Delete
CMSStatus.critical, "Unable to backup %s" % the_cms.db_name
[54] Fix | Delete
)
[55] Fix | Delete
raise CMSError("Unable to backup %s" % the_cms.db_name)
[56] Fix | Delete
[57] Fix | Delete
LOGGER.info("Modifying database %s", database)
[58] Fix | Delete
the_cms.modified_dbs[database] = dump_file
[59] Fix | Delete
[60] Fix | Delete
return import_db(
[61] Fix | Delete
the_cms.db_user, the_cms.db_pass, the_cms.db_name, database_file
[62] Fix | Delete
)
[63] Fix | Delete
[64] Fix | Delete
[65] Fix | Delete
def test_db_connection(the_cms):
[66] Fix | Delete
'''
[67] Fix | Delete
Test the database connection and return errors
[68] Fix | Delete
'''
[69] Fix | Delete
LOGGER.debug("Testing db connection")
[70] Fix | Delete
try:
[71] Fix | Delete
with pymysql.connect(
[72] Fix | Delete
host=the_cms.db_host,
[73] Fix | Delete
user=the_cms.db_user,
[74] Fix | Delete
password=the_cms.db_pass,
[75] Fix | Delete
database=the_cms.db_name,
[76] Fix | Delete
) as conn:
[77] Fix | Delete
with conn.cursor() as cursor:
[78] Fix | Delete
if cursor.execute("SHOW TABLES") == 0:
[79] Fix | Delete
return None # No tables, but connected
[80] Fix | Delete
except pymysql.Error as err:
[81] Fix | Delete
LOGGER.debug("Connection error")
[82] Fix | Delete
return err
[83] Fix | Delete
return None
[84] Fix | Delete
[85] Fix | Delete
[86] Fix | Delete
def simple_query(the_cms, field, table, search_field='', search_pattern=''):
[87] Fix | Delete
'''
[88] Fix | Delete
Search database returning specific field with optional search parameters
[89] Fix | Delete
'''
[90] Fix | Delete
[91] Fix | Delete
if the_cms.status < CMSStatus.db_has_matching_tables:
[92] Fix | Delete
LOGGER.error(
[93] Fix | Delete
"Database %s has not yet been confrmed working, "
[94] Fix | Delete
"but query attempted",
[95] Fix | Delete
the_cms.db_name,
[96] Fix | Delete
)
[97] Fix | Delete
return None
[98] Fix | Delete
[99] Fix | Delete
if search_pattern == '':
[100] Fix | Delete
if search_field != '':
[101] Fix | Delete
LOGGER.warning("Search field given, but no pattern given")
[102] Fix | Delete
search_field = ''
[103] Fix | Delete
# MySQL identifiers can't be escaped by execute() like literals can
[104] Fix | Delete
prefix = the_cms.db_pref.replace('`', '``')
[105] Fix | Delete
escaped_table = f"`{prefix}{table.replace('`', '``')}`"
[106] Fix | Delete
escaped_field = f"`{field.replace('`', '``')}`"
[107] Fix | Delete
if search_pattern == '':
[108] Fix | Delete
query = f"SELECT {escaped_field} FROM {escaped_table};"
[109] Fix | Delete
args = None
[110] Fix | Delete
else:
[111] Fix | Delete
escaped_search = f"`{search_field.replace('`', '``')}`"
[112] Fix | Delete
args = (search_pattern,)
[113] Fix | Delete
query = (
[114] Fix | Delete
f"SELECT {escaped_field} FROM {escaped_table} "
[115] Fix | Delete
f"WHERE {escaped_search} LIKE %s"
[116] Fix | Delete
)
[117] Fix | Delete
try:
[118] Fix | Delete
with pymysql.connect(
[119] Fix | Delete
host=the_cms.db_host,
[120] Fix | Delete
user=the_cms.db_user,
[121] Fix | Delete
password=the_cms.db_pass,
[122] Fix | Delete
database=the_cms.db_name,
[123] Fix | Delete
) as conn:
[124] Fix | Delete
with conn.cursor() as cursor:
[125] Fix | Delete
result = cursor.execute(query, args)
[126] Fix | Delete
if result < 1: # No tables, but connected
[127] Fix | Delete
return None
[128] Fix | Delete
return cursor.fetchall()[0]
[129] Fix | Delete
except pymysql.Error as err:
[130] Fix | Delete
LOGGER.error(err)
[131] Fix | Delete
return None
[132] Fix | Delete
[133] Fix | Delete
[134] Fix | Delete
def check_db_auth(the_cms):
[135] Fix | Delete
'''
[136] Fix | Delete
Check whether the database user and password is correct
[137] Fix | Delete
'''
[138] Fix | Delete
[139] Fix | Delete
# First, see whether the name uses a valid format
[140] Fix | Delete
if not re.match(
[141] Fix | Delete
"%s_[a-z0-9]{1,%d}" % (the_cms.dbprefix, 15 - len(the_cms.dbprefix)),
[142] Fix | Delete
the_cms.db_user,
[143] Fix | Delete
):
[144] Fix | Delete
LOGGER.info("Database username '%s' is not correct", the_cms.db_user)
[145] Fix | Delete
[146] Fix | Delete
new_name = make_valid_db_name(
[147] Fix | Delete
the_cms.cpuser, the_cms.dbprefix, the_cms.db_user, name_type="user"
[148] Fix | Delete
)
[149] Fix | Delete
if None is new_name:
[150] Fix | Delete
[151] Fix | Delete
# If we did not get a new name, allow the user to make one
[152] Fix | Delete
new_name = common_get_string(
[153] Fix | Delete
"What new name would you like? ",
[154] Fix | Delete
"%s_[a-z0-9]{1,%d}"
[155] Fix | Delete
% (the_cms.dbprefix, 15 - len(the_cms.dbprefix)),
[156] Fix | Delete
)
[157] Fix | Delete
if None is not new_name:
[158] Fix | Delete
if not the_cms.set_variable('db_user', new_name):
[159] Fix | Delete
return False
[160] Fix | Delete
the_cms.db_user = the_cms.get_variable('db_user')
[161] Fix | Delete
LOGGER.info("Username set to %s", the_cms.db_user)
[162] Fix | Delete
else:
[163] Fix | Delete
LOGGER.error("Username '%s' not reset!", the_cms.db_user)
[164] Fix | Delete
return False
[165] Fix | Delete
[166] Fix | Delete
else:
[167] Fix | Delete
[168] Fix | Delete
# We got a new name. Prompt to set it
[169] Fix | Delete
if the_cms.ilevel < 1 or prompt_y_n("Set name to %s? " % new_name):
[170] Fix | Delete
if not the_cms.set_variable('db_user', new_name):
[171] Fix | Delete
return False
[172] Fix | Delete
the_cms.db_user = the_cms.get_variable('db_user')
[173] Fix | Delete
LOGGER.info("Username set to %s", the_cms.db_user)
[174] Fix | Delete
else:
[175] Fix | Delete
new_name = common_get_string(
[176] Fix | Delete
"Use what database user name: ", 'database'
[177] Fix | Delete
)
[178] Fix | Delete
if not the_cms.set_variable('db_user', new_name):
[179] Fix | Delete
return False
[180] Fix | Delete
the_cms.db_user = the_cms.get_variable('db_user')
[181] Fix | Delete
LOGGER.info("Username set to %s", the_cms.db_user)
[182] Fix | Delete
[183] Fix | Delete
# Username is a valid format
[184] Fix | Delete
[185] Fix | Delete
# Does it exist and work?
[186] Fix | Delete
[187] Fix | Delete
# We can just check whether it exists, and if not, create it
[188] Fix | Delete
if not db_user_exists(the_cms.cpuser, the_cms.db_user):
[189] Fix | Delete
if the_cms.ilevel < 1 or prompt_y_n(
[190] Fix | Delete
"Database user '%s' does not exist. Create it?" % the_cms.db_user
[191] Fix | Delete
):
[192] Fix | Delete
create_db_user(the_cms.cpuser, the_cms.db_user, the_cms.db_pass)
[193] Fix | Delete
[194] Fix | Delete
# Check just in case it's not really added
[195] Fix | Delete
if not db_user_exists(the_cms.cpuser, the_cms.db_user):
[196] Fix | Delete
the_cms.set_status(
[197] Fix | Delete
CMSStatus.error,
[198] Fix | Delete
"Failed to create database user '%s'" % the_cms.db_user,
[199] Fix | Delete
)
[200] Fix | Delete
return False
[201] Fix | Delete
else:
[202] Fix | Delete
the_cms.set_status(
[203] Fix | Delete
CMSStatus.error, "Could not create %s" % the_cms.db_user
[204] Fix | Delete
)
[205] Fix | Delete
return False
[206] Fix | Delete
[207] Fix | Delete
# So, the db user exists. Does the pw match?
[208] Fix | Delete
result = test_db_connection(the_cms)
[209] Fix | Delete
if result is None:
[210] Fix | Delete
LOGGER.debug("Authorization fixed")
[211] Fix | Delete
return True
[212] Fix | Delete
if 1045 == result[0]:
[213] Fix | Delete
if the_cms.ilevel < 1 or prompt_y_n(
[214] Fix | Delete
"Password for user '%s' doesn't match. Reset it?" % the_cms.db_user
[215] Fix | Delete
):
[216] Fix | Delete
if not change_db_pass(
[217] Fix | Delete
the_cms.cpuser, the_cms.db_user, the_cms.db_pass
[218] Fix | Delete
):
[219] Fix | Delete
LOGGER.error("Could reset password for %s.", the_cms.db_user)
[220] Fix | Delete
return True
[221] Fix | Delete
LOGGER.error("Could not fix password for %s.", the_cms.db_user)
[222] Fix | Delete
return False
[223] Fix | Delete
if 1044 == result[0]:
[224] Fix | Delete
# The user isn't associated, but this confirms auth worked
[225] Fix | Delete
return True
[226] Fix | Delete
# Some other error, so we'll assume this is not the issue
[227] Fix | Delete
(errno, sterror) = result
[228] Fix | Delete
LOGGER.info(
[229] Fix | Delete
"Database connection failing. "
[230] Fix | Delete
"Can't check username. "
[231] Fix | Delete
"Receiving error:\n(%d): %s",
[232] Fix | Delete
errno,
[233] Fix | Delete
sterror,
[234] Fix | Delete
)
[235] Fix | Delete
return True
[236] Fix | Delete
[237] Fix | Delete
[238] Fix | Delete
# End check_db_auth
[239] Fix | Delete
[240] Fix | Delete
[241] Fix | Delete
def check_db_access(the_cms):
[242] Fix | Delete
'''
[243] Fix | Delete
Check whether the database exists and the user has privileges
[244] Fix | Delete
'''
[245] Fix | Delete
[246] Fix | Delete
# First, see whether the name uses a valid format
[247] Fix | Delete
if not re.match(
[248] Fix | Delete
"%s_[a-z0-9]{1,%d}" % (the_cms.dbprefix, 15 - len(the_cms.dbprefix)),
[249] Fix | Delete
the_cms.db_name,
[250] Fix | Delete
):
[251] Fix | Delete
LOGGER.info("Database name '%s' is not correct", the_cms.db_name)
[252] Fix | Delete
[253] Fix | Delete
new_name = make_valid_db_name(
[254] Fix | Delete
the_cms.cpuser, the_cms.dbprefix, the_cms.db_name
[255] Fix | Delete
)
[256] Fix | Delete
if None is new_name:
[257] Fix | Delete
[258] Fix | Delete
# If we did not get a new name, allow the user to make one
[259] Fix | Delete
new_name = common_get_string(
[260] Fix | Delete
"What new database name would you like? ",
[261] Fix | Delete
"%s_[a-z0-9]{1,%d}"
[262] Fix | Delete
% (the_cms.dbprefix, 15 - len(the_cms.dbprefix)),
[263] Fix | Delete
)
[264] Fix | Delete
if None is not new_name:
[265] Fix | Delete
if not the_cms.set_variable('db_name', new_name):
[266] Fix | Delete
return False
[267] Fix | Delete
the_cms.db_name = the_cms.get_variable('db_name')
[268] Fix | Delete
LOGGER.info("Database name set to %s", the_cms.db_name)
[269] Fix | Delete
else:
[270] Fix | Delete
the_cms.set_status(CMSStatus.error, "Database name not correct")
[271] Fix | Delete
return False
[272] Fix | Delete
[273] Fix | Delete
else:
[274] Fix | Delete
[275] Fix | Delete
# We got a new name. Prompt to set it
[276] Fix | Delete
if the_cms.ilevel < 1 or prompt_y_n("Set name to %s?" % new_name):
[277] Fix | Delete
if not the_cms.set_variable('db_name', new_name):
[278] Fix | Delete
return False
[279] Fix | Delete
the_cms.db_name = the_cms.get_variable('db_name')
[280] Fix | Delete
LOGGER.info("Database name set to %s", the_cms.db_name)
[281] Fix | Delete
else:
[282] Fix | Delete
new_name = common_get_string(
[283] Fix | Delete
"Use what database name: ", 'database'
[284] Fix | Delete
)
[285] Fix | Delete
if not the_cms.set_variable('db_name', new_name):
[286] Fix | Delete
return False
[287] Fix | Delete
the_cms.db_name = the_cms.get_variable('db_name')
[288] Fix | Delete
LOGGER.info("Database name set to %s", the_cms.db_name)
[289] Fix | Delete
[290] Fix | Delete
# Database name is a valid format
[291] Fix | Delete
[292] Fix | Delete
if not db_exists(the_cms.cpuser, the_cms.db_name):
[293] Fix | Delete
if the_cms.ilevel < 1 or prompt_y_n(
[294] Fix | Delete
"Database '%s' does not exist. Create it?" % the_cms.db_name
[295] Fix | Delete
):
[296] Fix | Delete
create_db(the_cms.cpuser, the_cms.db_name)
[297] Fix | Delete
if not db_exists(the_cms.cpuser, the_cms.db_name):
[298] Fix | Delete
the_cms.set_status(
[299] Fix | Delete
CMSStatus.error,
[300] Fix | Delete
"Failed to create database '%s'" % the_cms.db_name,
[301] Fix | Delete
)
[302] Fix | Delete
return False
[303] Fix | Delete
else:
[304] Fix | Delete
the_cms.set_status(CMSStatus.error, "Database could not be created")
[305] Fix | Delete
return False
[306] Fix | Delete
[307] Fix | Delete
# Did adding the database fix the problem?
[308] Fix | Delete
result = test_db_connection(the_cms)
[309] Fix | Delete
if result is None:
[310] Fix | Delete
# Yes, that did it
[311] Fix | Delete
LOGGER.debug("Database connection fixed.")
[312] Fix | Delete
return True
[313] Fix | Delete
errno, sterror = get_mysql_err(result)
[314] Fix | Delete
if errno not in (1044, 1049):
[315] Fix | Delete
# Not certain, but not the same error, so pretend that it did.
[316] Fix | Delete
LOGGER.error(
[317] Fix | Delete
"Still could not connect to '%s'. New error: %d: %s",
[318] Fix | Delete
the_cms.db_name,
[319] Fix | Delete
errno,
[320] Fix | Delete
sterror,
[321] Fix | Delete
)
[322] Fix | Delete
return True
[323] Fix | Delete
[324] Fix | Delete
LOGGER.debug(
[325] Fix | Delete
"The database exists, but the user cannot access the database."
[326] Fix | Delete
)
[327] Fix | Delete
[328] Fix | Delete
# If we've made it here, we can assign the user
[329] Fix | Delete
if the_cms.ilevel < 1 or prompt_y_n(
[330] Fix | Delete
"Associate database user '%s' with database '%s'?"
[331] Fix | Delete
% (the_cms.db_user, the_cms.db_name)
[332] Fix | Delete
):
[333] Fix | Delete
associate_db_user(the_cms.cpuser, the_cms.db_name, the_cms.db_user)
[334] Fix | Delete
else:
[335] Fix | Delete
the_cms.set_status(
[336] Fix | Delete
CMSStatus.error, "Could not associate database user."
[337] Fix | Delete
)
[338] Fix | Delete
LOGGER.warning("Could not associate database user.")
[339] Fix | Delete
return False
[340] Fix | Delete
[341] Fix | Delete
return True
[342] Fix | Delete
[343] Fix | Delete
[344] Fix | Delete
# End check_db_access
[345] Fix | Delete
[346] Fix | Delete
[347] Fix | Delete
def check_db_error(the_cms):
[348] Fix | Delete
'''
[349] Fix | Delete
Check for database connection errors.
[350] Fix | Delete
Return None if no error or number if there was an errror
[351] Fix | Delete
'''
[352] Fix | Delete
[353] Fix | Delete
# Make sure everything was set up first
[354] Fix | Delete
if the_cms.status < CMSStatus.db_is_set:
[355] Fix | Delete
LOGGER.warning(
[356] Fix | Delete
"Database credentials haven't been set. Last status: %s",
[357] Fix | Delete
the_cms.reason,
[358] Fix | Delete
)
[359] Fix | Delete
return -1
[360] Fix | Delete
[361] Fix | Delete
# Make sure that we're checking the local db first
[362] Fix | Delete
if 'localhost' != the_cms.db_host:
[363] Fix | Delete
LOGGER.info("Databse host is set to '%s'.", the_cms.db_host)
[364] Fix | Delete
if the_cms.ilevel < 1 or prompt_y_n("Set database host to localhost?"):
[365] Fix | Delete
if not the_cms.set_variable('db_host', "localhost"):
[366] Fix | Delete
return -1
[367] Fix | Delete
the_cms.db_host = the_cms.get_variable('db_host')
[368] Fix | Delete
LOGGER.debug("Database host has been fixed")
[369] Fix | Delete
[370] Fix | Delete
result = test_db_connection(the_cms)
[371] Fix | Delete
if result is None:
[372] Fix | Delete
LOGGER.debug("Database connection working")
[373] Fix | Delete
return None
[374] Fix | Delete
return get_mysql_err(result)[0]
[375] Fix | Delete
[376] Fix | Delete
[377] Fix | Delete
# End check_db_error
[378] Fix | Delete
[379] Fix | Delete
[380] Fix | Delete
def fix_db_error(the_cms, error_number):
[381] Fix | Delete
'''
[382] Fix | Delete
Check whether the database connection is working
[383] Fix | Delete
'''
[384] Fix | Delete
[385] Fix | Delete
if error_number is None:
[386] Fix | Delete
return True
[387] Fix | Delete
[388] Fix | Delete
if error_number == -1:
[389] Fix | Delete
return False
[390] Fix | Delete
[391] Fix | Delete
LOGGER.info("There was a database error for %s", the_cms.db_name)
[392] Fix | Delete
[393] Fix | Delete
if error_number == 1045:
[394] Fix | Delete
LOGGER.info("The username or password is incorrect")
[395] Fix | Delete
return check_db_auth(the_cms)
[396] Fix | Delete
if error_number in (1044, 1049):
[397] Fix | Delete
LOGGER.info("The user cannot access the database")
[398] Fix | Delete
return check_db_access(the_cms)
[399] Fix | Delete
if error_number == 2006:
[400] Fix | Delete
LOGGER.info(
[401] Fix | Delete
"MySQL server has gone away. May need to be researched manually"
[402] Fix | Delete
)
[403] Fix | Delete
return False
[404] Fix | Delete
# Unknown error
[405] Fix | Delete
LOGGER.error("Unknown error.")
[406] Fix | Delete
LOGGER.error(error_number)
[407] Fix | Delete
return False
[408] Fix | Delete
[409] Fix | Delete
[410] Fix | Delete
# End fix_db_error
[411] Fix | Delete
[412] Fix | Delete
[413] Fix | Delete
def check_db(the_cms):
[414] Fix | Delete
'''
[415] Fix | Delete
Check whether the database connection is working
[416] Fix | Delete
'''
[417] Fix | Delete
[418] Fix | Delete
# Make sure everything was set up first
[419] Fix | Delete
if the_cms.status < CMSStatus.db_is_set:
[420] Fix | Delete
LOGGER.warning(
[421] Fix | Delete
"Database credentials haven't been set. Last status: %s",
[422] Fix | Delete
the_cms.reason,
[423] Fix | Delete
)
[424] Fix | Delete
return False
[425] Fix | Delete
[426] Fix | Delete
# Make sure that we're checking the local db first
[427] Fix | Delete
if 'localhost' != the_cms.db_host:
[428] Fix | Delete
LOGGER.info("Databse host is set to '%s'.", the_cms.db_host)
[429] Fix | Delete
if the_cms.ilevel < 1 or prompt_y_n("Set database host to localhost?"):
[430] Fix | Delete
if not the_cms.set_variable('db_host', "localhost"):
[431] Fix | Delete
return False
[432] Fix | Delete
the_cms.db_host = the_cms.get_variable('db_host')
[433] Fix | Delete
LOGGER.debug("Database host has been fixed")
[434] Fix | Delete
[435] Fix | Delete
db_error = check_db_error(the_cms)
[436] Fix | Delete
count = 0
[437] Fix | Delete
while None is not db_error:
[438] Fix | Delete
if not fix_db_error(the_cms, db_error):
[439] Fix | Delete
LOGGER.info("Could not resolve database error %s", db_error)
[440] Fix | Delete
return False
[441] Fix | Delete
[442] Fix | Delete
count += 1
[443] Fix | Delete
if count > 10:
[444] Fix | Delete
LOGGER.error("Too many database errors. Giving up.")
[445] Fix | Delete
return False
[446] Fix | Delete
[447] Fix | Delete
db_error = check_db_error(the_cms)
[448] Fix | Delete
[449] Fix | Delete
the_cms.set_status(
[450] Fix | Delete
CMSStatus.db_is_connecting, "Database confirmed connected"
[451] Fix | Delete
)
[452] Fix | Delete
return True
[453] Fix | Delete
[454] Fix | Delete
[455] Fix | Delete
# End check_db
[456] Fix | Delete
[457] Fix | Delete
[458] Fix | Delete
def check_db_data(the_cms):
[459] Fix | Delete
'''
[460] Fix | Delete
Check database to ensure that is not empty, and that it has tables
[461] Fix | Delete
matching the prefix. If not, attempt to import.
[462] Fix | Delete
'''
[463] Fix | Delete
[464] Fix | Delete
if the_cms.status < CMSStatus.db_is_connecting:
[465] Fix | Delete
if not check_db(the_cms):
[466] Fix | Delete
LOGGER.warning(
[467] Fix | Delete
"Database has not been confirmed to connect. "
[468] Fix | Delete
"Cannot check database data."
[469] Fix | Delete
)
[470] Fix | Delete
return False
[471] Fix | Delete
[472] Fix | Delete
try:
[473] Fix | Delete
with pymysql.connect(
[474] Fix | Delete
host=the_cms.db_host,
[475] Fix | Delete
user=the_cms.db_user,
[476] Fix | Delete
password=the_cms.db_pass,
[477] Fix | Delete
database=the_cms.db_name,
[478] Fix | Delete
) as conn:
[479] Fix | Delete
with conn.cursor() as cursor:
[480] Fix | Delete
if cursor.execute("SHOW TABLES") == 0:
[481] Fix | Delete
LOGGER.info("No tables in '%s'", the_cms.db_name)
[482] Fix | Delete
return fix_empty_db(the_cms)
[483] Fix | Delete
the_cms.set_status(
[484] Fix | Delete
CMSStatus.db_has_tables, "Database has tables"
[485] Fix | Delete
)
[486] Fix | Delete
count = cursor.execute(
[487] Fix | Delete
"SHOW TABLES LIKE %s%%", (the_cms.db_pref,)
[488] Fix | Delete
)
[489] Fix | Delete
if count == 0:
[490] Fix | Delete
LOGGER.info(
[491] Fix | Delete
"Database '%s' has tables, "
[492] Fix | Delete
"but none matching the '%s' prefix.",
[493] Fix | Delete
the_cms.db_name,
[494] Fix | Delete
the_cms.db_pref,
[495] Fix | Delete
)
[496] Fix | Delete
return fix_empty_db(the_cms)
[497] Fix | Delete
except pymysql.Error as err:
[498] Fix | Delete
raise CMSError(f"Database query error: {err}") from err
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function