/*******************************************************************************
 * Copyright (c) Intel Corporation
 * Copyright (c) 2017
 *
 * 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 org.osc.core.broker.util.db.upgrade;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.io.FileUtils;
import org.osc.core.broker.service.exceptions.VmidcException;
import org.osc.core.broker.util.crypto.KeyStoreProvider.KeyStoreProviderException;
import org.osc.core.broker.util.db.DBConnectionManager;
import org.osc.core.common.job.FreqType;
import org.osc.core.common.job.ThresholdType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
 * Schema: this class will help create our vmiDC database schema and generate
 * DDL file.
 */
public class Schema {

    private static final Logger log = LoggerFactory.getLogger(Schema.class);

    // @formatter:off
    private final static String[] sqlStatements = {

            "create table APPLIANCE (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "manager_software_version varchar(255) not null," +
                "manager_type varchar(255) not null," +
                "model varchar(255) not null," +
                "primary key (id)" +
            ");",

            "create table APPLIANCE_MANAGER_CONNECTOR (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "ip_address varchar(255) not null," +
                "manager_type varchar(255) not null," +
                "name varchar(255) not null," +
                "password varchar(255)," +
                "public_key blob," +
                "service_type varchar(255) not null," +
                "username varchar(255)," +
                "last_job_id_fk bigint," +
                "last_known_broker_ip_address varchar(255)," +
                "primary key (id), " +
                "api_key varchar(255)" +
            ");",

            "create table APPLIANCE_SOFTWARE_VERSION (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "appliance_software_version varchar(255) not null," +
                "image_url varchar(255) not null," +
                "virtualization_software_version varchar(255) not null," +
                "virtualization_type varchar(255) not null," +
                "minimum_cpus int," +
                "memory_in_mb int," +
                "disk_in_gb int," +
                "additional_nic_for_inspection bit not null default 0," +
                "appliance_fk bigint not null," +
                "image_pull_secret_name varchar(255)," +
                "primary key (id)" +
            ");",

            "create table APPLIANCE_SOFTWARE_VERSION_ENCAPSULATION_TYPE_ATTR ("
                    + "appliance_software_version_fk bigint not null, encapsulation_type varchar(255));",

            "create table POD_PORT_IP_ADDRESS ("
                    + "pod_port_fk bigint not null, ip_address varchar(255));",

            "create table VM_PORT_IP_ADDRESS ("
                    + "vm_port_fk bigint not null, ip_address varchar(255));",

            "create table POD_LABEL (" +
                 "pod_fk bigint not null, " +
                 "label_fk bigint not null," +
                 "primary key (pod_fk, label_fk) );",

			"create table DISTRIBUTED_APPLIANCE (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "appliance_version varchar(255) not null," +
                "last_job_id_fk bigint," +
                "name varchar(255) not null," +
                "mgr_secret_key varchar(255)," +
                "appliance_fk bigint not null," +
                "appliance_manager_connector_fk bigint not null," +
                "primary key (id)" +
            ");",

            "create table DISTRIBUTED_APPLIANCE_INSTANCE (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "ip_address varchar(255)," +
                "last_status timestamp," +
                "workload_interfaces bigint," +
                "packets bigint," +
                "discovered boolean," +
                "inspection_ready boolean," +
                "name varchar(255) not null," +
                "mgr_device_id varchar(255)," +
                "current_console_password varchar(255)," +
                "new_console_password varchar(255)," +
                "appliance_config blob," +
                "virtual_system_fk bigint not null," +
                "os_host_name varchar(255)," +
                "os_availability_zone_name varchar(255)," +
                "inspection_os_ingress_port_id varchar(255)," +
                "inspection_ingress_mac_address varchar(255)," +
                "inspection_os_egress_port_id varchar(255)," +
                "inspection_egress_mac_address varchar(255)," +
                "mgmt_os_port_id varchar(255)," +
                "mgmt_mac_address varchar(255)," +
                "floating_ip_id varchar(255)," +
                "mgmt_ip_address varchar(255)," +
                "mgmt_gateway_address varchar(255)," +
                "mgmt_subnet_prefix_length varchar(255)," +
                "deployment_spec_fk bigint," +
                "external_id varchar(255)," +
                "inspection_element_id varchar(255)," +
                "inspection_element_parent_id varchar(255)," +
                "primary key (id)" +
            ");",

            "create table DOMAIN (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "name varchar(255) not null," +
                "mgr_id varchar(255)," +
                "appliance_manager_connector_fk bigint not null," +
                "primary key (id)" +
            ");",

            "create table JOB (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "completed_timestamp timestamp," +
                "name varchar(255) not null," +
                "queued_timestamp timestamp," +
                "started_timestamp timestamp," +
                "state varchar(255)," +
                "status varchar(255)," +
                "submitted_by varchar(255)," +
                "failure_reason varchar(255)," +
                "primary key (id)" +
            ");",

            "create table POLICY (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "name varchar(255) not null," +
                "mgr_policy_id varchar(255) not null," +
                "appliance_manager_connector_fk bigint not null," +
                "domain_fk bigint not null," +
                "primary key (id)" +
            ");",

            "create table RELEASE_INFO (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "build_version varchar(255)," +
                "db_version integer," +
                "primary key (id)" +
            ");",

            "create table SECURITY_GROUP_INTERFACE (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "name varchar(255) not null," +
                "virtual_system_fk bigint not null," +
                "tag varchar(255)," +
                "mgr_interface_id varchar(255)," +
                "mgr_security_group_id varchar(255)," +
                "user_configurable bit not null default 0 ," +
                "failure_policy_type varchar(255) not null," +
                "chain_order bigint not null default 0," +
                "network_elem_id varchar(255)," +
                "security_group_fk bigint," +
                "primary key (id)" +
            ");",


            "create table TASK (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "completed_timestamp timestamp," +
                "dependency_order bigint not null," +
                "fail_reason varchar(1024)," +
                "name varchar(255) not null," +
                "queued_timestamp timestamp," +
                "started_timestamp timestamp," +
                "state varchar(255)," +
                "status varchar(255)," +
                "task_gaurd varchar(255) not null," +
                "job_fk bigint not null," +
                "primary key (id)" +
            ");",

            "create table TASK_PREDECESSOR (" +
                "task_id bigint not null," +
                "predecessor_id bigint not null," +
                "primary key (task_id, predecessor_id)" +
            ");",

            "create table TASK_SUCCESSOR (" +
                "task_id bigint not null," +
                "successor_id bigint not null," +
                "primary key (task_id, successor_id)" +
            ");",

            "create table TASK_CHILD (" +
                "task_id bigint not null," +
                "child_id bigint not null," +
                "primary key (task_id, child_id)" +
            ");",

            "create table TASK_OBJECT (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "task_fk bigint not null," +
                "name varchar(255) not null," +
                "object_type varchar(255) not null," +
                "object_id bigint not null," +
                "primary key (id)" +
            ");",

            "create table JOB_OBJECT (" +
                    "id bigint generated by default as identity," +
                    "created_by varchar(255)," +
                    "created_timestamp timestamp," +
                    "deleted_by varchar(255)," +
                    "deleted_timestamp timestamp," +
                    "marked_for_deletion boolean," +
                    "updated_by varchar(255)," +
                    "updated_timestamp timestamp," +
                    "version bigint," +
                    "job_fk bigint not null," +
                    "name varchar(255) not null," +
                    "object_type varchar(255) not null," +
                    "object_id bigint not null," +
                    "primary key (id)" +
                ");",

            "create table USER (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "email varchar(255)," +
                "first_name varchar(255)," +
                "last_name varchar(255)," +
                "login_name varchar(255) not null," +
                "middle_name varchar(255)," +
                "password varchar(255)," +
                "phone varchar(255)," +
                "role varchar(255)," +
                "primary key (id)" +
            ");",

            "create table VIRTUALIZATION_CONNECTOR (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "name varchar(255) not null," +
                "controller_ip_address varchar(255)," +
                "controller_password varchar(255)," +
                "controller_username varchar(255)," +
                "provider_ip_address varchar(255) not null," +
                "provider_password varchar(255) not null," +
                "provider_username varchar(255) not null," +
                "virtualization_software_version varchar(255)," +
                "virtualization_type varchar(255) not null," +
                "controller_type varchar(255) not null," +
                "admin_project_name varchar(255)," +
                "admin_domain_id varchar(255)," +
                "primary key (id)" +
            ");",

            "create table VIRTUALIZATION_CONNECTOR_PROVIDER_ATTR ("
                    + "vc_fk bigint not null, key varchar(255), value varchar(255));",

            "create table VIRTUAL_SYSTEM (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "name varchar(255) not null," +
                "key_store blob," +
                "mgr_id varchar(255)," +
                "appliance_sw_version_fk bigint not null," +
                "distributed_appliance_fk bigint not null," +
                "domain_fk bigint," +
                "virtualization_connector_fk bigint not null," +
                "encapsulation_type varchar(255)," +
                "primary key (id)" +
            ");",

           "create table SECURITY_GROUP (" +
                   "id bigint generated by default as identity," +
                   "created_by varchar(255)," +
                   "created_timestamp timestamp," +
                   "deleted_by varchar(255)," +
                   "deleted_timestamp timestamp," +
                   "marked_for_deletion boolean," +
                   "updated_by varchar(255)," +
                   "updated_timestamp timestamp," +
                   "version bigint," +
                   "name varchar(255) not null," +
                   "last_job_id_fk bigint," +
                   "network_elem_id varchar(255)," +
                   "vc_fk bigint not null," +
                   "project_id varchar(255)," +
                   "project_name varchar(255)," +
                   "protect_all bit not null default 1," +
                   "sfc_fk bigint, " +
                   "primary key (id)" +
                   ");",

           "create table SECURITY_GROUP_MEMBER (" +
                   "id bigint generated by default as identity," +
                   "created_by varchar(255)," +
                   "created_timestamp timestamp," +
                   "deleted_by varchar(255)," +
                   "deleted_timestamp timestamp," +
                   "marked_for_deletion boolean," +
                   "updated_by varchar(255)," +
                   "updated_timestamp timestamp," +
                   "version bigint," +
                   "security_group_fk bigint not null," +
                   "member_type varchar(255) not null," +
                   "address varchar(255)," +
                   "vm_fk bigint," +
                   "label_fk bigint," +
                   "network_fk bigint," +
                   "subnet_fk bigint," +
                   "primary key (id)" +
                   ");",

           "create table AVAILABILITY_ZONE (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "region varchar(255) not null," +
                "zone varchar(255) not null," +
                "ds_fk bigint not null," +
                "primary key (id)" +
           ");",

           "create table HOST (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "name varchar(255) not null," +
                "openstack_id varchar(255) not null," +
                "ds_host_fk bigint not null," +
                "primary key (id)" +
           ");",

           "create table HOST_AGGREGATE (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "openstack_id varchar(255) not null," +
                "name varchar(255) not null," +
                "ds_fk bigint not null," +
                "primary key (id)" +
                ");",

           "create table DEPLOYMENT_SPEC (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "name varchar(255) not null," +

                "region varchar(255)," +
                "project_name varchar(255)," +
                "project_id varchar(255)," +
                "management_network_name varchar(255)," +
                "management_network_id varchar(255)," +
                "inspection_network_name varchar(255)," +
                "inspection_network_id varchar(255)," +
                "port_group_id varchar(255)," +

                "floating_pool_name varchar(255)," +
                "namespace varchar(255)," +
                "external_id varchar(255)," +
                "last_job_id_fk bigint," +
                "instance_count bigint not null default 1," +
                "shared bit not null default 0," +
                "vs_fk bigint not null," +
                "os_sg_reference_fk bigint," +
                "primary key (id)" +
                ");",

           "create table OS_IMAGE_REFERENCE (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "region varchar(255) not null," +
                "image_ref_id varchar(255)," +
                "vs_fk bigint not null," +
                "asv_fk bigint," +
                "primary key (id)" +
                ");",

           "create table OS_FLAVOR_REFERENCE (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "region varchar(255) not null," +
                "flavor_ref_id varchar(255)," +
                "vs_fk bigint not null," +
                "primary key (id)" +
                ");",

           "create table OS_SECURITY_GROUP_REFERENCE (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "sg_ref_id varchar(255) not null," +
                "sg_ref_name varchar(255) not null," +
                "primary key (id)" +
                ");",

           "create table VM (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "vm_id varchar(255) not null," +
                "region varchar(255) not null," +
                "name varchar(255) not null," +
                "host varchar(255)," +
                "primary key (id)" +
                ");",

            "create table POD (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "external_id varchar(255) not null," +
                "node varchar(255) not null," +
                "name varchar(255) not null," +
                "namespace varchar(255) not null," +
                "primary key (id)" +
                ");",

             "create table LABEL (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "name varchar(255) not null," +
                "value varchar(255) not null," +
                "primary key (id)" +
                ");",

           "create table OS_SUBNET (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "region varchar(255) not null," +
                "subnet_id varchar(255) not null," +
                "name varchar(255) not null," +
                "network_id varchar(255)not null," +
                "protect_external boolean default false,"+
                "primary key (id)" +
                ");",

           "create table VM_PORT (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "os_network_id varchar(255) not null," +
                "os_port_id varchar(255) not null," +
                "mac_address varchar(255) not null," +
                "inspection_hook_id varchar(255)," +
                "vm_fk bigint," +
                "network_fk bigint," +
                "subnet_fk bigint," +
                "parent_id varchar(255)," +
                "primary key (id)" +
                ");",

            "create table POD_PORT (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "external_id varchar(255) not null," +
                "mac_address varchar(255) not null," +
                "pod_fk bigint," +
                "parent_id varchar(255)," +
                "primary key (id)" +
                ");",

            "create table OS_NETWORK (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "region varchar(255) not null," +
                "name varchar(255) not null," +
                "openstack_id varchar(255) not null," +
                "primary key (id)" +
                ");",

            "create table DISTRIBUTED_APPLIANCE_INSTANCE_VM_PORT (" +
                    "dai_fk bigint not null, " +
                    "vm_port_fk bigint not null," +
                    "primary key (dai_fk, vm_port_fk));",

            "create table DISTRIBUTED_APPLIANCE_INSTANCE_POD_PORT (" +
                    "dai_fk bigint not null, " +
                    "pod_port_fk bigint not null," +
                    "primary key (dai_fk, pod_port_fk));",

            "create table JOBS_ARCHIVE (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "auto_schedule boolean not null," +
                "frequency varchar(255) not null," +
                "threshold_unit varchar(255) not null," +
                "threshold_value int not null," +
                "last_trigger_timestamp timestamp," +
                "primary key (id)" +
	            ");",

            "create table EMAIL_SETTINGS (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "mail_server varchar(255) not null," +
                "port int not null," +
                "email_id varchar(255) not null," +
                "password varchar(255)," +
                "primary key (id)" +
	            ");",

            "create table ALARM (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "enable_alarm boolean not null," +
                "name varchar(255) not null," +
                "event_type varchar(255) not null," +
                "regex_match varchar(255)," +
                "severity varchar(255) not null," +
                "action varchar(255) not null," +
                "recipient_email varchar(255)," +
                "primary key (id)" +
	            ");",

            "create table ALERT (" +
                "id bigint generated by default as identity," +
                "created_by varchar(255)," +
                "created_timestamp timestamp," +
                "deleted_by varchar(255)," +
                "deleted_timestamp timestamp," +
                "marked_for_deletion boolean," +
                "updated_by varchar(255)," +
                "updated_timestamp timestamp," +
                "version bigint," +
                "name varchar(255) not null," +
                "object_name varchar(255)," +
                "event_type varchar(255) not null," +
                "object_id bigint," +
                "object_type varchar(255)," +
                "severity varchar(255) not null," +
                "message varchar(255)," +
                "acknowledgement_status varchar(255) not null," +
                "time_acknowledged_timestamp timestamp," +
                "acknowledged_by varchar(255)," +
                "primary key (id)" +
                ");",

		    "create table SERVICE_FUNCTION_CHAIN (" +
    		    "id bigint generated by default as identity," +
		        "created_by varchar(255)," +
		        "created_timestamp timestamp," +
		        "deleted_by varchar(255)," +
		        "deleted_timestamp timestamp," +
		        "marked_for_deletion boolean," +
		        "updated_by varchar(255)," +
		        "updated_timestamp timestamp," +
		        "version bigint," +
		        "vc_fk bigint not null," +
		        "name varchar(255) not null," +
		        "primary key (id)" +
		        ");",

            "create table APPLIANCE_SOFTWARE_VERSION_IMAGE_PROPERTIES (" +
                "asv_fk bigint not null, key varchar(255), value varchar(255));",

            "create table APPLIANCE_SOFTWARE_VERSION_CONFIG_PROPERTIES (" +
                "asv_fk bigint not null, key varchar(255), value varchar(255));",

            "create table SERVICE_FUNCTION_CHAIN_VIRTUAL_SYSTEM (" +
                    "sfc_fk bigint not null, " +
                    "virtual_system_fk bigint not null," +
                    "vs_order bigint not null," +
                    "primary key (sfc_fk, virtual_system_fk));",

            "CREATE TABLE SSL_CERTIFICATE_ATTR (" +
                    "ID BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL," +
                    "CREATED_BY VARCHAR(255)," +
                    "CREATED_TIMESTAMP TIMESTAMP," +
                    "DELETED_BY VARCHAR(255)," +
                    "DELETED_TIMESTAMP TIMESTAMP," +
                    "MARKED_FOR_DELETION BOOLEAN," +
                    "UPDATED_BY VARCHAR(255)," +
                    "UPDATED_TIMESTAMP TIMESTAMP," +
                    "VERSION BIGINT," +
                    "SSL_ALIAS VARCHAR(255) NOT NULL," +
                    "SSL_SHA1 VARCHAR(255) NOT NULL);",

            "CREATE TABLE SSL_CERTIFICATE_ATTR_VIRTUALIZATION_CONNECTOR(" +
                    "VIRTUALIZATION_CONNECTOR_ID BIGINT NOT NULL," +
                    "SSL_CERTIFICATE_ATTR_ID BIGINT NOT NULL," +
                    "CONSTRAINT VC_ID_FK FOREIGN KEY (VIRTUALIZATION_CONNECTOR_ID) REFERENCES VIRTUALIZATION_CONNECTOR (ID) ON UPDATE CASCADE ON DELETE CASCADE," +
                    "CONSTRAINT SSL_CERT_ATTR_VC_FK FOREIGN KEY (SSL_CERTIFICATE_ATTR_ID) REFERENCES SSL_CERTIFICATE_ATTR (ID) ON UPDATE CASCADE ON DELETE CASCADE);",

            "CREATE TABLE SSL_CERTIFICATE_ATTR_APPL_MAN_CONNECTOR(" +
                    "APPLIANCE_MANAGER_CONNECTOR_ID BIGINT NOT NULL," +
                    "SSL_CERTIFICATE_ATTR_ID BIGINT NOT NULL," +
                    "CONSTRAINT AMC_ID_FK FOREIGN KEY (APPLIANCE_MANAGER_CONNECTOR_ID) REFERENCES APPLIANCE_MANAGER_CONNECTOR (ID) ON UPDATE CASCADE ON DELETE CASCADE," +
                    "CONSTRAINT SSL_CERT_ATTR_AMC_FK FOREIGN KEY (SSL_CERTIFICATE_ATTR_ID) REFERENCES SSL_CERTIFICATE_ATTR (ID) ON UPDATE CASCADE ON DELETE CASCADE);",


            "create table SECURITY_GROUP_INTERFACE_POLICY (" +
                    "sgi_fk bigint not null, " +
                    "policy_fk bigint not null," +
                    "primary key (sgi_fk, policy_fk));",

            "alter table APPLIANCE " +
                "add constraint UK_AP_MODEL unique (model);",

            "alter table APPLIANCE_MANAGER_CONNECTOR " +
                "add constraint UK_MC_IP_ADDR unique (ip_address);",

            "alter table APPLIANCE_MANAGER_CONNECTOR " +
                "add constraint UK_MC_NAME unique (name);",

            "alter table APPLIANCE_MANAGER_CONNECTOR " +
                 "add constraint FK_MC_LAST_JOB " +
                 "foreign key (last_job_id_fk) " +
                 "references JOB(id) ON DELETE SET NULL;",

            "alter table APPLIANCE_MANAGER_CONNECTOR " +
                 "add constraint FK_MC_LAST_JOB_UNIQUE unique (last_job_id_fk);",

            "alter table APPLIANCE_SOFTWARE_VERSION " +
                "add constraint UK_AV_AP_SV_VT_VSV unique (" +
                 "appliance_fk, appliance_software_version, virtualization_type, virtualization_software_version);",

            "alter table APPLIANCE_SOFTWARE_VERSION " +
                 "add constraint UK_ASV_IMAGE_URL unique (image_url);",

            "alter table APPLIANCE_SOFTWARE_VERSION " +
                 "add constraint FK_AV_APPLIANCE " +
                 "foreign key (appliance_fk) " +
                 "references APPLIANCE;",

            "alter table APPLIANCE_SOFTWARE_VERSION_ENCAPSULATION_TYPE_ATTR add constraint " +
                 "FK_ASV_ASV_ENCAPSULATION foreign key (appliance_software_version_fk) references APPLIANCE_SOFTWARE_VERSION;",

            "alter table APPLIANCE_SOFTWARE_VERSION_IMAGE_PROPERTIES add constraint " +
                 "FK_ASV_IMAGE_PROPERTY foreign key (asv_fk) references APPLIANCE_SOFTWARE_VERSION;",

            "alter table APPLIANCE_SOFTWARE_VERSION_IMAGE_PROPERTIES add constraint " +
                 "UK_ASV_IMAGE_PROPERTY_KEY unique (asv_fk, key);",

            "alter table APPLIANCE_SOFTWARE_VERSION_CONFIG_PROPERTIES add constraint " +
                 "FK_ASV_CONFIG_PROPERTY foreign key (asv_fk) references APPLIANCE_SOFTWARE_VERSION;",

            "alter table APPLIANCE_SOFTWARE_VERSION_CONFIG_PROPERTIES add constraint " +
                 "UK_ASV_CONFIG_PROPERTY_KEY unique (asv_fk, key);",

            "alter table VM_PORT_IP_ADDRESS add constraint " +
                 "FK_VM_PORT_IP_ADDRESS foreign key (vm_port_fk) references VM_PORT;",

            "alter table POD_PORT_IP_ADDRESS add constraint " +
                 "FK_POD_PORT_IP_ADDRESS foreign key (pod_port_fk) references POD_PORT;",

            "alter table DISTRIBUTED_APPLIANCE " +
                "add constraint UK_DA_NAME unique (name);",

            "alter table DISTRIBUTED_APPLIANCE " +
                 "add constraint FK_DA_LAST_JOB " +
                 "foreign key (last_job_id_fk) " +
                 "references JOB(id) ON DELETE SET NULL;",

            "alter table DISTRIBUTED_APPLIANCE " +
                 "add constraint FK_DA_LAST_JOB_UNIQUE unique (last_job_id_fk);",

            "alter table DISTRIBUTED_APPLIANCE_INSTANCE " +
                "add constraint UK_DAI_NAME unique (name);",

            "alter table RELEASE_INFO " +
                "add constraint UK_RLS_BV_DBV unique (build_version, db_version);",

            "alter table USER " +
                "add constraint UK_USR_LOGIN unique (login_name);",

            "alter table VIRTUALIZATION_CONNECTOR " +
                "add constraint UK_VC_NAME unique (name);",

            "alter table VIRTUALIZATION_CONNECTOR " +
                "add constraint UK_VC_PROVIDER_IP_ADDR unique (provider_ip_address);",

            "alter table VIRTUALIZATION_CONNECTOR_PROVIDER_ATTR add constraint " +
                "FK_PROVIDER_ATTR_VC foreign key (vc_fk) references VIRTUALIZATION_CONNECTOR;",

            "alter table VIRTUALIZATION_CONNECTOR_PROVIDER_ATTR add constraint " +
                "UK_PROVIDER_ATTR_KEY unique (vc_fk, key);",

            "alter table VIRTUAL_SYSTEM " +
                "add constraint UK_VS_VC_DA unique (virtualization_connector_fk, distributed_appliance_fk);",

            "alter table DISTRIBUTED_APPLIANCE " +
                "add constraint FK_DA_APPLIANCE " +
                "foreign key (appliance_fk) " +
                "references APPLIANCE;",

            "alter table DISTRIBUTED_APPLIANCE " +
                "add constraint FK_DA_APPLIANCE_MANAGER_CONNECTOR " +
                "foreign key (appliance_manager_connector_fk) " +
                "references APPLIANCE_MANAGER_CONNECTOR;",

            "alter table DISTRIBUTED_APPLIANCE_INSTANCE " +
                "add constraint FK_DAI_VIRTUAL_SYSTEM " +
                "foreign key (virtual_system_fk) " +
                "references VIRTUAL_SYSTEM;",

            "alter table DISTRIBUTED_APPLIANCE_INSTANCE " +
                "add constraint FK_DAI_DEPLOYMENT_SPEC " +
                "foreign key (deployment_spec_fk) " +
                "references DEPLOYMENT_SPEC;",

            "alter table DOMAIN " +
                "add constraint FK_DO_APPLIANCE_MANAGER_CONNECTOR " +
                "foreign key (appliance_manager_connector_fk) " +
                "references APPLIANCE_MANAGER_CONNECTOR;",

            "alter table POLICY " +
                "add constraint FK_PO_APPLIANCE_MANAGER_CONNECTOR " +
                "foreign key (appliance_manager_connector_fk) " +
                "references APPLIANCE_MANAGER_CONNECTOR;",

            "alter table POLICY " +
                "add constraint FK_PO_DOMAIN " +
                "foreign key (domain_fk) " +
                "references DOMAIN;",

            "alter table SECURITY_GROUP_INTERFACE " +
                "add constraint FK_SG_VIRTUAL_SYSTEM " +
                "foreign key (virtual_system_fk) " +
                "references VIRTUAL_SYSTEM;",

            "alter table SECURITY_GROUP_INTERFACE " +
                    "add constraint FK_SECURITY_GROUP " +
                    "foreign key (security_group_fk) " +
                    "references SECURITY_GROUP;",

            "alter table TASK " +
                "add constraint FK_TASK_JOB " +
                "foreign key (job_fk) " +
                "references JOB;",

            "alter table TASK_PREDECESSOR " +
                "add constraint FK_TASK_PRED_ID " +
                "foreign key (predecessor_id) " +
                "references TASK;",

            "alter table TASK_PREDECESSOR " +
                "add constraint FK_TASK_PRED_TASK_ID " +
                "foreign key (task_id) " +
                "references TASK;",

            "alter table TASK_SUCCESSOR " +
                "add constraint FK_TASK_SUCC_TASK " +
                "foreign key (successor_id) " +
                "references TASK;",

            "alter table TASK_SUCCESSOR " +
                "add constraint FK_TASK_SUCC_TASK_ID " +
                "foreign key (task_id) " +
                "references TASK;",

            "alter table TASK_CHILD " +
                "add constraint FK_TASK_CHILD_ID " +
                "foreign key (child_id) " +
                "references TASK;",

            "alter table TASK_CHILD " +
                "add constraint FK_TASK_CHILD_TASK_ID " +
                "foreign key (task_id) " +
                "references TASK;",

            "alter table TASK_OBJECT " +
                "add constraint FK_TASK_OBJECT_TASK " +
                "foreign key (task_fk) " +
                "references TASK;",

            "alter table TASK_OBJECT " +
                "add constraint UK_TASK_OBJ_TYPE_OBJ_ID " +
                "unique (task_fk, object_type, object_id);",

            "alter table JOB_OBJECT " +
                "add constraint FK_JOB_OBJECT_JOB " +
                "foreign key (job_fk) " +
                "references JOB;",

            "alter table JOB_OBJECT " +
                "add constraint UK_JOB_OBJ_TYPE_OBJ_ID " +
                "unique (job_fk, object_type, object_id);",

            "alter table VIRTUAL_SYSTEM " +
                "add constraint FK_VS_APPLIANCE_SW_VERSION " +
                "foreign key (appliance_sw_version_fk) " +
                "references APPLIANCE_SOFTWARE_VERSION;",

            "alter table VIRTUAL_SYSTEM " +
                "add constraint FK_VS_DISTRIBUTED_APPLIANCE " +
                "foreign key (distributed_appliance_fk) " +
                "references DISTRIBUTED_APPLIANCE;",

            "alter table VIRTUAL_SYSTEM " +
                "add constraint FK_AG_DOMAIN " +
                "foreign key (domain_fk) " +
                "references DOMAIN;",

            "alter table VIRTUAL_SYSTEM " +
                "add constraint FK_VS_VIRTUALIZATION_CONNECTOR " +
                "foreign key (virtualization_connector_fk) " +
                "references VIRTUALIZATION_CONNECTOR;",

            "alter table SECURITY_GROUP " +
                "add constraint UK_NAME_PROJECT unique (" +
                "name, project_id);",

            "alter table SECURITY_GROUP " +
                "add constraint FK_SG_VC " +
                "foreign key (vc_fk) " +
                "references VIRTUALIZATION_CONNECTOR;",

            "alter table SECURITY_GROUP " +
                "add constraint FK_SG_LAST_JOB " +
                "foreign key (last_job_id_fk) " +
                "references JOB(id) ON DELETE SET NULL;",

			"alter table SECURITY_GROUP " +
			    "add constraint FK_SG_SFC " +
			    "foreign key (sfc_fk) " +
			    "references SERVICE_FUNCTION_CHAIN;",

            "alter table SECURITY_GROUP_MEMBER " +
                "add constraint FK_SGM_SG " +
                "foreign key (security_group_fk) " +
                "references SECURITY_GROUP;",

            "alter table SECURITY_GROUP_MEMBER " +
                "add constraint UK_SGM_SG_VM_NETWORK_ADDRESS " +
                "unique (security_group_fk, vm_fk, network_fk, address);",

            "alter table SECURITY_GROUP_MEMBER " +
                "add constraint FK_SGM_VM " +
                "foreign key (vm_fk) " +
                "references VM;",

            "alter table SECURITY_GROUP_MEMBER " +
                "add constraint FK_SGM_LABEL " +
                "foreign key (label_fk) " +
                "references LABEL;",

            "alter table SECURITY_GROUP_MEMBER " +
                "add constraint FK_SGM_NETWORK " +
                "foreign key (network_fk) " +
                "references OS_NETWORK;",

            "alter table SECURITY_GROUP_MEMBER " +
                "add constraint FK_SGM_SUBNET " +
                "foreign key (subnet_fk) " +
                "references OS_SUBNET;",

            "alter table OS_FLAVOR_REFERENCE " +
                "add constraint UK_FLAVOR_OSID unique (flavor_ref_id);",

            "alter table OS_FLAVOR_REFERENCE " +
                "add constraint FK_OS_FLAVOR_REFERENCE_VS " +
                "foreign key (vs_fk) " +
                "references VIRTUAL_SYSTEM;",

           "alter table OS_IMAGE_REFERENCE " +
                "add constraint UK_IMAGE_OSID unique (image_ref_id);",

           "alter table OS_IMAGE_REFERENCE " +
                "add constraint FK_VS_OS_IMAGE_REFERENCE " +
                "foreign key (vs_fk) " +
                "references VIRTUAL_SYSTEM;",

           "alter table OS_IMAGE_REFERENCE " +
                   "add constraint FK_ASV_OS_IMAGE_REFERENCE " +
                   "foreign key (asv_fk) " +
                   "references APPLIANCE_SOFTWARE_VERSION;",

           "alter table OS_SECURITY_GROUP_REFERENCE " +
                   "add constraint UK_OSSG_OSID unique (sg_ref_id);",

           "alter table DEPLOYMENT_SPEC " +
                   "add constraint FK_DS_OS_SG_REFERENCE " +
                   "foreign key (os_sg_reference_fk) " +
                   "references OS_SECURITY_GROUP_REFERENCE;",

           "alter table DEPLOYMENT_SPEC " +
                "add constraint UK_VS_PROJECT_REGION unique (" +
                "vs_fk, project_id, region);",

            "alter table DEPLOYMENT_SPEC " +
                "add constraint FK_DEPLOYMENT_SPEC_VS " +
                "foreign key (vs_fk) " +
                "references VIRTUAL_SYSTEM(id);" +

            "alter table DEPLOYMENT_SPEC " +
                "add constraint FK_DS_LAST_JOB " +
                "foreign key (last_job_id_fk) " +
                "references JOB(id) ON DELETE SET NULL;",

            "alter table AVAILABILITY_ZONE " +
                "add constraint FK_DS " +
                "foreign key (ds_fk) " +
                "references DEPLOYMENT_SPEC(id);",

            "alter table HOST " +
                "add constraint FK_HOST_DS " +
                "foreign key (ds_host_fk) " +
                "references DEPLOYMENT_SPEC(id);",

            "alter table HOST " +
                "add constraint UK_HOST_ID unique (" +
                "ds_host_fk, openstack_id);",

            "alter table HOST_AGGREGATE " +
                "add constraint FK_HOST_AGGREGATE_DS " +
                "foreign key (ds_fk) " +
                "references DEPLOYMENT_SPEC(id);",

            "alter table VM " +
                "add constraint UK_VM_ID unique (" +
                "vm_id);",

            "alter table POD " +
                "add constraint UK_POD_EXT_ID unique (" +
                "external_id);",

            "alter table VM_PORT " +
                "add constraint FK_VMP_VM " +
                "foreign key (vm_fk) " +
                "references VM;",

            "alter table VM_PORT " +
                "add constraint FK_VMP_NETWORK " +
                "foreign key (network_fk) " +
                "references OS_NETWORK;",

            "alter table VM_PORT " +
                "add constraint FK_VMP_SUBNET " +
                "foreign key (subnet_fk) " +
                "references OS_SUBNET;",

            "alter table VM_PORT " +
                "add constraint UK_VM_PORT_MAC unique (" +
                "mac_address);",

            "alter table VM_PORT " +
                "add constraint UK_VMP_ID " +
                "unique (os_port_id);",

            "alter table OS_NETWORK " +
                "add constraint UK_OSNETWORK_OSID " +
                "unique (openstack_id);",

            "alter table OS_SUBNET " +
                "add constraint UK_SUBNET_ID " +
                "unique (subnet_id);",

            "alter table DISTRIBUTED_APPLIANCE_INSTANCE_VM_PORT " +
                    "add constraint FK_DAI_VMP_DAI " +
                    "foreign key (dai_fk) " +
                    "references DISTRIBUTED_APPLIANCE_INSTANCE;",

            "alter table DISTRIBUTED_APPLIANCE_INSTANCE_VM_PORT " +
                "add constraint FK_DAI_VMP_VMP " +
                "foreign key (vm_port_fk) " +
                "references VM_PORT;",

            "alter table DISTRIBUTED_APPLIANCE_INSTANCE_POD_PORT " +
                    "add constraint FK_DAI_PODP_DAI " +
                    "foreign key (dai_fk) " +
                    "references DISTRIBUTED_APPLIANCE_INSTANCE;",

            "alter table DISTRIBUTED_APPLIANCE_INSTANCE_POD_PORT " +
                "add constraint FK_DAI_PODP_PODP " +
                "foreign key (pod_port_fk) " +
                "references POD_PORT;",

            "alter table SERVICE_FUNCTION_CHAIN_VIRTUAL_SYSTEM " +
                    "add constraint FK_SFC_VS_SFC " +
                    "foreign key (sfc_fk) " +
                    "references SERVICE_FUNCTION_CHAIN;",

            "alter table SERVICE_FUNCTION_CHAIN_VIRTUAL_SYSTEM " +
                    "add constraint FK_SFC_VS_VS " +
                    "foreign key (virtual_system_fk) " +
                    "references VIRTUAL_SYSTEM;",

            "alter table SERVICE_FUNCTION_CHAIN " +
                    "add constraint UK_SFC_NAME unique (name);",

			"alter table SERVICE_FUNCTION_CHAIN " +
					"add constraint FK_SFC_VC " +
					"foreign key (vc_fk) " +
					"references VIRTUALIZATION_CONNECTOR;",

            "alter table SECURITY_GROUP_INTERFACE_POLICY " +
                    "add constraint FK_SGI_POLICY_SGI " +
                    "foreign key (sgi_fk) " +
                    "references SECURITY_GROUP_INTERFACE;",

            "alter table SECURITY_GROUP_INTERFACE_POLICY " +
                "add constraint FK_SGI_POLICY_POLICY " +
                "foreign key (policy_fk) " +
                "references POLICY;",

        }; // end of schema array
    // @formatter:on

//    public static void generateDdlFile(Configuration configuration, String outputFile) {
//
//        SchemaExport export = new SchemaExport(configuration);
//
//        export.setOutputFile(outputFile);
//        export.setDelimiter(";");
//        export.execute(true, false, false, true);
//    }

    @SuppressFBWarnings(value = "SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE")
    public static void createSchema(DBConnectionManager mgr) throws SQLException, KeyStoreProviderException, IOException, InterruptedException, VmidcException {
        try (Connection connection = mgr.getSQLConnection();
             Statement stmt = connection.createStatement()) {

        	connection.setAutoCommit(false);

            try {
                for (String statement : sqlStatements) {
                    log.info("Execute sql: " + statement);
                    stmt.execute(statement);
                }

                // Stamp db version
                stmt.execute("INSERT INTO release_info (id, version, db_version) VALUES (1, 0, " + ReleaseUpgradeMgr.TARGET_DB_VERSION + ");");

                // Add job archiving default settings - auto scheduled disabled, run weekly, purge jobs older then 1 year
                stmt.execute("INSERT INTO JOBS_ARCHIVE (id, version, auto_schedule, frequency, threshold_value, threshold_unit) VALUES "
                        + "(1, 0, 0, '" + FreqType.WEEKLY + "', 1, '" + ThresholdType.YEARS + "');");

                connection.commit();
            } catch (Exception ex) {
                log.error("Error while creating schema.", ex);

                try {
                    connection.rollback();
                } catch (Exception he) {
                    log.error("Error rolling back transaction", he);
                }

                // delete the newly created database to ensure we don't attempt to startup with a corrupted state
                try {
                    FileUtils.forceDeleteOnExit(new File("vmiDCDB.h2.db"));
                } catch (IOException e) {
                    log.error("Error deleting newly created database.", e);
                }

                throw ex;
            }
        }
    }
}
