EschedulerManager.java 3.5 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * 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 cn.escheduler.dao.upgrade;

import cn.escheduler.common.utils.SchemaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * upgrade manager
 */
public class EschedulerManager {
    private static final Logger logger = LoggerFactory.getLogger(EschedulerManager.class);
    UpgradeDao upgradeDao = UpgradeDao.getInstance();

    public void initEscheduler() {
L
ligang 已提交
33 34 35 36 37
        // Determines whether the escheduler table structure has been init
        if(upgradeDao.isExistsTable("t_escheduler_version") || upgradeDao.isExistsTable("t_escheduler_queue")) {
            logger.info("The database has been initialized. Skip the initialization step");
            return;
        }
L
ligang 已提交
38 39 40 41 42
        this.initEschedulerSchema();
    }

    public void initEschedulerSchema() {

L
ligang 已提交
43
        logger.info("Start initializing the escheduler manager mysql table structure");
L
ligang 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        upgradeDao.initEschedulerSchema();
    }


    /**
     * upgrade escheduler
     */
    public void upgradeEscheduler() throws Exception{

        // Gets a list of all upgrades
        List<String> schemaList = SchemaUtils.getAllSchemaList();
        if(schemaList == null || schemaList.size() == 0) {
            logger.info("There is no schema to upgrade!");
        }else {

            String version = "";
L
ligang 已提交
60 61 62 63 64 65 66 67 68 69
            // Gets the version of the current system
            if (upgradeDao.isExistsTable("t_escheduler_version")) {
                version = upgradeDao.getCurrentVersion();
            }else if(upgradeDao.isExistsColumn("t_escheduler_queue","create_time")){
                version = "1.0.1";
            }else if(upgradeDao.isExistsTable("t_escheduler_queue")){
                version = "1.0.0";
            }else{
                logger.error("Unable to determine current software version, so cannot upgrade");
            }
L
ligang 已提交
70 71 72
            // The target version of the upgrade
            String schemaVersion = "";
            for(String schemaDir : schemaList) {
L
ligang 已提交
73

L
ligang 已提交
74 75 76 77 78 79 80 81 82

                schemaVersion = schemaDir.split("_")[0];
                if(SchemaUtils.isAGreatVersion(schemaVersion , version)) {

                    logger.info("upgrade escheduler metadata version from " + version + " to " + schemaVersion);


                    logger.info("Begin upgrading escheduler's mysql table structure");
                    upgradeDao.upgradeEscheduler(schemaDir);
L
ligang 已提交
83 84 85 86 87
                    if(SchemaUtils.isAGreatVersion(version,"1.0.1")){
                        version = upgradeDao.getCurrentVersion();
                    }else {
                        version = schemaVersion;
                    }
L
ligang 已提交
88 89 90 91 92 93 94 95 96
                }

            }
        }

        // Assign the value of the version field in the version table to the version of the product
        upgradeDao.updateVersion(SchemaUtils.getSoftVersion());
    }
}