/*
 * Copyright 2017 ThoughtWorks, Inc.
 *
 * Licensed 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 com.thoughtworks.go.server.database;

import com.thoughtworks.go.util.SystemEnvironment;
import net.sf.dbdeploy.InMemory;
import net.sf.dbdeploy.database.syntax.HsqlDbmsSyntax;
import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.File;
import java.sql.SQLException;

import static com.thoughtworks.go.util.ExceptionUtils.bomb;
import static com.thoughtworks.go.util.ExceptionUtils.bombUnless;

public class DbDeployMigration implements Migration {
    private static final Logger LOG = LoggerFactory.getLogger(DbDeployMigration.class);

    private final BasicDataSource dataSource;
    private SystemEnvironment env;

    public DbDeployMigration(BasicDataSource dataSource, SystemEnvironment env) {
        this.dataSource = dataSource;
        this.env = env;
    }

    public void migrate() throws SQLException {
        upgradeWithDbDeploy();
    }

    private void upgradeWithDbDeploy() {
        LOG.info("Upgrading database at {}. This might take a while depending on the size of the database.", dataSource);
        File upgradePath = env.getDBDeltasPath();
        bombUnless(upgradePath.exists(), "Database upgrade scripts do not exist in directory " + upgradePath.getAbsolutePath());
        InMemory dbDeploy = new InMemory(dataSource, new HsqlDbmsSyntax(), upgradePath, "DDL");
        try {
            String migrationSql = dbDeploy.migrationSql();
            new JdbcTemplate(dataSource).execute(migrationSql);
        } catch (Exception e) {
            String message = "Unable to create database upgrade script for database " + dataSource.getUrl() + ". The problem was: " + e.getMessage();
            if (e.getCause() != null) {
                message += ". The cause was: " + e.getCause().getMessage();
            }
            LOG.error(message, e);
            throw bomb(message, e);
        }
        LOG.info("Database upgraded");
    }


}

@@ -829,16 +829,60 @@
 Source;%0A
+import org.apache.commons.lang.StringUtils;%0A
 import o
@@ -1032,16 +1032,64 @@
 ception;
+%0Aimport java.util.Arrays;%0Aimport java.util.List;
 %0A%0Aimport
@@ -1830,24 +1830,459 @@
 ataSource);%0A
+%0A        List%3CString%3E messages = Arrays.asList(%0A                StringUtils.repeat(%22*%22, %22%22, 72),%0A                %22WARNING: Shutting down your server at this point will lead to a database corruption. Please wait until the database upgrade completes.%22,%0A                StringUtils.repeat(%22*%22, %22%22, 72)%0A        );%0A        for (String message : messages) %7B%0A            System.err.println(message);%0A            LOG.info(message);%0A        %7D%0A%0A
         File
@@ -2681,16 +2681,147 @@
 onSql);%0A
+%0A            System.err.println(%22Database upgrade completed successfully.%22);%0A            LOG.info(%22Database upgrade completed.%22);%0A%0A
         
@@ -3175,26 +3175,34 @@
     
-throw bomb
+System.err.println
 (message
 , e)
@@ -3189,35 +3189,32 @@
 .println(message
-, e
 );%0A        %7D%0A   
@@ -3212,48 +3212,88 @@
     
-%7D%0A
     
-    LOG.info(%22Database upgraded%22);
+e.printStackTrace(System.err);%0A            throw bomb(message, e);%0A        %7D
 %0A   

Improve logging and messaging around migrations