Add --supported arg for rattail mysql-chars flag

to show what the underlying DB engine supports

this also enables "live mode" which will actually attempt to modify
live DB / tables...fingers crossed
This commit is contained in:
Lance Edgar 2023-06-17 20:29:14 -05:00
parent ad4f882c2e
commit 18f7b8a43f

View file

@ -68,6 +68,10 @@ class MysqlChars(Subcommand):
"not specified, all info will be shown for the "
"object(s) regardless of their charset/collation.")
parser.add_argument('--supported', action='store_true',
help="Instead of showing current DB/table info, show "
"what's actually supported by underlying DB engine.")
parser.add_argument('--fix', action='store_true',
help="Execute SQL to convert charset and/or collation "
"for relevant objects. Note, this will affect \"all\" "
@ -102,11 +106,32 @@ class MysqlChars(Subcommand):
engine.dialect.name, engine))
sys.exit(1)
if args.fix:
if args.supported:
self.view_supported(engine)
elif args.fix:
self.fix_db(engine, args)
else:
self.view_db(engine, args)
def view_supported(self, engine):
import sqlalchemy as sa
COLLATIONS = sa.sql.table(
'COLLATIONS',
sa.sql.column('COLLATION_NAME'),
sa.sql.column('CHARACTER_SET_NAME'),
sa.sql.column('IS_DEFAULT'),
schema='information_schema')
query = sa.sql.select(COLLATIONS.c.COLLATION_NAME,
COLLATIONS.c.CHARACTER_SET_NAME,
COLLATIONS.c.IS_DEFAULT)\
.order_by(COLLATIONS.c.COLLATION_NAME)
with engine.begin() as cxn:
result = cxn.execute(query)
self.show_results(result.fetchall())
def view_db(self, engine, args):
import sqlalchemy as sa
@ -170,7 +195,7 @@ class MysqlChars(Subcommand):
self.stdout.write("\n")
else:
raise NotImplementedError
engine.execute(stmt)
tablesinfo = self.fetch_tablesinfo(engine, args,
offenders_only=args.offenders)
@ -193,7 +218,7 @@ class MysqlChars(Subcommand):
compile_kwargs={'literal_binds': True})))
else:
raise NotImplementedError
engine.execute(stmt)
if args.dry_run:
self.stdout.write("\n")
@ -232,7 +257,7 @@ class MysqlChars(Subcommand):
compile_kwargs={'literal_binds': True})))
else:
raise NotImplementedError
engine.execute(stmt)
if args.dry_run and printed_header:
self.stdout.write("\n")