|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.submarine.server.database.initialization; |
| 19 | + |
| 20 | +import org.apache.submarine.commons.utils.SubmarineConfVars; |
| 21 | +import org.apache.submarine.commons.utils.SubmarineConfiguration; |
| 22 | +import org.flywaydb.core.Flyway; |
| 23 | +import org.flywaydb.core.api.configuration.FluentConfiguration; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +public class DatabaseMetadataService { |
| 28 | + |
| 29 | + private static final Logger LOG = LoggerFactory.getLogger(DatabaseMetadataService.class); |
| 30 | + |
| 31 | + /** |
| 32 | + * init database metadata by flyway |
| 33 | + */ |
| 34 | + public void initDatabaseMetadata() { |
| 35 | + SubmarineConfiguration conf = SubmarineConfiguration.getInstance(); |
| 36 | + if (!conf.getBoolean(SubmarineConfVars.ConfVars.SUBMARINE_METADATA_INIT)) { |
| 37 | + LOG.info("Skip submarine metadata initialization. If you want to init database metadata, " + |
| 38 | + "you can set `submarine.metadata.init` or SUBMARINE_METADATA_INIT env to true."); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // get database connection conf |
| 43 | + String jdbcUrl = conf.getJdbcUrl(); |
| 44 | + String jdbcUserName = conf.getJdbcUserName(); |
| 45 | + String jdbcPassword = conf.getJdbcPassword(); |
| 46 | + |
| 47 | + // flyway config |
| 48 | + FluentConfiguration fluentConfiguration = new FluentConfiguration() |
| 49 | + .dataSource(jdbcUrl, jdbcUserName, jdbcPassword); |
| 50 | + |
| 51 | + // sql files location |
| 52 | + fluentConfiguration.locations(conf.getString(SubmarineConfVars.ConfVars.SUBMARINE_METADATA_LOCATION)); |
| 53 | + // schema metadata history table, control schema version. version id is default release version |
| 54 | + fluentConfiguration.table("submarine_schema_history"); |
| 55 | + // if schema metadata history table is missed,try to create it |
| 56 | + fluentConfiguration.baselineOnMigrate(true); |
| 57 | + // Whether to automatically call validate or not when running migrate. |
| 58 | + // If encounter script tuning, we can set this value to false |
| 59 | + fluentConfiguration.validateOnMigrate( |
| 60 | + conf.getBoolean(SubmarineConfVars.ConfVars.SUBMARINE_METADATA_VALIDATE)); |
| 61 | + // basic version, we may start it from 0.7.0 |
| 62 | + // We can replace this value if we want to update from an intermediate version in some cases |
| 63 | + fluentConfiguration.baselineVersion( |
| 64 | + conf.getString(SubmarineConfVars.ConfVars.SUBMARINE_METADATA_VERSION)); |
| 65 | + |
| 66 | + Flyway flyway = fluentConfiguration.load(); |
| 67 | + try { |
| 68 | + flyway.migrate(); |
| 69 | + } catch (Exception e) { |
| 70 | + LOG.warn("Error during database initialization. You may need to manually initialize " + |
| 71 | + "the actual metadata and data.", e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments