Skip to content
This repository was archived by the owner on Feb 22, 2021. It is now read-only.

Commit

Permalink
Add DB migrations support
Browse files Browse the repository at this point in the history
  • Loading branch information
asafalima committed May 16, 2017
1 parent e18bfd6 commit 0df0fb8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 46 deletions.
18 changes: 18 additions & 0 deletions apollo-backend/create-migration
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

if [ "$#" == "0" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
echo "Create new migration file"
echo "USAGE: $0 <Name of migration>"
echo "EXAMPLE: $0 Create users table"
exit 0
fi

version=$(date +%Y%m%d%H%M%S)
name=$(echo $@ | tr -s ' ' | tr ' ' '_' | python -c "print raw_input().capitalize()")

filename="V${version}__${name}.sql"
path="src/main/resources/db/migration/${filename}"

touch ${path}
git add ${path}
32 changes: 7 additions & 25 deletions apollo-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,6 @@
</execution>
</executions>
</plugin>
<!--
<plugin>
<groupId>com.spotify</deploymentGroupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>registry.internal.logz.io:5000/elasticsearch-benchmark-tool</imageName>
<dockerDirectory>docker</dockerDirectory>
<resources>
<resource>
<targetPath>/packages</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}-jar-with-dependencies.jar</include>
</resource>
</resources>
</configuration>
</plugin>
-->
</plugins>
</build>
<dependencies>
Expand All @@ -77,11 +59,6 @@
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -90,7 +67,7 @@
<dependency>
<groupId>io.logz.logback</groupId>
<artifactId>logzio-logback-appender</artifactId>
<version>1.0.11</version>
<version>1.0.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
Expand Down Expand Up @@ -142,6 +119,11 @@
<artifactId>github-api</artifactId>
<version>1.84</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>4.2.0</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand All @@ -153,7 +135,7 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.1.7</version>
<version>1.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.flywaydb.core.Flyway;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -46,6 +47,8 @@ private ApolloMyBatis(ApolloConfiguration configuration) {
try {
logger.info("Creating MyBatis instance");
DataSource dataSource = new ApolloDataSource(configuration).getDataSource();
migrateDatabase(dataSource);

TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("apollo", transactionFactory, dataSource);

Expand All @@ -72,4 +75,11 @@ public static ApolloMyBatisSession getSession() {

return new ApolloMyBatisSession(sqlSessionFactory);
}

private void migrateDatabase(DataSource dataSource) {
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
flyway.migrate();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`user_email` varchar(100) NOT NULL,
`first_name` varchar(100) NOT NULL,
Expand All @@ -9,7 +8,6 @@ CREATE TABLE `users` (
UNIQUE KEY `user_email` (`user_email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `environment`;
CREATE TABLE `environment` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(1000) NOT NULL,
Expand All @@ -23,7 +21,6 @@ CREATE TABLE `environment` (
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `service`;
CREATE TABLE `service` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(1000) NOT NULL,
Expand All @@ -33,7 +30,6 @@ CREATE TABLE `service` (
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `deployable_version`;
CREATE TABLE `deployable_version` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`git_commit_sha` varchar(1000) NOT NULL,
Expand All @@ -49,7 +45,6 @@ CREATE TABLE `deployable_version` (
CONSTRAINT `deployable_version_service_fk` FOREIGN KEY (`service_id`) REFERENCES `service` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `deployment`;
CREATE TABLE `deployment` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`environment_id` int(11) unsigned NOT NULL,
Expand All @@ -67,7 +62,6 @@ CREATE TABLE `deployment` (
CONSTRAINT `deployment_user_fk` FOREIGN KEY (`user_email`) REFERENCES `users` (`user_email`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `deployment_permissions`;
CREATE TABLE `deployment_permissions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(1000) NOT NULL,
Expand All @@ -80,14 +74,12 @@ CREATE TABLE `deployment_permissions` (
CONSTRAINT `deployment_permission_environment_fk` FOREIGN KEY (`environment_id`) REFERENCES `environment` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `deployment_groups`;
CREATE TABLE `deployment_groups` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(1000) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `deployment_user_groups`;
CREATE TABLE `deployment_user_groups` (
`user_email` varchar(1000) NOT NULL,
`deployment_group_id` int(11) unsigned NOT NULL,
Expand All @@ -96,11 +88,10 @@ CREATE TABLE `deployment_user_groups` (
CONSTRAINT `deployment_user_groups_deployment_groups_id_fk` FOREIGN KEY (`deployment_group_id`) REFERENCES `deployment_groups` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `deployment_group_permissions`;
CREATE TABLE `deployment_group_permissions` (
`deployment_group_id` int(11) unsigned NOT NULL,
`deployment_permission_id` int(11) unsigned NOT NULL,
UNIQUE KEY (`deployment_group_id`, `deployment_permission_id`),
CONSTRAINT `deployment_group_permissions_deployment_groups_id_fk` FOREIGN KEY (`deployment_group_id`) REFERENCES `deployment_groups` (`id`),
CONSTRAINT `deployment_group_permissions_deployment_permissions_id_fk` FOREIGN KEY (`deployment_permission_id`) REFERENCES `deployment_permissions` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package io.logz.apollo.helpers;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import io.logz.apollo.configuration.ApolloConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.jdbc.ext.ScriptUtils;

import javax.script.ScriptException;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

/**
Expand All @@ -26,12 +20,8 @@ public ApolloMySQL() throws SQLException, IOException, ScriptException {

// Create mysql instance
logger.info("Starting MySQL container");
mysql = new MySQLContainer("mysql:5.7");
mysql = new MySQLContainer("mysql:5.7.18");
mysql.start();

logger.info("Creating MySQL connection and creating the schema");
Connection connection = mysql.createConnection("");
ScriptUtils.executeSqlScript(connection, "/" , Files.toString(new File("apollo-schema.sql"), Charsets.UTF_8));
}

public String getContainerIpAddress() {
Expand Down

0 comments on commit 0df0fb8

Please sign in to comment.