Github user njayaram2 commented on a diff in the pull request:

    https://github.com/apache/madlib/pull/271#discussion_r192204168
  
    --- Diff: src/madpack/madpack.py ---
    @@ -824,6 +873,246 @@ def parse_arguments():
         # Get the arguments
         return parser.parse_args()
     
    +def run_install_check(args, testcase):
    +    schema = args['schema']
    +    dbrev = args['dbrev']
    +    # 1) Compare OS and DB versions. Continue if OS = DB.
    +    if get_rev_num(dbrev) != get_rev_num(rev):
    +        _print_revs(rev, dbrev, con_args, schema)
    +        info_(this, "Versions do not match. Install-check stopped.", True)
    +        return
    +
    +    # Create install-check user
    +    test_user = ('madlib_' +
    +                 rev.replace('.', '').replace('-', '_') +
    +                 '_installcheck')
    +    try:
    +        _internal_run_query("DROP USER IF EXISTS %s;" % (test_user), False)
    +    except:
    +        _internal_run_query("DROP OWNED BY %s CASCADE;" % (test_user), 
True)
    +        _internal_run_query("DROP USER IF EXISTS %s;" % (test_user), True)
    +    _internal_run_query("CREATE USER %s;" % (test_user), True)
    +
    +    _internal_run_query("GRANT USAGE ON SCHEMA %s TO %s;" % (schema, 
test_user), True)
    +
    +    # 2) Run test SQLs
    +    info_(this, "> Running test scripts for:", verbose)
    +
    +    caseset = (set([test.strip() for test in testcase.split(',')])
    +               if testcase != "" else set())
    +
    +    modset = {}
    +    for case in caseset:
    +        if case.find('/') > -1:
    +            [mod, algo] = case.split('/')
    +            if mod not in modset:
    +                modset[mod] = []
    +            if algo not in modset[mod]:
    +                modset[mod].append(algo)
    +        else:
    +            modset[case] = []
    +
    +    # Loop through all modules
    +    for moduleinfo in portspecs['modules']:
    +
    +        # Get module name
    +        module = moduleinfo['name']
    +
    +        # Skip if doesn't meet specified modules
    +        if modset is not None and len(modset) > 0 and module not in modset:
    +            continue
    +        # JIRA: MADLIB-1078 fix
    +        # Skip pmml during install-check (when run without the -t option).
    +        # We can still run install-check on pmml with '-t' option.
    +        if not modset and module in ['pmml']:
    +            continue
    +        info_(this, "> - %s" % module, verbose)
    +
    +        # Make a temp dir for this module (if doesn't exist)
    +        cur_tmpdir = tmpdir + '/' + module + '/test'  # tmpdir is a global 
variable
    +        _make_dir(cur_tmpdir)
    +
    +        # Find the Python module dir (platform specific or generic)
    +        if os.path.isdir(maddir + "/ports/" + portid + "/" + dbver + 
"/modules/" + module):
    +            maddir_mod_py = maddir + "/ports/" + portid + "/" + dbver + 
"/modules"
    +        else:
    +            maddir_mod_py = maddir + "/modules"
    +
    +        # Find the SQL module dir (platform specific or generic)
    +        if os.path.isdir(maddir + "/ports/" + portid + "/modules/" + 
module):
    +            maddir_mod_sql = maddir + "/ports/" + portid + "/modules"
    +        else:
    +            maddir_mod_sql = maddir + "/modules"
    +
    +        # Prepare test schema
    +        test_schema = "madlib_installcheck_%s" % (module)
    +        _internal_run_query("DROP SCHEMA IF EXISTS %s CASCADE; CREATE 
SCHEMA %s;" %
    +                            (test_schema, test_schema), True)
    +        _internal_run_query("GRANT ALL ON SCHEMA %s TO %s;" %
    +                            (test_schema, test_user), True)
    +
    +        # Switch to test user and prepare the search_path
    +        pre_sql = '-- Switch to test user:\n' \
    +                  'SET ROLE %s;\n' \
    +                  '-- Set SEARCH_PATH for install-check:\n' \
    +                  'SET search_path=%s,%s;\n' \
    +                  % (test_user, test_schema, schema)
    +
    +        # Loop through all test SQL files for this module
    +        sql_files = maddir_mod_sql + '/' + module + '/test/*.sql_in'
    +        for sqlfile in sorted(glob.glob(sql_files), reverse=True):
    +            algoname = os.path.basename(sqlfile).split('.')[0]
    +            # run only algo specified
    +            if (module in modset and modset[module] and
    +                    algoname not in modset[module]):
    +                continue
    +
    +            # Set file names
    +            tmpfile = cur_tmpdir + '/' + os.path.basename(sqlfile) + '.tmp'
    +            logfile = cur_tmpdir + '/' + os.path.basename(sqlfile) + '.log'
    +
    +            # If there is no problem with the SQL file
    +            milliseconds = 0
    +
    +            # Run the SQL
    +            run_start = datetime.datetime.now()
    +            retval = _run_sql_file_install_check(schema, maddir_mod_py,
    +                                                 module, sqlfile, tmpfile,
    +                                                 logfile, pre_sql)
    +            # Runtime evaluation
    +            run_end = datetime.datetime.now()
    +            milliseconds = round((run_end - run_start).seconds * 1000 +
    +                                 (run_end - run_start).microseconds / 1000)
    +
    +            # Check the exit status
    +            result = _parse_result_logfile(retval, logfile, tmpfile, 
sqlfile,
    +                                           module, milliseconds)
    +
    +        # Cleanup test schema for the module
    +        _internal_run_query("DROP SCHEMA IF EXISTS %s CASCADE;" % 
(test_schema), True)
    +
    +    # Drop install-check user
    +    _internal_run_query("DROP OWNED BY %s CASCADE;" % (test_user), True)
    +    _internal_run_query("DROP USER %s;" % (test_user), True)
    +
    +def create_install_madlib_sqlfile(args, madpack_cmd, testcase):
    +    upgrade = args['upgrade']
    +    schema = args['schema']
    +    dbrev = args['dbrev']
    +    is_schema_in_db = args['is_schema_in_db']
    +    with open(args['output_filename'], 'a+') as output_filehandle:
    +        # COMMAND: uninstall/reinstall
    +        if madpack_cmd in ('uninstall', 'reinstall'):
    +            if get_rev_num(dbrev) == [0]:
    +                info_(this, "Nothing to uninstall. No version found in 
schema %s." % schema.upper(), True)
    +                return 1
    +
    +            # Find any potential data to lose
    +            affected_objects = _internal_run_query("""
    +                SELECT
    +                    n1.nspname AS schema,
    +                    relname AS relation,
    +                    attname AS column,
    +                    typname AS type
    +                FROM
    +                    pg_attribute a,
    +                    pg_class c,
    +                    pg_type t,
    +                    pg_namespace n,
    +                    pg_namespace n1
    +                WHERE
    +                    n.nspname = '%s'
    +                    AND t.typnamespace = n.oid
    +                    AND a.atttypid = t.oid
    +                    AND c.oid = a.attrelid
    +                    AND c.relnamespace = n1.oid
    +                    AND c.relkind = 'r'
    +                ORDER BY
    +                    n1.nspname, relname, attname, typname""" % 
schema.lower(), True)
    +
    +            info_(this, "*** Uninstalling MADlib ***", True)
    +            info_(this, 
"***********************************************************************************",
 True)
    +            info_(this, "* Schema %s and all database objects depending on 
it will be dropped!" % schema.upper(), True)
    +            if affected_objects:
    +                info_(this, "* If you continue the following data will be 
lost (schema : table.column : type):", True)
    +                for ao in affected_objects:
    +                    info_(this, '* - ' + ao['schema'] + ' : ' + 
ao['relation'] + '.' +
    +                          ao['column'] + ' : ' + ao['type'], True)
    +            info_(this, 
"***********************************************************************************",
 True)
    +            info_(this, "Would you like to continue? [Y/N]", True)
    +            go = raw_input('>>> ').upper()
    +            while (go not in ('Y', 'N', 'YES', 'NO')):
    +                go = raw_input('Yes or No >>> ').upper()
    +
    +            # 2) Do the uninstall/drop
    +            if go in ('N', 'NO'):
    +                info_(this, 'No problem. Nothing dropped.', True)
    +                return 1
    +            elif go in ('Y', 'YES'):
    +                try:
    +                    _write_to_file(output_filehandle,
    +                                   "DROP SCHEMA %s CASCADE;" % (schema), 
True)
    +                    is_schema_in_db = False
    +                except:
    +                    error_(this, "Cannot drop schema %s." % 
schema.upper(), True)
    +
    +            else:
    +                return 1
    +
    +        # COMMAND: install/reinstall
    +        if madpack_cmd in ('install', 'reinstall'):
    +            # Refresh MADlib version in DB, None for GP/PG
    +            if madpack_cmd == 'reinstall':
    +                info_(this, "Setting MADlib database version to be None 
for reinstall", verbose)
    +                dbrev = None
    +
    +            info_(this, "*** Installing MADlib ***", True)
    +
    +            # 1) Compare OS and DB versions.
    +            # noop if OS <= DB.
    +            _print_revs(rev, dbrev, con_args, schema)
    +            if is_rev_gte(get_rev_num(dbrev), get_rev_num(rev)):
    --- End diff --
    
    Keeping the `if elif`, but changed the order. Although the checks are on 
different variables, they are directly related.
    Renamed variables: `dbrev`->`db_madlib_ver`, and `rev`->`new_madlib_ver`.


---

Reply via email to