Skip to content

Commit

Permalink
Merge branch 'arch_24147/add_score_enum_for_score_in_database_pr' int…
Browse files Browse the repository at this point in the history
…o branches/rudder/8.1
  • Loading branch information
Jenkins CI committed Feb 7, 2024
2 parents e1414f2 + 117bfb8 commit 0e30fe5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,20 @@ CREATE TABLE NodeFacts (
);


CREATE TYPE score AS enum ('A', 'B', 'C', 'D', 'E')

Create table GlobalScore (
nodeId text primary key
, score text
, message text
, details jsonb
, score score NOT NULL
, message text NOT NULL
, details jsonb NOT NULL
);

Create table scoreDetails (
nodeId text
, scoreId text
, score text
, message text
, details jsonb
nodeId text NOT NULL
, scoreId text NOT NULL
, score score NOT NULL
, message text NOT NULL
, details jsonb NOT NULL
, PRIMARY KEY (nodeId, scoreId)
);
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import com.normation.inventory.domain.NodeId
import com.normation.rudder.db.Doobie
import doobie.implicits._
import doobie.implicits.toSqlInterpolator
import doobie.postgres.implicits.pgEnumString
import doobie.util.invariant.InvalidEnum
import zio.interop.catz._

trait GlobalScoreRepository {
Expand All @@ -56,8 +58,17 @@ object GlobalScoreRepositoryImpl {
import ScoreSerializer._
import com.normation.rudder.db.json.implicits._
import doobie._
implicit val getScoreValue: Get[ScoreValue] = Get[String].temap(ScoreValue.fromString)
implicit val putScoreValue: Put[ScoreValue] = Put[String].contramap(_.value)

implicit val scoreMeta: Meta[ScoreValue] = {
def getValue(value: String) = {
ScoreValue.fromString(value) match {
case Right(s) => s
case Left(err) => throw InvalidEnum[ScoreValue](value)
}
}

pgEnumString("score", getValue, _.value)
}

implicit val stateWrite: Meta[List[NoDetailsScore]] = new Meta(pgDecoderGet, pgEncoderPut)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ import com.normation.errors.IOResult
import com.normation.inventory.domain.NodeId
import com.normation.rudder.db.Doobie
import doobie.Fragments
import doobie.Get
import doobie.Put
import doobie.Meta
import doobie.Read
import doobie.Write
import doobie.implicits._
import doobie.implicits.toSqlInterpolator
import doobie.postgres.implicits.pgEnumString
import doobie.util.invariant.InvalidEnum
import zio.interop.catz._
import zio.json.ast.Json

Expand All @@ -63,8 +64,16 @@ trait ScoreRepository {
class ScoreRepositoryImpl(doobie: Doobie) extends ScoreRepository {

import com.normation.rudder.db.json.implicits._
implicit val getScoreValue: Get[ScoreValue] = Get[String].temap(ScoreValue.fromString)
implicit val putScoreValue: Put[ScoreValue] = Put[String].contramap(_.value)

implicit val scoreMeta: Meta[ScoreValue] = {
def getValue(value: String) = {
ScoreValue.fromString(value) match {
case Right(s) => s
case Left(err) => throw InvalidEnum[ScoreValue](value)
}
}
pgEnumString("score", getValue, _.value)
}

// implicit val stateWrite: Meta[Score] = new Meta(pgDecoderGet, pgEncoderPut)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.normation.rudder.db.Doobie
import com.normation.zio._
import doobie.implicits._
import zio.interop.catz._
import zio.syntax.ToZio

/*
* During 8.1 cycle, we added a score that is applied to every nodes to give a better understanding
Expand All @@ -53,25 +54,31 @@ class CheckTableScore(

import doobie._

override def description: String = "Check if database tables Users and UserSessions exist"
override def description: String = "Check if score databases exist"

def createScoreTables: IOResult[Unit] = {
val sql1 = sql"""CREATE TABLE IF NOT EXISTS GlobalScore (

val checkType = sql"SELECT 1 FROM pg_type WHERE typname = 'score'"
val sqlType = sql"""CREATE TYPE score AS enum ('A', 'B', 'C', 'D', 'E')"""
val sql1 = sql"""CREATE TABLE IF NOT EXISTS GlobalScore (
nodeId text primary key
, score text NOT NULL CHECK (score <> '')
, score score NOT NULL CHECK (score <> '')
, message text NOT NULL CHECK (message <> '')
, details jsonb NOT NULL
);"""

val sql2 = sql"""CREATE TABLE IF NOT EXISTS ScoreDetails (
nodeId text
, scoreId text
, score text NOT NULL CHECK (score <> '')
, message text NOT NULL CHECK (score <> '')
, score score NOT NULL CHECK (score <> '')
, message text NOT NULL CHECK (message <> '')
, details jsonb NOT NULL
, PRIMARY KEY (nodeId, scoreId)
);"""

transactIOResult(s"Error with 'score' type creation")(xa =>
checkType.query[Int].option.transact(xa).flatMap(i => if (i.isDefined) ().succeed else sqlType.update.run.transact(xa))
).unit *>
transactIOResult(s"Error with 'GlobalScore' table creation")(xa => sql1.update.run.transact(xa)).unit *>
transactIOResult(s"Error with 'ScoreDetails' table creation")(xa => sql2.update.run.transact(xa)).unit
}
Expand Down

0 comments on commit 0e30fe5

Please sign in to comment.