This is an automated email from the ASF dual-hosted git repository.

jinsongzhou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/amoro.git


The following commit(s) were added to refs/heads/master by this push:
     new 63eb99baa [Improvement]: Add an AMS configuration property to skip 
table creation when AMS startup (#3144)
63eb99baa is described below

commit 63eb99baa62e5e8a99df03c4a6a416d114bd79bf
Author: baiyangtx <[email protected]>
AuthorDate: Fri Aug 30 16:24:04 2024 +0800

    [Improvement]: Add an AMS configuration property to skip table creation 
when AMS startup (#3144)
    
    * skip tables creation
    
    * skip tables creation
---
 .../org/apache/amoro/server/AmoroManagementConf.java |  6 ++++++
 .../persistence/SqlSessionFactoryProvider.java       | 20 +++++++++++++++++---
 amoro-ams/dist/src/main/amoro-bin/conf/config.yaml   |  2 ++
 docs/admin-guides/deployment.md                      |  4 ++++
 4 files changed, 29 insertions(+), 3 deletions(-)

diff --git 
a/amoro-ams/amoro-ams-server/src/main/java/org/apache/amoro/server/AmoroManagementConf.java
 
b/amoro-ams/amoro-ams-server/src/main/java/org/apache/amoro/server/AmoroManagementConf.java
index b63c13246..871ab5bc9 100644
--- 
a/amoro-ams/amoro-ams-server/src/main/java/org/apache/amoro/server/AmoroManagementConf.java
+++ 
b/amoro-ams/amoro-ams-server/src/main/java/org/apache/amoro/server/AmoroManagementConf.java
@@ -260,6 +260,12 @@ public class AmoroManagementConf {
           .defaultValue("jdbc:derby:/tmp/amoro/derby;create=true")
           .withDescription("Database connection address");
 
+  public static final ConfigOption<Boolean> DB_AUTO_CREATE_TABLES =
+      ConfigOptions.key("database.auto-create-tables")
+          .booleanType()
+          .defaultValue(true)
+          .withDescription("Auto init table schema when started");
+
   public static final ConfigOption<String> DB_DRIVER_CLASS_NAME =
       ConfigOptions.key("database.jdbc-driver-class")
           .stringType()
diff --git 
a/amoro-ams/amoro-ams-server/src/main/java/org/apache/amoro/server/persistence/SqlSessionFactoryProvider.java
 
b/amoro-ams/amoro-ams-server/src/main/java/org/apache/amoro/server/persistence/SqlSessionFactoryProvider.java
index 2230e0f76..6a59d34a8 100644
--- 
a/amoro-ams/amoro-ams-server/src/main/java/org/apache/amoro/server/persistence/SqlSessionFactoryProvider.java
+++ 
b/amoro-ams/amoro-ams-server/src/main/java/org/apache/amoro/server/persistence/SqlSessionFactoryProvider.java
@@ -40,6 +40,8 @@ import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 import org.apache.ibatis.transaction.TransactionFactory;
 import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.InputStreamReader;
 import java.net.URI;
@@ -47,6 +49,7 @@ import java.net.URISyntaxException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.sql.Connection;
 import java.sql.ResultSet;
@@ -54,6 +57,7 @@ import java.sql.Statement;
 import java.time.Duration;
 
 public class SqlSessionFactoryProvider {
+  private static final Logger LOG = 
LoggerFactory.getLogger(SqlSessionFactoryProvider.class);
 
   private static final String DERBY_INIT_SQL_SCRIPT = 
"derby/ams-derby-init.sql";
   private static final String MYSQL_INIT_SQL_SCRIPT = 
"mysql/ams-mysql-init.sql";
@@ -123,8 +127,14 @@ public class SqlSessionFactoryProvider {
    * @param config
    */
   private void createTablesIfNeed(Configurations config) {
+    boolean initSchema = 
config.getBoolean(AmoroManagementConf.DB_AUTO_CREATE_TABLES);
+    if (!initSchema) {
+      LOG.info("Skip auto create tables due to configuration");
+      return;
+    }
     String dbTypeConfig = config.getString(AmoroManagementConf.DB_TYPE);
     String query = "";
+    LOG.info("Start create tables, database type:{}", dbTypeConfig);
 
     try (SqlSession sqlSession = get().openSession(true);
         Connection connection = sqlSession.getConnection();
@@ -142,13 +152,17 @@ public class SqlSessionFactoryProvider {
                 "SELECT 1 FROM information_schema.tables WHERE table_schema = 
%s AND table_name = '%s'",
                 "current_schema()", "catalog_metadata");
       }
+      LOG.info("Start check table creation, using query: {}", query);
       try (ResultSet rs = statement.executeQuery(query)) {
         if (!rs.next()) {
+          Path script = Paths.get(getInitSqlScriptPath(dbTypeConfig));
+          LOG.info("Table not exists, start run create tables script file:{}", 
script);
           ScriptRunner runner = new ScriptRunner(connection);
           runner.runScript(
-              new InputStreamReader(
-                  
Files.newInputStream(Paths.get(getInitSqlScriptPath(dbTypeConfig))),
-                  StandardCharsets.UTF_8));
+              new InputStreamReader(Files.newInputStream(script), 
StandardCharsets.UTF_8));
+          LOG.info("Tables are created successfully");
+        } else {
+          LOG.info("Tables are created, skip auto create tables.");
         }
       }
     } catch (Exception e) {
diff --git a/amoro-ams/dist/src/main/amoro-bin/conf/config.yaml 
b/amoro-ams/dist/src/main/amoro-bin/conf/config.yaml
index f77917c32..d16027c34 100644
--- a/amoro-ams/dist/src/main/amoro-bin/conf/config.yaml
+++ b/amoro-ams/dist/src/main/amoro-bin/conf/config.yaml
@@ -103,6 +103,7 @@ ams:
 #    url: 
jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useAffectedRows=true&allowPublicKeyRetrieval=true&useSSL=false
 #    username: root
 #    password: root
+#    auto-create-tables: true
 #    connection-pool-max-total: 20
 #    connection-pool-max-idle: 16
 #    connection-pool-max-wait-millis: 1000
@@ -114,6 +115,7 @@ ams:
 #    url: jdbc:postgresql://127.0.0.1:5432/db
 #    username: user
 #    password: passwd
+#    auto-create-tables: true
 #    connection-pool-max-total: 20
 #    connection-pool-max-idle: 16
 #    connection-pool-max-wait-millis: 1000
diff --git a/docs/admin-guides/deployment.md b/docs/admin-guides/deployment.md
index 8404f4ec8..30fb903fa 100644
--- a/docs/admin-guides/deployment.md
+++ b/docs/admin-guides/deployment.md
@@ -111,6 +111,8 @@ $ mv mysql-connector-java-${MYSQL_JDBC_DRIVER_VERSION}.jar 
lib
 ```
 
 Create an empty database in MySQL/PostgreSQL, then AMS will automatically 
create tables in this MySQL/PostgreSQL database when it first started.
+If you want to create tables on yourself, set 
`ams.database.auto-create-tables` to false.
+
 
 One thing you need to do is Adding MySQL/PostgreSQL configuration under 
`config.yaml` of Ams:
 
@@ -123,12 +125,14 @@ ams:
     url: 
jdbc:mysql://127.0.0.1:3306/amoro?useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useAffectedRows=true&allowPublicKeyRetrieval=true&useSSL=false
     username: root
     password: root
+    auto-create-tables: true
 # PostgreSQL
 #ams:
 #  database:
 #    type: postgres
 #    jdbc-driver-class: org.postgresql.Driver
 #    url: jdbc:postgresql://127.0.0.1:5432/amoro
+#    auto-create-tables: false
 #    username: user
 #    password: passwd
 ```

Reply via email to