Skip to content

Commit

Permalink
#67 add Commit Metadata repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel Szymczyk committed Jan 19, 2015
1 parent 757cddd commit b0031c8
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 59 deletions.
1 change: 1 addition & 0 deletions javers-persistence-sql/build.gradle
Expand Up @@ -2,6 +2,7 @@ dependencies {
compile project(':javers-core')
compile 'org.polyjdbc:polyjdbc:0.4.0'

testCompile project(path: ":javers-core", configuration: "testArtifacts")
testCompile 'commons-dbcp:commons-dbcp:1.4'
testCompile 'ch.qos.logback:logback-classic:1.1.2'
testCompile 'com.h2database:h2:1.4.184'
Expand Down
@@ -1,29 +1,66 @@
package org.javers.repository.sql.domain;

import org.javers.core.commit.CommitMetadata;
import org.javers.core.json.JsonConverter;
import org.javers.repository.sql.poly.JaversPolyJDBC;
import org.javers.repository.sql.schema.FixedSchemaFactory;
import org.joda.time.LocalDateTime;
import org.polyjdbc.core.query.InsertQuery;
import org.polyjdbc.core.query.SelectQuery;
import org.polyjdbc.core.query.mapper.ObjectMapper;
import org.polyjdbc.core.type.Timestamp;

import java.sql.ResultSet;
import java.sql.SQLException;

import static org.javers.repository.sql.schema.FixedSchemaFactory.COMMIT_TABLE_AUTHOR;

/**
* @author pawel szymczyk
*/
public class CommitRepository {

private JaversPolyJDBC javersPolyjdbc;
private JsonConverter jsonConverter;

public CommitRepository(JaversPolyJDBC javersPolyjdbc, JsonConverter jsonConverter) {
public CommitRepository(JaversPolyJDBC javersPolyjdbc) {
this.javersPolyjdbc = javersPolyjdbc;
this.jsonConverter = jsonConverter;
}

public Long save(CommitMetadata commitMetadata) {
public long save(CommitMetadata commitMetadata) {
Long primaryKey = findCommitMetadata(commitMetadata);

return primaryKey != null ? primaryKey : insert(commitMetadata);
}

private Long findCommitMetadata(CommitMetadata commitMetadata) {
SelectQuery selectQuery = javersPolyjdbc.query()
.select(FixedSchemaFactory.COMMIT_TABLE_PK)
.from(FixedSchemaFactory.COMMIT_TABLE_NAME)
.where(FixedSchemaFactory.COMMIT_TABLE_AUTHOR + " = :author " +
"AND " + FixedSchemaFactory.COMMIT_TABLE_COMMIT_DATE + " = :date " +
"AND " + FixedSchemaFactory.COMMIT_TABLE_COMMIT_ID + " = :id")
.withArgument("author", commitMetadata.getAuthor())
.withArgument("date", toTimestamp(commitMetadata.getCommitDate()))
.withArgument("id", commitMetadata.getId().value());

return javersPolyjdbc.queryRunner().queryUnique(selectQuery, new ObjectMapper<Long>() {
@Override
public Long createObject(ResultSet resultSet) throws SQLException {
return resultSet.getLong(FixedSchemaFactory.COMMIT_TABLE_PK);
}
}, false);
}

private Long insert(CommitMetadata commitMetadata) {
InsertQuery query = javersPolyjdbc.query().insert().into(FixedSchemaFactory.COMMIT_TABLE_NAME)
.value(FixedSchemaFactory.COMMIT_TABLE_AUTHOR, commitMetadata.getAuthor())
.value(FixedSchemaFactory.COMMIT_TABLE_COMMIT_DATE, jsonConverter.toJson(commitMetadata.getCommitDate()))
.value(FixedSchemaFactory.COMMIT_TABLE_COMMIT_ID, commitMetadata.getId().toString());
.value(COMMIT_TABLE_AUTHOR, commitMetadata.getAuthor())
.value(FixedSchemaFactory.COMMIT_TABLE_COMMIT_DATE, toTimestamp(commitMetadata.getCommitDate()))
.value(FixedSchemaFactory.COMMIT_TABLE_COMMIT_ID, commitMetadata.getId().value())
.sequence(FixedSchemaFactory.COMMIT_TABLE_PK, FixedSchemaFactory.COMMIT_TABLE_PK_SEQ);

javersPolyjdbc.queryRunner().insert(query);
return javersPolyjdbc.queryRunner().insert(query);
}

return 1L;
private Timestamp toTimestamp(LocalDateTime commitMetadata) {
return Timestamp.from(commitMetadata.toDate());
}
}
Expand Up @@ -34,6 +34,7 @@ public class FixedSchemaFactory {
public static final String COMMIT_TABLE_AUTHOR = "author";
public static final String COMMIT_TABLE_COMMIT_DATE = "commit_date";
public static final String COMMIT_TABLE_COMMIT_ID = "commit_id";
public static final String COMMIT_TABLE_PK_SEQ = "jv_commit_pk_seq";

public static final String SNAPSHOT_TABLE_NAME = "jv_snapshot";
public static final String SNAPSHOT_TABLE_PK = "snapshot_pk";
Expand Down Expand Up @@ -79,11 +80,7 @@ private Schema commitTableSchema(Dialect dialect, String tableName) {
.withAttribute().timestamp(COMMIT_TABLE_COMMIT_DATE).and()
.withAttribute().string(COMMIT_TABLE_COMMIT_ID).withMaxLength(10).and()
.build();
// return schema;

return schema != null ? schema : new Schema(null);


return schema;
}

private Schema cdoClassTableSchema(Dialect dialect, String tableName) {
Expand Down
Expand Up @@ -3,12 +3,13 @@ package org.javers.repository.sql
import org.h2.jdbcx.JdbcConnectionPool
import org.h2.tools.Server
import org.javers.core.JaversRepositoryE2ETest
import spock.lang.Ignore

import java.sql.Connection

import static org.javers.core.JaversBuilder.javers


@Ignore
class JaversSqlRepositoryTestE2ETest extends JaversRepositoryE2ETest {

@Override
Expand Down
@@ -0,0 +1,43 @@
package org.javers.repository.sql.domain

import org.h2.tools.Server
import org.javers.core.json.JsonConverterBuilder
import org.javers.repository.sql.ConnectionProvider
import org.javers.repository.sql.DialectName
import org.javers.repository.sql.SqlRepositoryTestBuilder
import spock.lang.Shared
import spock.lang.Specification

import java.sql.Connection
import java.sql.DriverManager

/**
* @author pawel szymczyk
*/
class BaseRepositoryTest extends Specification {

@Shared def connectionProvider
@Shared def dbConnection
@Shared def sqlRepoBuilder

def setupSpec() {
Server.createTcpServer().start()
dbConnection = DriverManager.getConnection("jdbc:h2:tcp://localhost:9092/mem:test")
dbConnection.setAutoCommit(false)

connectionProvider = new ConnectionProvider() {
@Override
Connection getConnection() {
return dbConnection
}
}

sqlRepoBuilder = SqlRepositoryTestBuilder.sqlRepository()
.withConnectionProvider(connectionProvider)
.withDialect(DialectName.H2)
.withJSONConverter(JsonConverterBuilder.jsonConverter()
.build())

sqlRepoBuilder.build()
}
}
@@ -1,60 +1,31 @@
package org.javers.repository.sql.domain

import org.javers.core.Javers
import org.javers.core.JaversBuilder
import org.javers.core.commit.Commit
import org.javers.core.commit.CommitId
import org.javers.core.commit.CommitMetadata
import org.javers.core.json.JsonConverterBuilder
import org.javers.core.model.DummyUser
import org.javers.repository.sql.ConnectionProvider
import org.javers.repository.sql.DialectName
import org.javers.repository.sql.SqlRepositoryTestBuilder
import org.joda.time.LocalDateTime
import spock.lang.Specification

import java.sql.Connection
import java.sql.DriverManager
/**
* @author pawel szymczyk
*/
class CommitRepositoryTest extends BaseRepositoryTest {


class CommitRepositoryTest extends Specification {

def "should save commit"() {
def "should save and next find persisted commit metadata"() {

given:
def jsonConverter = JsonConverterBuilder.jsonConverter().build()

def connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/pawel.szymczyk", "pawel.szymczyk", "");
connection.setAutoCommit(false);

def builder = SqlRepositoryTestBuilder.sqlRepository().withConnectionProvider(new ConnectionProvider() {
@Override
Connection getConnection() {
connection
}
}).withDialect(DialectName.POSTGRES).withJSONConverter(jsonConverter)
def commitMetadata = new CommitMetadata("author", LocalDateTime.now(), new CommitId(1L, 0))
def commitRepository = sqlRepoBuilder.getComponent(CommitRepository)

builder.build()
when:
def primaryKey = commitRepository.save(commitMetadata)
dbConnection.commit()

def commitRepository = builder.getComponent(CommitRepository)
then:
primaryKey != null

when:
def id = commitRepository.save(new CommitMetadata("author", LocalDateTime.now(), new CommitId(1L,0)))
connection.commit()
def persistedPrimaryKey = commitRepository.save(commitMetadata)

then:
true

// then:
// globalIdRepository.get(id).id == "kazik"
// globalIdRepository.get(id).class == DummyUser
//
// when:
// def nextId = globalIdRepository.save(globalId)
//
// then:
// nextId == id

persistedPrimaryKey == primaryKey
}
}
Expand Up @@ -7,13 +7,15 @@ import org.javers.core.model.DummyUser
import org.javers.repository.sql.ConnectionProvider
import org.javers.repository.sql.DialectName
import org.javers.repository.sql.SqlRepositoryTestBuilder
import spock.lang.Ignore
import spock.lang.Specification

import java.sql.Connection
import java.sql.DriverManager

class GlobalIdRepositoryTest extends Specification {

@Ignore
def "should select or insert"() {
given:

Expand Down

0 comments on commit b0031c8

Please sign in to comment.