Skip to content

Commit

Permalink
Add Mongo Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
vknaisl committed Nov 14, 2019
1 parent c6216ba commit bb55a73
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 96 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
dist: xenial
language: generic

services:
- mongodb

jdk:
- openjdk11

Expand Down
10 changes: 6 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<java.version>11</java.version>

<!-- Core -->
<mongobee.version>0.13</mongobee.version>
<jwt.version>0.10.5</jwt.version>
<sshj.version>0.27.0</sshj.version>
<jinja.version>2.0.5</jinja.version>
Expand Down Expand Up @@ -47,10 +48,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.data</groupId>-->
<!-- <artifactId>spring-data-mongodb</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
Expand All @@ -59,6 +56,11 @@
<!-- ////////////////// -->
<!-- Core -->
<!-- ////////////////// -->
<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>${mongobee.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
Expand Down
21 changes: 1 addition & 20 deletions src/main/java/nl/dtls/adminpanel/AdminPanelApplication.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
package nl.dtls.adminpanel;

import static org.springframework.core.env.Profiles.of;

import nl.dtls.adminpanel.database.fixtures.DummyDataLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;

@SpringBootApplication
public class AdminPanelApplication implements ApplicationRunner {

@Autowired
private DummyDataLoader dummyDataLoader;

@Autowired
private Environment environment;
public class AdminPanelApplication {

public static void main(String[] args) {
String property = System.getProperties().getProperty("spring.profiles.active");
Expand All @@ -27,10 +14,4 @@ public static void main(String[] args) {
SpringApplication.run(AdminPanelApplication.class, args);
}

@Override
public void run(ApplicationArguments args) {
if (environment.acceptsProfiles(of(Profiles.DEVELOPMENT))) {
dummyDataLoader.load();
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/nl/dtls/adminpanel/Profiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class Profiles {

public static final String TESTING = "testing";

public static final String NON_TESTING = "!testing";

public static final String NON_PRODUCTION = "!production";

}
18 changes: 18 additions & 0 deletions src/main/java/nl/dtls/adminpanel/config/MongoConfig.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package nl.dtls.adminpanel.config;

import com.github.mongobee.Mongobee;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;

@Configuration
public class MongoConfig {

@Value("${spring.data.mongodb.uri}")
private String mongoUri;

@Autowired
private Environment environment;

@Autowired
private MappingMongoConverter mongoConverter;

Expand All @@ -34,4 +43,13 @@ public GridFSBucket getGridFSBuckets(MongoDbFactory mongoDbFactory) {
return GridFSBuckets.create(db);
}

@Bean
public Mongobee mongobee() throws Exception {
Mongobee runner = new Mongobee(mongoUri);
runner.setChangeLogsScanPackage("nl.dtls.adminpanel");
runner.setSpringEnvironment(environment);
runner.execute();
return runner;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package nl.dtls.adminpanel.database.migration.development;

import javax.annotation.PostConstruct;
import nl.dtls.adminpanel.Profiles;
import nl.dtls.adminpanel.database.migration.development.application.ApplicationMigration;
import nl.dtls.adminpanel.database.migration.development.instance.InstanceMigration;
import nl.dtls.adminpanel.database.migration.development.server.ServerMigration;
import nl.dtls.adminpanel.database.migration.development.user.UserMigration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Service
@Profile(Profiles.NON_PRODUCTION)
public class MigrationRunner {

@Autowired
private UserMigration userMigration;

@Autowired
private ApplicationMigration applicationMigration;

@Autowired
private ServerMigration serverMigration;

@Autowired
private InstanceMigration instanceMigration;

@PostConstruct
public void run() {
userMigration.runMigration();
applicationMigration.runMigration();
serverMigration.runMigration();
instanceMigration.runMigration();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nl.dtls.adminpanel.database.migration.development.application;

import nl.dtls.adminpanel.database.migration.development.application.data.ApplicationFixtures;
import nl.dtls.adminpanel.database.migration.development.common.Migration;
import nl.dtls.adminpanel.database.repository.ApplicationRepository;
import nl.dtls.adminpanel.entity.Application;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApplicationMigration implements Migration {

@Autowired
private ApplicationFixtures applicationFixtures;

@Autowired
private ApplicationRepository applicationRepository;

public void runMigration() {
applicationRepository.deleteAll();
Application fdpApplication = applicationFixtures.fdpApplication();
applicationRepository.save(fdpApplication);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nl.dtls.adminpanel.database.fixtures.data;
package nl.dtls.adminpanel.database.migration.development.application.data;

import java.util.ArrayList;
import nl.dtls.adminpanel.entity.Application;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nl.dtls.adminpanel.database.migration.development.common;

public interface Migration {

void runMigration();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nl.dtls.adminpanel.database.migration.development.instance;

import nl.dtls.adminpanel.database.migration.development.common.Migration;
import nl.dtls.adminpanel.database.migration.development.instance.data.InstanceFixtures;
import nl.dtls.adminpanel.database.repository.InstanceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class InstanceMigration implements Migration {

@Autowired
private InstanceFixtures instanceFixtures;

@Autowired
private InstanceRepository instanceRepository;

public void runMigration() {
instanceRepository.deleteAll();
instanceRepository.save(instanceFixtures.fdpStaging());
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package nl.dtls.adminpanel.database.fixtures.data;
package nl.dtls.adminpanel.database.migration.development.instance.data;

import java.util.HashMap;
import nl.dtls.adminpanel.database.migration.development.application.data.ApplicationFixtures;
import nl.dtls.adminpanel.database.migration.development.server.data.ServerFixtures;
import nl.dtls.adminpanel.database.repository.ApplicationRepository;
import nl.dtls.adminpanel.database.repository.ServerRepository;
import nl.dtls.adminpanel.entity.Application;
import nl.dtls.adminpanel.entity.Instance;
import nl.dtls.adminpanel.entity.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

Expand All @@ -16,8 +21,22 @@ public class InstanceFixtures {
@Value("${dummy.instance.jwtSecret:}")
private String instanceJwtSecret;

public Instance stagingFdpInstance(Application fdpApplication,
Server fdpServer) {
@Autowired
private ApplicationFixtures applicationFixtures;

@Autowired
private ServerFixtures serverFixtures;

@Autowired
private ApplicationRepository applicationRepository;

@Autowired
private ServerRepository serverRepository;

public Instance fdpStaging() {
Application fdpApplication = applicationRepository
.findByUuid(applicationFixtures.fdpApplication().getUuid()).get();
Server fdpServer = serverRepository.findByUuid(serverFixtures.fdpServer().getUuid()).get();
return new Instance(
"6f29daa8-1c43-49c3-9d1b-dc422e333d1e",
"FDP Staging Instance",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nl.dtls.adminpanel.database.migration.development.server;

import nl.dtls.adminpanel.database.migration.development.common.Migration;
import nl.dtls.adminpanel.database.migration.development.server.data.ServerFixtures;
import nl.dtls.adminpanel.database.repository.ServerRepository;
import nl.dtls.adminpanel.entity.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ServerMigration implements Migration {

@Autowired
private ServerFixtures serverFixtures;

@Autowired
private ServerRepository serverRepository;

public void runMigration() {
serverRepository.deleteAll();
Server fdpServer = serverFixtures.fdpServer();
serverRepository.save(fdpServer);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nl.dtls.adminpanel.database.fixtures.data;
package nl.dtls.adminpanel.database.migration.development.server.data;

import nl.dtls.adminpanel.entity.Server;
import org.springframework.beans.factory.annotation.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nl.dtls.adminpanel.database.migration.development.user;

import nl.dtls.adminpanel.database.migration.development.common.Migration;
import nl.dtls.adminpanel.database.migration.development.user.data.UserFixtures;
import nl.dtls.adminpanel.database.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserMigration implements Migration {

@Autowired
private UserRepository userRepository;

@Autowired
private UserFixtures userFixtures;

public void runMigration() {
userRepository.deleteAll();
userRepository.save(userFixtures.albert());
userRepository.save(userFixtures.nikola());
}

}
Loading

0 comments on commit bb55a73

Please sign in to comment.