kgyrtkirk commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r740918261



##########
File path: 
itests/util/src/main/java/org/apache/hadoop/hive/ql/qoption/QTestDatabaseHandler.java
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql.qoption;
+
+import org.apache.hadoop.hive.ql.QTestUtil;
+import org.apache.hadoop.hive.ql.externalDB.AbstractExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.MSSQLServer;
+import org.apache.hadoop.hive.ql.externalDB.MariaDB;
+import org.apache.hadoop.hive.ql.externalDB.MySQLExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.Oracle;
+import org.apache.hadoop.hive.ql.externalDB.PostgresExternalDB;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * An option handler for spinning (resp. stopping) databases before (resp. 
after) running a test.
+ *
+ * Syntax: qt:database:DatabaseType:path_to_init_script
+ *
+ * The database type ({@link DatabaseType}) is obligatory argument. The 
initialization script can be omitted.
+ *
+ * Current limitations:
+ * <ol>
+ *   <li>Only one test/file per run</li>
+ *   <li>Does not support parallel execution</li>
+ *   <li>Cannot instantiate more than one database of the same type per 
test</li>
+ * </ol>
+ */
+public class QTestDatabaseHandler implements QTestOptionHandler {
+  private static final Logger LOG = 
LoggerFactory.getLogger(QTestDatabaseHandler.class);
+
+  private enum DatabaseType {
+    POSTGRES {
+      @Override
+      AbstractExternalDB create() {
+        return new PostgresExternalDB();
+      }
+    }, MYSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MySQLExternalDB();
+      }
+    }, MARIADB {
+      @Override
+      AbstractExternalDB create() {
+        return new MariaDB();
+      }
+    }, MSSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MSSQLServer();
+      }
+    }, ORACLE {
+      @Override
+      AbstractExternalDB create() {
+        return new Oracle();
+      }
+    };
+
+    abstract AbstractExternalDB create();
+  }
+
+  private final Map<DatabaseType, String> databaseToScript = new 
EnumMap<>(DatabaseType.class);
+
+  @Override
+  public void processArguments(String arguments) {
+    String[] args = arguments.split(":");
+    if (args.length == 0) {
+      throw new IllegalArgumentException("No arguments provided");
+    }
+    if (args.length > 2) {
+      throw new IllegalArgumentException(
+          "Too many arguments. Expected {dbtype:script}. Found: " + 
Arrays.toString(args));
+    }
+    DatabaseType dbType = DatabaseType.valueOf(args[0].toUpperCase());
+    String initScript = args.length == 2 ? args[1] : "";
+    if (databaseToScript.containsKey(dbType)) {
+      throw new IllegalArgumentException(dbType + " database is already 
defined in this file.");
+    }
+    databaseToScript.put(dbType, initScript);
+  }
+
+  @Override
+  public void beforeTest(QTestUtil qt) throws Exception {
+    if (databaseToScript.isEmpty()) {
+      return;
+    }
+    for (Map.Entry<DatabaseType, String> dbEntry : 
databaseToScript.entrySet()) {
+      String scriptsDir = QTestUtil.getScriptsDir(qt.getConf());
+      Path dbScript = Paths.get(scriptsDir, dbEntry.getValue());
+      AbstractExternalDB db = dbEntry.getKey().create();

Review comment:
       something like:
   * use some real class insted of the enum
   * retain an object for the lifetime of the backing docker container to have 
an object represent it in the code
   * give the instructions to that object
   
   but this is not that important...we can have it this way as well




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to