Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support in memory and postgres #2

Merged
merged 7 commits into from Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions Procfile
@@ -1,3 +1 @@
web: java -jar target/elide-heroku-example.jar

release: java -jar target/dependency/liquibase.jar --changeLogFile=src/main/resources/db/changelog/changelog.xml --url=$JDBC_DATABASE_URL --classpath=target/dependency/postgres.jar update
29 changes: 6 additions & 23 deletions pom.xml
Expand Up @@ -25,6 +25,12 @@
<groupId>com.yahoo.elide</groupId>
<artifactId>elide-standalone</artifactId>
<version>${elide.version}</version>
<exclusions>
<exclusion>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.yahoo.elide</groupId>
Expand Down Expand Up @@ -55,7 +61,6 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>test</scope>
</dependency>

<!-- Test -->
Expand All @@ -77,7 +82,6 @@
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down Expand Up @@ -129,27 +133,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
<destFileName>liquibase.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgres.version}</version>
<destFileName>postgres.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
<execution>
<phase>process-sources</phase>
<goals>
Expand Down
31 changes: 9 additions & 22 deletions src/main/java/example/Main.java
Expand Up @@ -9,34 +9,21 @@

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Properties;

/**
* Example app using Elide library.
*/
@Slf4j
public class Main {
public static void main(String[] args) throws Exception {
ElideStandalone elide = new ElideStandalone(new Settings() {
public Properties getDatabaseProperties() {

Properties dbProps;
try {
dbProps = new Properties();
dbProps.load(
Main.class.getClassLoader().getResourceAsStream("dbconfig.properties")
);

dbProps.setProperty("javax.persistence.jdbc.url", System.getenv("JDBC_DATABASE_URL"));
dbProps.setProperty("javax.persistence.jdbc.user", System.getenv("JDBC_DATABASE_USERNAME"));
dbProps.setProperty("javax.persistence.jdbc.password", System.getenv("JDBC_DATABASE_PASSWORD"));
return dbProps;
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
});

//If JDBC_DATABASE_URL is not set, we'll run with H2 in memory.
boolean inMemory = (System.getenv("JDBC_DATABASE_URL") == null);

Settings settings = new Settings(inMemory) {};

ElideStandalone elide = new ElideStandalone(settings);

settings.runLiquibaseMigrations();

elide.start();
}
Expand Down
68 changes: 63 additions & 5 deletions src/main/java/example/Settings.java
Expand Up @@ -21,12 +21,18 @@
import example.models.ArtifactVersion;
import io.swagger.models.Info;
import io.swagger.models.Swagger;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.ClassLoaderResourceAccessor;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.glassfish.hk2.api.ServiceLocator;

import javax.persistence.EntityManagerFactory;
import java.io.IOException;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -38,12 +44,31 @@
*/
public abstract class Settings implements ElideStandaloneSettings {

protected String jdbcUrl;
protected String jdbcUser;
protected String jdbcPassword;

protected boolean inMemory;

public Settings(boolean inMemory) {
jdbcUrl = Optional.ofNullable(System.getenv("JDBC_DATABASE_URL"))
.orElse("jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE");

jdbcUser = Optional.ofNullable(System.getenv("JDBC_DATABASE_USERNAME"))
.orElse("sa");

jdbcPassword = Optional.ofNullable(System.getenv("JDBC_DATABASE_PASSWORD"))
.orElse("");

this.inMemory = inMemory;
}

@Override
public int getPort() {
//Heroku exports port to come from $PORT
return Optional.ofNullable(System.getenv("PORT"))
.map(Integer::valueOf)
.orElse(4080);
.orElse(8080);
}

@Override
Expand Down Expand Up @@ -96,17 +121,21 @@ public ElideSettings getElideSettings(ServiceLocator injector) {
}

public Properties getDatabaseProperties() {

Properties dbProps;

if (inMemory) {
return getInMemoryProps();
}

try {
dbProps = new Properties();
dbProps.load(
Main.class.getClassLoader().getResourceAsStream("dbconfig.properties")
);

dbProps.setProperty("javax.persistence.jdbc.url", System.getenv("JDBC_DATABASE_URL"));
dbProps.setProperty("javax.persistence.jdbc.user", System.getenv("JDBC_DATABASE_USERNAME"));
dbProps.setProperty("javax.persistence.jdbc.password", System.getenv("JDBC_DATABASE_PASSWORD"));
dbProps.setProperty("javax.persistence.jdbc.url", jdbcUrl);
dbProps.setProperty("javax.persistence.jdbc.user", jdbcUser);
dbProps.setProperty("javax.persistence.jdbc.password", jdbcPassword);
return dbProps;
} catch (IOException e) {
throw new IllegalStateException(e);
Expand All @@ -126,4 +155,33 @@ public void updateServletContextHandler(ServletContextHandler servletContextHand
throw new IllegalStateException(e);
}
}

protected Properties getInMemoryProps() {
Properties options = new Properties();

options.put("hibernate.show_sql", "true");
options.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
options.put("hibernate.current_session_context_class", "thread");
options.put("hibernate.jdbc.use_scrollable_resultset", "true");

options.put("javax.persistence.jdbc.driver", "org.h2.Driver");
options.put("javax.persistence.jdbc.url", jdbcUrl);
options.put("javax.persistence.jdbc.user", jdbcUser);
options.put("javax.persistence.jdbc.password", jdbcPassword);

return options;
}

public void runLiquibaseMigrations() throws Exception {
//Run Liquibase Initialization Script
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(
new JdbcConnection(DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword)));

Liquibase liquibase = new liquibase.Liquibase(
"db/changelog/changelog.xml",
new ClassLoaderResourceAccessor(),
database);

liquibase.update("db1");
}
}
51 changes: 4 additions & 47 deletions src/test/java/example/IntegrationTest.java
@@ -1,24 +1,13 @@
/*
* Copyright 2019, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package example;

import com.yahoo.elide.standalone.ElideStandalone;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.ClassLoaderResourceAccessor;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.TestInstance;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
* Base class for running a set of functional Elide tests. This class
Expand All @@ -28,54 +17,22 @@
public class IntegrationTest {
private ElideStandalone elide;

protected static final String JDBC_URL = "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE";
protected static final String JDBC_USER = "sa";
protected static final String JDBC_PASSWORD = "";

@BeforeAll
public void init() throws Exception {
elide = new ElideStandalone(new Settings() {
Settings settings = new Settings(true) {
@Override
public int getPort() {
return 8080;
}
};

@Override
public Properties getDatabaseProperties() {
Properties options = new Properties();

options.put("hibernate.show_sql", "true");
options.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
options.put("hibernate.current_session_context_class", "thread");
options.put("hibernate.jdbc.use_scrollable_resultset", "true");

options.put("javax.persistence.jdbc.driver", "org.h2.Driver");
options.put("javax.persistence.jdbc.url", JDBC_URL);
options.put("javax.persistence.jdbc.user", JDBC_USER);
options.put("javax.persistence.jdbc.password", JDBC_PASSWORD);
elide = new ElideStandalone(settings);

return options;
}
});

//Run Liquibase Initialization Script
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(
new JdbcConnection(getConnection()));

Liquibase liquibase = new liquibase.Liquibase(
"db/changelog/changelog.xml",
new ClassLoaderResourceAccessor(),
database);

liquibase.update("db1");
settings.runLiquibaseMigrations();

elide.start(false);
}

protected Connection getConnection() throws SQLException {
return DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
}

@AfterAll
public void shutdown() throws Exception {
elide.stop();
Expand Down