complete MetaDataTable and MetaDataColumn
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import mariadb
|
||||
from cement import Controller, ex
|
||||
from kontor_schema import KontorDB
|
||||
|
||||
|
||||
class Database(Controller):
|
||||
|
||||
class Meta:
|
||||
label = 'database'
|
||||
stacked_type = 'nested'
|
||||
@@ -49,3 +49,43 @@ class Database(Controller):
|
||||
kontor_db = KontorDB(self.app.engine, self.app.config, self.app.log)
|
||||
self.app.render(data, 'import.jinja2')
|
||||
kontor_db.import_db(data['db_file'], self.app.pargs.dry_run)
|
||||
|
||||
@ex(
|
||||
help='check the db schema against MetaDataTable and MetaDataColumn'
|
||||
)
|
||||
def check(self):
|
||||
mariadb_conn = mariadb.connect(
|
||||
host=self.app.config['mariadb']['host'],
|
||||
port=int(self.app.config['mariadb']['port']),
|
||||
user=self.app.config['mariadb']['user'],
|
||||
password=self.app.config['mariadb']['password'],
|
||||
database=self.app.config['mariadb']['database']
|
||||
)
|
||||
table_list = []
|
||||
cursor = mariadb_conn.cursor()
|
||||
cursor.execute("SHOW TABLES")
|
||||
for (tablename,) in cursor.fetchall():
|
||||
table_list.append(tablename)
|
||||
kontor_db = KontorDB(self.app.engine, self.app.log)
|
||||
table_names = kontor_db.get_table_names()
|
||||
for table in table_list:
|
||||
if table not in table_names:
|
||||
self.app.log.info(f"{table} is not stored in MetaDataTable")
|
||||
continue
|
||||
meta_data = kontor_db.get_columns(table)
|
||||
field_info = self.get_table_field_info(cursor, table)
|
||||
for column in field_info:
|
||||
if column not in meta_data:
|
||||
self.app.log.info(f"column {column} of table {table} is not in MetaDataColumn")
|
||||
mariadb_conn.close()
|
||||
|
||||
def get_table_field_info(self, cursor, table) -> dict:
|
||||
info = {}
|
||||
cursor.execute(f"SELECT * FROM {table} LIMIT 1")
|
||||
field_info = mariadb.fieldinfo()
|
||||
for column in cursor.description:
|
||||
column_name = column[0]
|
||||
column_type = field_info.type(column)
|
||||
column_flags = field_info.flag(column)
|
||||
info[column_name] = {"type": column_type, "flags": column_flags}
|
||||
return info
|
||||
|
||||
@@ -12,7 +12,6 @@ from .controllers.media import Media
|
||||
|
||||
# configuration defaults
|
||||
CONFIG = init_defaults('kontor', 'mariadb', 'media')
|
||||
CONFIG['kontor']['foo'] = 'bar'
|
||||
CONFIG['mariadb']['user'] = 'kontor'
|
||||
CONFIG['mariadb']['password'] = 'kontor'
|
||||
CONFIG['mariadb']['host'] = '127.0.0.1'
|
||||
@@ -21,8 +20,9 @@ CONFIG['mariadb']['database'] = 'kontor'
|
||||
CONFIG['media']['yt-dlp'] = '/home/tpeetz/bin/yt-dlp'
|
||||
CONFIG['media']['dir'] = '/data/media'
|
||||
|
||||
|
||||
def extend_sqlalchemy(app):
|
||||
app.log.info('extending kontor application with sqlalchemy')
|
||||
app.log.debug('extending kontor application with sqlalchemy')
|
||||
connect_string = ('mariadb+mariadbconnector://{}:{}@{}:{}/{}'.format(
|
||||
app.config.get('mariadb', 'user'),
|
||||
app.config.get('mariadb', 'password'),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
-e /home/tpeetz/projects/kontor/python/kontor-schema
|
||||
-e /home/tpeetz/projects/kontor/python/kontor-video
|
||||
-e ../kontor-schema
|
||||
-e ../kontor-video
|
||||
|
||||
cement==3.0.12
|
||||
cement[jinja2]
|
||||
|
||||
Reference in New Issue
Block a user