Repository: gluufederation/community-edition-setup
Issue: fix: maridb json columns 

When creating tables in mariadb,  add constaint `check (json_valid(column_name))`

Changed files:
setup_app/utils/db_utils.py, setup_app/installers/rdbm.py

diff --git a/setup_app/installers/rdbm.py b/setup_app/installers/rdbm.py
index f02c0e23..2dd404ff 100644
--- a/setup_app/installers/rdbm.py
+++ b/setup_app/installers/rdbm.py
@@ -76,8 +76,8 @@ class RDBMInstaller(BaseInstaller, SetupUtils):
         not_null = ' NOT NULL' if sql_tbl_name=='gluuPerson' and attrname=='inum' else ''
         data_type = self.get_sql_col_type(attrname, sql_tbl_name)
         col_def = '{0}{1}{0} {2}{3}'.format(self.qchar, attrname, data_type, not_null)
-        if Config.rdbm_type == 'mysql' and data_type == 'JSON':
-            col_def += ' comment "json"'
+        if Config.rdbm_type == 'mysql' and self.dbUtils.mariadb and data_type == 'JSON':
+            col_def += ' check (json_valid({0}{1}{0}))'.format(self.qchar, attrname)
         return col_def
 
     def local_install(self):
diff --git a/setup_app/utils/db_utils.py b/setup_app/utils/db_utils.py
index eb4cc782..fea6473d 100644
--- a/setup_app/utils/db_utils.py
+++ b/setup_app/utils/db_utils.py
@@ -41,6 +41,7 @@ class DBUtils(SetupUtils):
     Base = None
     session = None
     cbm = None
+    mariadb = False
 
     def bind(self, use_ssl=True, force=False):
 
@@ -108,7 +109,6 @@ class DBUtils(SetupUtils):
         base.logIt("Making {} Connection to {}:{}/{} with user {}".format(Config.rdbm_type.upper(), Config.rdbm_host, Config.rdbm_port, Config.rdbm_db, Config.rdbm_user))
 
         db_str = 'mysql+pymysql' if Config.rdbm_type == 'mysql' else 'postgresql+psycopg2'
-
         bind_uri = '{}://{}:{}@{}:{}/{}'.format(
                         db_str,
                         Config.rdbm_user,
@@ -128,6 +128,7 @@ class DBUtils(SetupUtils):
             self.session = Session()
             self.metadata = sqlalchemy.MetaData()
             self.session.connection()
+
             base.logIt("{} Connection was successful".format(Config.rdbm_type.upper()))
             if Config.rdbm_type == 'mysql':
                 self.set_mysql_version()
@@ -150,6 +151,11 @@ class DBUtils(SetupUtils):
             base.logIt("Cant determine MySQL version due to {}. Set to unknown".format(e))
             self.mysql_version = (0, 0, 0)
 
+        # are we on MariDB?
+        version_query = self.engine.execute(sqlalchemy.text('SELECT VERSION()'))
+        version_query_result = version_query.fetchone()
+        if version_query_result:
+            self.mariadb = 'mariadb' in version_query_result[0].lower()
 
     @property
     def json_dialects_instance(self):
@@ -762,11 +768,12 @@ class DBUtils(SetupUtils):
         self.Base.prepare()
 
         # fix JSON type for mariadb
-        if Config.rdbm_type == 'mysql':
+        if Config.rdbm_type == 'mysql' and self.mariadb:
             for tbl in self.Base.classes:
-                for col in tbl.__table__.columns:
-                    if isinstance(col.type, sqlalchemy.dialects.mysql.LONGTEXT) and col.comment.lower() == 'json':
-                        col.type = sqlalchemy.dialects.mysql.json.JSON()
+                slq_query = self.engine.execute(sqlalchemy.text('SELECT CONSTRAINT_NAME from INFORMATION_SCHEMA.CHECK_CONSTRAINTS where TABLE_NAME="{}" and CHECK_CLAUSE like "%json_valid%"'.format(tbl.__table__.name)))
+                slq_query_result = slq_query.fetchall()
+                for col in slq_query_result:
+                    tbl.__table__.columns[col[0]].type = sqlalchemy.dialects.mysql.json.JSON()
 
         base.logIt("Reflected tables {}".format(list(self.metadata.tables.keys())))