Skip to content

Commit b0acc26

Browse files
committed
CLOUDSTACK-10278: Introduce Flyway migration on CloudStack database
1 parent 4412563 commit b0acc26

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

engine/schema/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,10 @@
4848
<artifactId>cloud-framework-db</artifactId>
4949
<version>${project.version}</version>
5050
</dependency>
51+
<dependency>
52+
<groupId>org.flywaydb</groupId>
53+
<artifactId>flyway-core</artifactId>
54+
<version>5.0.7</version>
55+
</dependency>
5156
</dependencies>
5257
</project>

engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,16 @@
7777
import com.cloud.upgrade.dao.VersionVO.Step;
7878
import com.cloud.utils.component.SystemIntegrityChecker;
7979
import com.cloud.utils.db.GlobalLock;
80+
import com.cloud.utils.db.DbProperties;
8081
import com.cloud.utils.db.ScriptRunner;
8182
import com.cloud.utils.db.TransactionLegacy;
8283
import com.cloud.utils.exception.CloudRuntimeException;
8384
import com.google.common.collect.ImmutableList;
8485
import org.apache.cloudstack.utils.CloudStackVersion;
8586
import org.apache.commons.lang.StringUtils;
8687
import org.apache.log4j.Logger;
88+
import org.flywaydb.core.Flyway;
89+
import org.flywaydb.core.api.FlywayException;
8790

8891
import javax.inject.Inject;
8992
import java.io.IOException;
@@ -96,6 +99,7 @@
9699
import java.util.HashMap;
97100
import java.util.List;
98101
import java.util.Map;
102+
import java.util.Properties;
99103

100104
import static com.google.common.base.Preconditions.checkArgument;
101105
import static com.google.common.collect.Lists.newArrayList;
@@ -631,6 +635,27 @@ public void check() {
631635
} finally {
632636
lock.releaseRef();
633637
}
638+
639+
s_logger.info("Running Flyway migration on Cloudstack database");
640+
Properties dbProps = DbProperties.getDbProperties();
641+
final String cloudUsername = dbProps.getProperty("db.cloud.username");
642+
final String cloudPassword = dbProps.getProperty("db.cloud.password");
643+
final String cloudHost = dbProps.getProperty("db.cloud.host");
644+
final int cloudPort = Integer.parseInt(dbProps.getProperty("db.cloud.port"));
645+
final String dbUrl = "jdbc:mysql://" + cloudHost + ":" + cloudPort + "/cloud";
646+
647+
try {
648+
Flyway flyway = new Flyway();
649+
flyway.setDataSource(dbUrl, cloudUsername, cloudPassword);
650+
flyway.setTable("cloudstack_schema_version");
651+
flyway.setBaselineOnMigrate(true);
652+
flyway.setBaselineVersionAsString("4.11");
653+
flyway.setLocations("db/csmigration");
654+
flyway.migrate();
655+
} catch (FlywayException fwe) {
656+
s_logger.error("Failed to run Flyway migration on Cloudstack database due to " + fwe);
657+
}
658+
634659
}
635660

636661
private static final class NoopDbUpgrade implements DbUpgrade {
@@ -678,4 +703,4 @@ public InputStream[] getCleanupScripts() {
678703
}
679704

680705
}
681-
}
706+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with 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,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+

0 commit comments

Comments
 (0)