forked from Normation/rudder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2210661
commit 90fb897
Showing
2 changed files
with
232 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
.../src/test/scala/bootstrap/liftweb/checks/migration/TestMigrateEventLogEnforceSchema.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package bootstrap.liftweb.checks.migration | ||
|
||
import com.normation.rudder.db.{DBCommon, Doobie} | ||
import com.normation.zio.UnsafeRun | ||
import doobie.syntax.all._ | ||
import doobie.util.fragment | ||
import doobie.util.fragments.whereAnd | ||
import org.junit.runner.RunWith | ||
import org.specs2.runner.JUnitRunner | ||
import zio.interop.catz._ | ||
|
||
import scala.util.{Failure, Success, Try} | ||
|
||
@RunWith(classOf[JUnitRunner]) | ||
class TestMigrateEventLogEnforceSchema extends DBCommon { | ||
import TestMigrateEventLogEnforceSchema._ | ||
|
||
override def cleanDb(): Unit = { | ||
doobie.transactRunEither( | ||
sql""" | ||
DROP TABLE IF EXISTS $tempTable; | ||
DROP SEQUENCE IF EXISTS eventLogIdSeq_temp; | ||
DROP INDEX IF EXISTS eventType_idx_temp; | ||
DROP INDEX IF EXISTS creationDate_idx_temp; | ||
DROP INDEX IF EXISTS eventlog_fileFormat_idx_temp; | ||
""".update.run.transact(_) | ||
) match { | ||
case Right(_) => () | ||
case Left(ex) => throw ex | ||
} | ||
} | ||
|
||
// The previous schema, with the renamed table for this test | ||
// We add need to know for sure the initial state and the final state of the migrated table, | ||
// so we need to define the previous schema without conflicting with the current one (with all values renamed) | ||
private val previousSchemaDDL = sql""" | ||
CREATE SEQUENCE eventLogIdSeq_temp START 1; | ||
CREATE TABLE $tempTable ( | ||
id integer PRIMARY KEY DEFAULT nextval('eventLogIdSeq_temp') | ||
, creationDate timestamp with time zone NOT NULL DEFAULT 'now' | ||
, severity integer | ||
, causeId integer | ||
, modificationId text | ||
, principal text | ||
, reason text | ||
, eventType text | ||
, data xml | ||
); | ||
CREATE INDEX eventType_idx_temp ON $tempTable (eventType); | ||
CREATE INDEX creationDate_idx_temp ON $tempTable (creationDate); | ||
CREATE INDEX eventlog_fileFormat_idx_temp ON $tempTable (((((xpath('/entry//@fileFormat',data))[1])::text))); | ||
""" | ||
|
||
override def initDb(): Unit = { | ||
super.initDb() | ||
doobie.transactRunEither(previousSchemaDDL.update.run.transact(_)) match { | ||
case Right(_) => () | ||
case Left(ex) => throw ex | ||
} | ||
} | ||
|
||
"MigrateEventLogEnforceSchema" should { | ||
|
||
"migrate all columns successfully" in { | ||
lazy val migrationEventLogRepository = new MigrateEventLogEnforceSchemaTempTable(doobie) | ||
|
||
migrationEventLogRepository.checks() | ||
|
||
doobie.transactRunEither( | ||
(sql""" | ||
SELECT DISTINCT column_name, table_name, is_nullable | ||
FROM INFORMATION_SCHEMA.COLUMNS | ||
""" ++ whereAnd( | ||
fr"table_name = 'eventlog'", | ||
fr"column_name in ('eventtype', 'principal', 'severity', 'data')", | ||
fr"is_nullable = 'YES'" | ||
)) | ||
.query[(String, String, String)] | ||
.option | ||
.transact(_) | ||
) match { | ||
case Right(res) => res must beNone | ||
case Left(ex) => | ||
ko( | ||
s"The migration of 'EventLog' table does not add NOT NULL constraint to all columns with error : ${ex.getMessage}" | ||
) | ||
} | ||
} | ||
|
||
"report an error message when it fails" in { | ||
lazy val migrationEventLogRepository = new MigrateEventLogEnforceSchemaTempTable(doobie, Some(sql"some invalid query")) | ||
|
||
Try(migrationEventLogRepository.migrateAsync.runNow.join.runNow) match { | ||
case Failure(ex) => ex.getMessage must contain("Please contact the development team with the following information to help resolving the issue : ${err.fullMsg}") | ||
case Success(()) => ko("The migration is supposed to throw an error, but it did not") | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
object TestMigrateEventLogEnforceSchema { | ||
// this table is setup and tear down for the lifetime of this test execution | ||
private val tempTable = fragment.Fragment.const("eventlog_migratetest") | ||
|
||
private class MigrateEventLogEnforceSchemaTempTable(doobie: Doobie, overrideStatement: Option[fragment.Fragment] = None) | ||
extends MigrateEventLogEnforceSchema(doobie) { | ||
override def table = tempTable | ||
override def migrationStatement = { | ||
val default = super.migrationStatement | ||
overrideStatement.getOrElse(default) | ||
} | ||
} | ||
|
||
} |