<commit_before>package services.database

import models.database.Statement
import models.database.queries.ddl.DdlQueries
import models.database.queries.ddl._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import utils.Logging

import scala.concurrent.Future

object Schema extends Logging {
  val tables = Seq(
    "users" -> CreateUsersTable,

    "user_profiles" -> CreateUserProfilesTable,
    "password_info" -> CreatePasswordInfoTable,
    "oauth1_info" -> CreateOAuth1InfoTable,
    "oauth2_info" -> CreateOAuth2InfoTable,
    "openid_info" -> CreateOpenIdInfoTable,
    "session_info" -> CreateSessionInfoTable,

    "requests" -> CreateRequestsTable,
    "client_trace" -> CreateClientTraceTable,

    "user_feedback" -> CreateUserFeedbackTable,
    "user_feedback_notes" -> CreateUserFeedbackNotesTable,

    "daily_metrics" -> CreateDailyMetricsTable,

    "adhoc_queries" -> CreateAdHocQueriesTable
  )

  def update() = {
    Future.sequence(tables.map { t =>
      Database.query(DdlQueries.DoesTableExist(t._1)).flatMap { exists =>
        if (exists) {
          Future.successful(Unit)
        } else {
          log.info(s"Creating missing table [${t._1}].")
          val name = s"CreateTable-${t._1}"
          Database.raw(name, t._2.sql).map(x => Unit)
        }
      }
    }).flatMap { ok =>
      createUser(Database.query(DdlQueries.DoesTestUserExist), DdlQueries.InsertTestUser)
    }
  }

  def wipe() = {
    log.warn("Wiping database schema.")
    val tableNames = tables.reverse.map(_._1)
    Database.execute(DdlQueries.TruncateTables(tableNames)).map(x => tableNames)
  }

  private[this] def createUser(q: Future[Boolean], insert: Statement) = q.flatMap { exists =>
    if (exists) {
      Future.successful(Unit)
    } else {
      log.info(s"Creating user [${insert.getClass.getSimpleName}].")
      Database.execute(insert).map(x => x == 1)
    }
  }
}
<commit_msg>Resolving table creation race condition.<commit_after>package services.database

import models.database.Statement
import models.database.queries.ddl.DdlQueries
import models.database.queries.ddl._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import utils.Logging

import scala.concurrent.Future

object Schema extends Logging {
  val tables = Seq(
    "users" -> CreateUsersTable,

    "user_profiles" -> CreateUserProfilesTable,
    "password_info" -> CreatePasswordInfoTable,
    "oauth1_info" -> CreateOAuth1InfoTable,
    "oauth2_info" -> CreateOAuth2InfoTable,
    "openid_info" -> CreateOpenIdInfoTable,
    "session_info" -> CreateSessionInfoTable,

    "requests" -> CreateRequestsTable,
    "client_trace" -> CreateClientTraceTable,

    "user_feedback" -> CreateUserFeedbackTable,
    "user_feedback_notes" -> CreateUserFeedbackNotesTable,

    "daily_metrics" -> CreateDailyMetricsTable,

    "adhoc_queries" -> CreateAdHocQueriesTable
  )

  def update() = {
    val tableFuture = tables.foldLeft(Future.successful(Unit)) { (f, t) =>
      f.flatMap { u =>
        Database.query(DdlQueries.DoesTableExist(t._1)).flatMap { exists =>
          if (exists) {
            Future.successful(Unit)
          } else {
            log.info(s"Creating missing table [${t._1}].")
            val name = s"CreateTable-${t._1}"
            Database.raw(name, t._2.sql).map(x => Unit)
          }
        }
      }
    }

    tableFuture.flatMap { ok =>
      createUser(Database.query(DdlQueries.DoesTestUserExist), DdlQueries.InsertTestUser)
    }
  }

  def wipe() = {
    log.warn("Wiping database schema.")
    val tableNames = tables.reverse.map(_._1)
    Database.execute(DdlQueries.TruncateTables(tableNames)).map(x => tableNames)
  }

  private[this] def createUser(q: Future[Boolean], insert: Statement) = q.flatMap { exists =>
    if (exists) {
      Future.successful(Unit)
    } else {
      log.info(s"Creating user [${insert.getClass.getSimpleName}].")
      Database.execute(insert).map(x => x == 1)
    }
  }
}

{'fraction_non_alphanumeric': 0.12641651976832033, 'fraction_numerical': 0.005036514731805591, 'mean_word_length': 3.336244541484716, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1765767', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1259, 'n_words': 207}