From cdd4e7bf03d0e4c0f486ed8d327151475167584e Mon Sep 17 00:00:00 2001 From: lgcareer <18610854716@163.com> Date: Thu, 23 Dec 2021 00:19:58 +0800 Subject: [PATCH] [Improvement-#7529][tools] Init DB schema from the full sql file. (#7530) --- ...re.sql => dolphinscheduler_postgresql.sql} | 0 .../datasource/CreateDolphinScheduler.java | 11 ++-- .../datasource/DolphinSchedulerManager.java | 13 +++-- .../tools/datasource/dao/UpgradeDao.java | 53 ++++--------------- 4 files changed, 26 insertions(+), 51 deletions(-) rename dolphinscheduler-dao/src/main/resources/sql/{dolphinscheduler_postgre.sql => dolphinscheduler_postgresql.sql} (100%) diff --git a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgre.sql b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql similarity index 100% rename from dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgre.sql rename to dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql diff --git a/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/CreateDolphinScheduler.java b/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/CreateDolphinScheduler.java index d204f7a32e..204dc5c2cc 100644 --- a/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/CreateDolphinScheduler.java +++ b/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/CreateDolphinScheduler.java @@ -42,10 +42,13 @@ public class CreateDolphinScheduler { @Override public void run(String... args) throws Exception { - dolphinSchedulerManager.initDolphinScheduler(); - logger.info("init DolphinScheduler finished"); - dolphinSchedulerManager.upgradeDolphinScheduler(); - logger.info("upgrade DolphinScheduler finished"); + if (dolphinSchedulerManager.schemaIsInitialized()) { + dolphinSchedulerManager.upgradeDolphinScheduler(); + logger.info("upgrade DolphinScheduler finished"); + } else { + dolphinSchedulerManager.initDolphinScheduler(); + logger.info("init DolphinScheduler finished"); + } logger.info("create DolphinScheduler success"); } } diff --git a/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/DolphinSchedulerManager.java b/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/DolphinSchedulerManager.java index 2788da8d4e..7830220086 100644 --- a/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/DolphinSchedulerManager.java +++ b/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/DolphinSchedulerManager.java @@ -55,21 +55,28 @@ public class DolphinSchedulerManager { } public void initDolphinScheduler() { + this.initDolphinSchedulerSchema(); + } + + /** + * whether schema is initialized + * @return true if schema is initialized + */ + public boolean schemaIsInitialized() { // Determines whether the dolphinscheduler table structure has been init if (upgradeDao.isExistsTable("t_escheduler_version") || upgradeDao.isExistsTable("t_ds_version") || upgradeDao.isExistsTable("t_escheduler_queue")) { logger.info("The database has been initialized. Skip the initialization step"); - return; + return true; } - this.initDolphinSchedulerSchema(); + return false; } public void initDolphinSchedulerSchema() { logger.info("Start initializing the DolphinScheduler manager table structure"); upgradeDao.initSchema(); } - public void upgradeDolphinScheduler() throws IOException { // Gets a list of all upgrades List schemaList = SchemaUtils.getAllSchemaList(); diff --git a/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/dao/UpgradeDao.java b/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/dao/UpgradeDao.java index c337936cb0..2978a62be1 100644 --- a/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/dao/UpgradeDao.java +++ b/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/dao/UpgradeDao.java @@ -89,52 +89,17 @@ public abstract class UpgradeDao { public abstract DbType getDbType(); public void initSchema() { - // Execute the dolphinscheduler DDL, it cannot be rolled back - runInitDDL(); - - // Execute the dolphinscheduler DML, it can be rolled back - runInitDML(); + // Execute the dolphinscheduler full sql + runInitSql(getDbType()); } - private void runInitDML() { - Resource mysqlSQLFilePath = new ClassPathResource("sql/" + initSqlPath() + "/dolphinscheduler_dml.sql"); - Connection conn = null; - try { - conn = dataSource.getConnection(); - conn.setAutoCommit(false); - - // Execute the dolphinscheduler_dml.sql script to import related data of dolphinscheduler - ScriptRunner initScriptRunner = new ScriptRunner(conn, false, true); - Reader initSqlReader = new InputStreamReader(mysqlSQLFilePath.getInputStream()); - initScriptRunner.runScript(initSqlReader); - - conn.commit(); - } catch (IOException e) { - try { - conn.rollback(); - } catch (SQLException e1) { - logger.error(e1.getMessage(), e1); - } - logger.error(e.getMessage(), e); - throw new RuntimeException(e.getMessage(), e); - } catch (Exception e) { - try { - if (null != conn) { - conn.rollback(); - } - } catch (SQLException e1) { - logger.error(e1.getMessage(), e1); - } - logger.error(e.getMessage(), e); - throw new RuntimeException(e.getMessage(), e); - } finally { - ConnectionUtils.releaseResource(conn); - } - - } - - private void runInitDDL() { - Resource mysqlSQLFilePath = new ClassPathResource("sql/" + initSqlPath() + "/dolphinscheduler_ddl.sql"); + /** + * run init sql to init db schema + * @param dbType db type + */ + private void runInitSql(DbType dbType) { + String sqlFile = String.format("dolphinscheduler_%s.sql",dbType.getDescp()); + Resource mysqlSQLFilePath = new ClassPathResource("sql/" + sqlFile); try (Connection conn = dataSource.getConnection()) { // Execute the dolphinscheduler_ddl.sql script to create the table structure of dolphinscheduler ScriptRunner initScriptRunner = new ScriptRunner(conn, true, true);