Exemplo n.º 1
0
def load(**kwargs):
    directoryFullPath = kwargs["directoryFullPath"]

    t = verify(manifestFileFullPath=os.path.join(directoryFullPath, MANIFEST_FILE_NAME))
    if t["retCode"] != 0:
        return {"retCode": 1, "errMsg": "Checksum verification failed!", "debug": t}

    if kwargs["configFileFullPath"]:
        DATAMINING_CONFIG = json.loads(pUtils.quickFileRead(kwargs["configFileFullPath"]))
    else:
        DATAMINING_CONFIG = json.loads(
            pUtils.quickFileRead(
                os.path.join(os.environ["MTP_TESTSTATION"], "MTP", "config", "miselu", "database", kwargs["config"])
            )
        )

    sql = SQL(**DATAMINING_CONFIG)
    sql.conn()

    for tableName in tableNameList:
        # Create Empty Temp table
        v = []
        s = "SELECT * INTO TEMP TABLE %s FROM %s"
        s += "\n WHERE testRunID!=testRunID"
        s = s % (tableName + "_t", tableName)
        sql.execute(s, v)

        # Load temp table
        v = []
        s = "COPY"
        s += "\n %s"
        s += "\n FROM STDIN"
        s += "\n WITH CSV "
        s += "\n  HEADER"
        s = s % (tableName + "_t")
        fileFullPath = os.path.join(directoryFullPath, fileBaseName + "_" + tableName + "." + extension)

        with open(fileFullPath, "rt") as f:
            sql.cur.copy_expert(s, f)

    # Create TestRubID_input reference table
    v = ["Table1", "Table2", "Table2"]
    s = "WITH Table2 as (SELECT testrunID FROM TestRun_t)"
    s += "\n, TableDiff AS ("
    s += "\n   SELECT MIN(tableName) as tableName"
    s += "\n   , testrunID"
    s += "\n   FROM"
    s += "\n   ("
    s += "\n    SELECT %s AS tableName"
    s += "\n    , testrunID"
    s += "\n    FROM TestRun"
    s += "\n    UNION ALL"
    s += "\n    SELECT %s AS tableName"
    s += "\n    , testrunID"
    s += "\n    FROM Table2"
    s += "\n   ) AS MCH_UnionTable1"
    s += "\n   GROUP BY testRunID"
    s += "\n   HAVING COUNT(*) = 1"
    s += "\n   ORDER BY tableName"
    s += "\n  )"
    s += "SELECT * INTO TEMP TABLE TestRunID_input FROM TableDiff WHERE tableName = %s"
    s += "\n;"
    sql.execute(s, v)

    for tableName in tableNameList:
        # Insert new records
        v = []
        s = "INSERT INTO %s"
        s += "\n (SELECT %s.* FROM %s,TestRunID_input WHERE TestRunID_input.testRunID = %s.testRunID)"
        s += "\n;"
        s = s % ((tableName,) + (tableName + "_t",) * 3)
        sql.execute(s, v)

    newRecordsAmount = sql.read("SELECT COUNT(*) FROM TestRunID_input", [])[0][0]
    sql.commit()
    sql.close()

    return {"retCode": 0, "errMsg": None, "newRecordsAmount": newRecordsAmount}