Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.convergencelabs.convergence.server.backend.db.schema.DomainSchemaStatePersistence.scala Maven / Gradle / Ivy
/*
* Copyright (c) 2019 - Convergence Labs, Inc.
*
* This file is part of the Convergence Server, which is released under
* the terms of the GNU General Public License version 3 (GPLv3). A copy
* of the GPLv3 should have been provided along with this file, typically
* located in the "LICENSE" file, which is part of this source code package.
* Alternatively, see for the
* full text of the GPLv3 license, if it was not provided.
*/
package com.convergencelabs.convergence.server.backend.db.schema
import com.convergencelabs.convergence.server.backend.datastore.convergence._
import com.convergencelabs.convergence.server.model.DomainId
import com.convergencelabs.convergence.server.model.server.domain.DomainStatus
import java.time.Instant
import scala.util.Try
private[schema] class DomainSchemaStatePersistence(domainId: DomainId,
domainStore: DomainStore,
deltaStore: DomainSchemaDeltaLogStore,
versionStore: DomainSchemaVersionLogStore)
extends SchemaStatePersistence {
override def installedVersion(): Try[Option[String]] = {
domainStore.findDomainDatabase(domainId).map(_.map(_.schemaVersion))
}
override def appliedDeltas(): Try[List[UpgradeDeltaId]] = {
deltaStore.appliedDeltasForDomain(domainId).map(deltas => {
deltas.map(d => UpgradeDeltaId(d.id, d.tag))
})
}
override def recordDeltaSuccess(delta: UpgradeDeltaAndScript, appliedForVersion: String): Try[Unit] = {
val UpgradeDeltaAndScript(deltaId, _, script) = delta
for {
seqNo <- deltaStore.getMaxDeltaSequenceNumber(domainId)
entry = DomainSchemaDeltaLogEntry(domainId, seqNo + 1, deltaId.id, deltaId.tag, script, SchemaDeltaStatus.Success, None, appliedForVersion, Instant.now())
_ <- deltaStore.createDomainDeltaLogEntries(List(entry))
} yield ()
}
override def recordDeltaFailure(delta: UpgradeDeltaAndScript, error: String, appliedForVersion: String): Unit = {
val UpgradeDeltaAndScript(deltaId, _, script) = delta
for {
seqNo <- deltaStore.getMaxDeltaSequenceNumber(domainId)
entry = DomainSchemaDeltaLogEntry(domainId, seqNo + 1, deltaId.id, deltaId.tag, script, SchemaDeltaStatus.Error, Some(error), appliedForVersion, Instant.now())
_ <- deltaStore.createDomainDeltaLogEntries(List(entry))
} yield ()
}
override def recordImplicitDeltasFromInstall(deltaIds: List[UpgradeDeltaId], appliedForVersion: String, installScript: String): Try[Unit] = {
val entries = createEntries(deltaIds, installScript, appliedForVersion)
deltaStore.createDomainDeltaLogEntries(entries)
}
private[this] def createEntries(deltaIds: List[UpgradeDeltaId], installScript: String, appliedForVersion: String): List[DomainSchemaDeltaLogEntry] = {
var curSeqNo = 1
deltaIds.map { deltaId =>
val seqNo = curSeqNo
val script = if (seqNo == 1) installScript else ""
curSeqNo += 1
DomainSchemaDeltaLogEntry(domainId, seqNo, deltaId.id, deltaId.tag, script, SchemaDeltaStatus.Success, None, appliedForVersion, Instant.now())
}
}
override def recordNewVersion(version: String, date: Instant): Try[Unit] = {
val entry = DomainSchemaVersionLogEntry(domainId, version, date)
for {
_ <- versionStore.createDomainSchemaVersionLogEntry(entry)
_ <- domainStore.setDomainSchemaVersion(domainId, version: String)
_ <- domainStore.setDomainStatus(domainId, DomainStatus.Ready, "")
} yield ()
}
/**
* Records that upgrading has started
*
* @return A Success if recording succeeds or a Failure otherwise.
*/
override def recordUpgrading(): Try[Unit] = {
this.domainStore.setDomainStatus(this.domainId, DomainStatus.SchemaUpgrading, "")
}
/**
* Records that upgrading has failed
*
* @return A Success if recording succeeds or a Failure otherwise.
*/
override def recordUpgradeFailure(message: String): Try[Unit] = {
this.domainStore.setDomainStatus(this.domainId, DomainStatus.Error, message)
}
}