<commit_before>/*                                                                           *\
*    ____                   ____  ___ __                                      *
*   / __ \____  ___  ____  / __ \/ (_) /_____  _____                          *
*  / / / / __ \/ _ \/ __ \/ / / / / / __/ __ \/ ___/   OpenOlitor             *
* / /_/ / /_/ /  __/ / / / /_/ / / / /_/ /_/ / /       contributed by tegonal *
* \____/ .___/\___/_/ /_/\____/_/_/\__/\____/_/        http://openolitor.ch   *
*     /_/                                                                     *
*                                                                             *
* This program is free software: you can redistribute it and/or modify it     *
* under the terms of the GNU General Public License as published by           *
* the Free Software Foundation, either version 3 of the License,              *
* or (at your option) any later version.                                      *
*                                                                             *
* This program is distributed in the hope that it will be useful, but         *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for *
* more details.                                                               *
*                                                                             *
* You should have received a copy of the GNU General Public License along     *
* with this program. If not, see http://www.gnu.org/licenses/                 *
*                                                                             *
\*                                                                           */
package ch.openolitor.core.db.evolution

import scalikejdbc._
import scalikejdbc.SQLSyntax._
import ch.openolitor.core.repositories.CoreDBMappings
import scala.util._
import ch.openolitor.core.models._
import com.typesafe.scalalogging.LazyLogging
import ch.openolitor.core.db.evolution.scripts.V1Scripts
import ch.openolitor.util.IteratorUtil
import org.joda.time.DateTime
import ch.openolitor.core.repositories.BaseEntitySQLSyntaxSupport
import ch.openolitor.stammdaten.StammdatenDBMappings
import ch.openolitor.stammdaten.models._
import ch.openolitor.core.repositories.SqlBinder
import scala.reflect._

trait Script {
  def execute(implicit session: DBSession): Try[Boolean]
}

case class EvolutionException(msg: String) extends Exception

object Evolution extends Evolution(V1Scripts.scripts)

/**
 * Base evolution class to evolve database from a specific revision to another
 */
class Evolution(scripts: Seq[Script]) extends CoreDBMappings with LazyLogging with StammdatenDBMappings {
  import IteratorUtil._

  logger.debug(s"Evolution manager consists of:$scripts")

  def checkDBSeeds(seeds: Map[Class[_ <: BaseId], Long])(implicit cpContext: ConnectionPoolContext, userId: UserId): Try[Map[Class[_ <: BaseId], Long]] = {
    DB readOnly { implicit session =>
      try {
        val dbIds = Seq(
          adjustSeed[Abotyp, AbotypId](seeds, abotypMapping),
          adjustSeed[Depot, DepotId](seeds, depotMapping),
          adjustSeeds[VertriebsartId](
            seeds,
            maxId[Depotlieferung, VertriebsartId](depotlieferungMapping),
            maxId[Heimlieferung, VertriebsartId](heimlieferungMapping),
            maxId[Postlieferung, VertriebsartId](postlieferungMapping)
          ),
          adjustSeeds[AboId](
            seeds,
            maxId[DepotlieferungAbo, AboId](depotlieferungAboMapping),
            maxId[HeimlieferungAbo, AboId](heimlieferungAboMapping),
            maxId[PostlieferungAbo, AboId](postlieferungAboMapping)
          ),
          adjustSeed[Kunde, KundeId](seeds, kundeMapping),
          adjustSeed[CustomKundentyp, CustomKundentypId](seeds, customKundentypMapping),
          adjustSeed[Lieferung, LieferungId](seeds, lieferungMapping),
          adjustSeed[Pendenz, PendenzId](seeds, pendenzMapping),
          adjustSeed[Person, PersonId](seeds, personMapping),
          adjustSeed[Produzent, ProduzentId](seeds, produzentMapping),
          adjustSeed[Produkt, ProduktId](seeds, produktMapping),
          adjustSeed[ProduktProduktekategorie, ProduktProduktekategorieId](seeds, produktProduktekategorieMapping),
          adjustSeed[ProduktProduzent, ProduktProduzentId](seeds, produktProduzentMapping),
          adjustSeed[Produktekategorie, ProduktekategorieId](seeds, produktekategorieMapping),
          adjustSeed[Projekt, ProjektId](seeds, projektMapping),
          adjustSeed[Tour, TourId](seeds, tourMapping),
          adjustSeed[Lieferplanung, LieferplanungId](seeds, lieferplanungMapping),
          adjustSeed[Lieferposition, LieferpositionId](seeds, lieferpositionMapping),
          adjustSeed[Bestellposition, BestellpositionId](seeds, bestellpositionMapping),
          adjustSeed[Abwesenheit, AbwesenheitId](seeds, abwesenheitMapping)
        ).flatten

        Success(seeds ++ dbIds.toMap)
      } catch {<commit_msg>OO-156: added db seed checks<commit_after>/*                                                                           *\
*    ____                   ____  ___ __                                      *
*   / __ \____  ___  ____  / __ \/ (_) /_____  _____                          *
*  / / / / __ \/ _ \/ __ \/ / / / / / __/ __ \/ ___/   OpenOlitor             *
* / /_/ / /_/ /  __/ / / / /_/ / / / /_/ /_/ / /       contributed by tegonal *
* \____/ .___/\___/_/ /_/\____/_/_/\__/\____/_/        http://openolitor.ch   *
*     /_/                                                                     *
*                                                                             *
* This program is free software: you can redistribute it and/or modify it     *
* under the terms of the GNU General Public License as published by           *
* the Free Software Foundation, either version 3 of the License,              *
* or (at your option) any later version.                                      *
*                                                                             *
* This program is distributed in the hope that it will be useful, but         *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for *
* more details.                                                               *
*                                                                             *
* You should have received a copy of the GNU General Public License along     *
* with this program. If not, see http://www.gnu.org/licenses/                 *
*                                                                             *
\*                                                                           */
package ch.openolitor.core.db.evolution

import scalikejdbc._
import scalikejdbc.SQLSyntax._
import ch.openolitor.core.repositories.CoreDBMappings
import scala.util._
import ch.openolitor.core.models._
import com.typesafe.scalalogging.LazyLogging
import ch.openolitor.core.db.evolution.scripts.V1Scripts
import ch.openolitor.util.IteratorUtil
import org.joda.time.DateTime
import ch.openolitor.core.repositories.BaseEntitySQLSyntaxSupport
import ch.openolitor.stammdaten.StammdatenDBMappings
import ch.openolitor.stammdaten.models._
import ch.openolitor.buchhaltung.models._
import ch.openolitor.core.repositories.SqlBinder
import scala.reflect._
import ch.openolitor.buchhaltung.BuchhaltungDBMappings

trait Script {
  def execute(implicit session: DBSession): Try[Boolean]
}

case class EvolutionException(msg: String) extends Exception

object Evolution extends Evolution(V1Scripts.scripts)

/**
 * Base evolution class to evolve database from a specific revision to another
 */
class Evolution(scripts: Seq[Script]) extends CoreDBMappings with LazyLogging with StammdatenDBMappings with BuchhaltungDBMappings {
  import IteratorUtil._

  logger.debug(s"Evolution manager consists of:$scripts")

  def checkDBSeeds(seeds: Map[Class[_ <: BaseId], Long])(implicit cpContext: ConnectionPoolContext, userId: UserId): Try[Map[Class[_ <: BaseId], Long]] = {
    DB readOnly { implicit session =>
      try {
        val dbIds = Seq(
          adjustSeed[Abotyp, AbotypId](seeds, abotypMapping),
          adjustSeed[Depot, DepotId](seeds, depotMapping),
          adjustSeeds[VertriebsartId](
            seeds,
            maxId[Depotlieferung, VertriebsartId](depotlieferungMapping),
            maxId[Heimlieferung, VertriebsartId](heimlieferungMapping),
            maxId[Postlieferung, VertriebsartId](postlieferungMapping)
          ),
          adjustSeeds[AboId](
            seeds,
            maxId[DepotlieferungAbo, AboId](depotlieferungAboMapping),
            maxId[HeimlieferungAbo, AboId](heimlieferungAboMapping),
            maxId[PostlieferungAbo, AboId](postlieferungAboMapping)
          ),
          adjustSeed[Kunde, KundeId](seeds, kundeMapping),
          adjustSeed[CustomKundentyp, CustomKundentypId](seeds, customKundentypMapping),
          adjustSeed[Lieferung, LieferungId](seeds, lieferungMapping),
          adjustSeed[Pendenz, PendenzId](seeds, pendenzMapping),
          adjustSeed[Person, PersonId](seeds, personMapping),
          adjustSeed[Produzent, ProduzentId](seeds, produzentMapping),
          adjustSeed[Produkt, ProduktId](seeds, produktMapping),
          adjustSeed[ProduktProduktekategorie, ProduktProduktekategorieId](seeds, produktProduktekategorieMapping),
          adjustSeed[ProduktProduzent, ProduktProduzentId](seeds, produktProduzentMapping),
          adjustSeed[Produktekategorie, ProduktekategorieId](seeds, produktekategorieMapping),
          adjustSeed[Projekt, ProjektId](seeds, projektMapping),
          adjustSeed[Tour, TourId](seeds, tourMapping),
          adjustSeed[Lieferplanung, LieferplanungId](seeds, lieferplanungMapping),
          adjustSeed[Lieferposition, LieferpositionId](seeds, lieferpositionMapping),
          adjustSeed[Bestellposition, BestellpositionId](seeds, bestellpositionMapping),
          adjustSeed[Abwesenheit, AbwesenheitId](seeds, abwesenheitMapping),
          adjustSeed[Rechnung, RechnungId](seeds, rechnungMapping),
          adjustSeed[ZahlungsImport, ZahlungsImportId](seeds, zahlungsImportMapping),
          adjustSeed[ZahlungsEingang, ZahlungsEingangId](seeds, zahlungsEingangMapping)
        ).flatten

        Success(seeds ++ dbIds.toMap)
      } catch {
{'fraction_non_alphanumeric': 0.08625592417061612, 'fraction_numerical': 0.0008530805687203792, 'mean_word_length': 2.044143104443162, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6959880', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2878, 'n_words': 565}