From 79479db14b8b86c349d3bce0bbc8bba830081841 Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Thu, 1 Feb 2018 09:55:46 -0800 Subject: Bump sbt-settings and run scalafmt --- project/plugins.sbt | 2 +- src/main/scala/CodegenPlugin.scala | 10 ++++---- src/main/scala/Generators.scala | 52 ++++++++++++++++++++------------------ src/main/scala/Main.scala | 52 ++++++++++++++++++++------------------ src/main/scala/OutputHelpers.scala | 18 +++++++------ src/main/scala/SchemaParser.scala | 5 ++-- src/main/scala/TypedIdTable.scala | 8 +++--- 7 files changed, 78 insertions(+), 69 deletions(-) diff --git a/project/plugins.sbt b/project/plugins.sbt index 55c530f..1ef8b09 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,3 +1,3 @@ resolvers += "releases" at "https://drivergrp.jfrog.io/drivergrp/releases" -addSbtPlugin("xyz.driver" % "sbt-settings" % "1.0.1") +addSbtPlugin("xyz.driver" % "sbt-settings" % "1.0.6") diff --git a/src/main/scala/CodegenPlugin.scala b/src/main/scala/CodegenPlugin.scala index f27c591..4f9e180 100644 --- a/src/main/scala/CodegenPlugin.scala +++ b/src/main/scala/CodegenPlugin.scala @@ -18,11 +18,11 @@ object CodegenPlugin extends AutoPlugin { * @param foreignKeys foreign key references to data models add manually */ case class CodegenDatabase( - databaseURI: String, - outputPackage: String, - outputPath: String, - schemaWhitelist: List[String] = List.empty, - foreignKeys: Map[TableColumn, TableColumn] = Map.empty + databaseURI: String, + outputPackage: String, + outputPath: String, + schemaWhitelist: List[String] = List.empty, + foreignKeys: Map[TableColumn, TableColumn] = Map.empty ) lazy val codegenDatabaseConfigs = SettingKey[List[CodegenDatabase]]( diff --git a/src/main/scala/Generators.scala b/src/main/scala/Generators.scala index e02cbd6..8f81620 100644 --- a/src/main/scala/Generators.scala +++ b/src/main/scala/Generators.scala @@ -1,14 +1,14 @@ import slick.{model => m} class RowSourceCodeGenerator( - model: m.Model, - override val headerComment: String, - override val imports: String, - override val schemaName: String, - fullDatabaseModel: m.Model, - idType: Option[String], - manualForeignKeys: Map[(String, String), (String, String)], - typeReplacements: Map[String, String] + model: m.Model, + override val headerComment: String, + override val imports: String, + override val schemaName: String, + fullDatabaseModel: m.Model, + idType: Option[String], + manualForeignKeys: Map[(String, String), (String, String)], + typeReplacements: Map[String, String] ) extends TypedIdSourceCodeGenerator( singleSchemaModel = model, databaseModel = fullDatabaseModel, @@ -35,20 +35,22 @@ class RowSourceCodeGenerator( override def code = tables.map(_.code.mkString("\n")).mkString("\n\n") } -class TableSourceCodeGenerator(schemaOnlyModel: m.Model, - override val headerComment: String, - override val imports: String, - override val schemaName: String, - fullDatabaseModel: m.Model, - pkg: String, - manualForeignKeys: Map[(String, String), (String, String)], - override val parentType: Option[String], - idType: Option[String], - typeReplacements: Map[String, String]) - extends TypedIdSourceCodeGenerator(singleSchemaModel = schemaOnlyModel, - databaseModel = fullDatabaseModel, - idType, - manualForeignKeys) with TableOutputHelpers { +class TableSourceCodeGenerator( + schemaOnlyModel: m.Model, + override val headerComment: String, + override val imports: String, + override val schemaName: String, + fullDatabaseModel: m.Model, + pkg: String, + manualForeignKeys: Map[(String, String), (String, String)], + override val parentType: Option[String], + idType: Option[String], + typeReplacements: Map[String, String]) + extends TypedIdSourceCodeGenerator( + singleSchemaModel = schemaOnlyModel, + databaseModel = fullDatabaseModel, + idType, + manualForeignKeys) with TableOutputHelpers { val defaultIdImplementation = """|final case class Id[T](v: Int) @@ -68,8 +70,8 @@ class TableSourceCodeGenerator(schemaOnlyModel: m.Model, if (ddlEnabled && schemaName != "public") { // TODO: "public" is postgres-specific // TODO: make generated DDL overrideable upstream try { - val (before, ddl +: after) = tableCode.lines.toVector.span(! _.contains("val schema")) - val Array(identifier, tablesDDL) = ddl.split('=').map(_.trim) + val (before, ddl +: after) = tableCode.lines.toVector.span(!_.contains("val schema")) + val Array(_, tablesDDL) = ddl.split('=').map(_.trim) val schemaDDL: Vector[String] = s"""|lazy val schema: profile.SchemaDescription = { | val schemaDDL = profile.DDL( @@ -77,7 +79,7 @@ class TableSourceCodeGenerator(schemaOnlyModel: m.Model, | drop2 = "drop schema " + profile.quoteIdentifier("$schemaName")) | schemaDDL ++ ${tablesDDL.trim} |}""".stripMargin.lines.toVector - (before ++ schemaDDL ++ after).mkString("\n") + (before ++ schemaDDL ++ after).mkString("\n") } catch { case _: MatchError => throw new Exception("failed to modify schemaddl line") } diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala index 39100eb..6fe0417 100644 --- a/src/main/scala/Main.scala +++ b/src/main/scala/Main.scala @@ -16,28 +16,30 @@ trait RowFileGenerator { self: SourceCodeGenerator => object Generator { - private def outputSchemaCode(schemaName: String, - folder: String, - pkg: String, - tableGen: TableFileGenerator, - rowGen: RowFileGenerator): Unit = { + private def outputSchemaCode( + schemaName: String, + folder: String, + pkg: String, + tableGen: TableFileGenerator, + rowGen: RowFileGenerator): Unit = { val camelSchemaName = schemaName.split('_').map(_.capitalize).mkString("") tableGen.writeTablesToFile(folder: String, pkg: String, fileName = s"${camelSchemaName}Tables.scala") rowGen.writeRowsToFile(folder: String, pkg: String, fileName = s"${camelSchemaName}Rows.scala") } - def run(uri: URI, - pkg: String, - schemaNames: Option[List[String]], - outputPath: String, - manualForeignKeys: Map[(String, String), (String, String)], - parentType: Option[String], - idType: Option[String], - header: String, - tablesFileImports: List[String], - rowsFileImports: List[String], - typeReplacements: Map[String, String]) = { + def run( + uri: URI, + pkg: String, + schemaNames: Option[List[String]], + outputPath: String, + manualForeignKeys: Map[(String, String), (String, String)], + parentType: Option[String], + idType: Option[String], + header: String, + tablesFileImports: List[String], + rowsFileImports: List[String], + typeReplacements: Map[String, String]) = { val dc: DatabaseConfig[JdbcProfile] = DatabaseConfig.forURI[JdbcProfile](uri) val parsedSchemasOpt: Option[Map[String, List[String]]] = @@ -51,9 +53,10 @@ object Generator { parsedSchemasOpt.getOrElse(Map.empty).foreach { case (schemaName, tables) => - val schemaOnlyModel = Await.result(dc.db.run(ModelTransformation - .createModel(dc.profile, Some(Map(schemaName -> tables)))), - Duration.Inf) + val schemaOnlyModel = Await.result( + dc.db.run(ModelTransformation + .createModel(dc.profile, Some(Map(schemaName -> tables)))), + Duration.Inf) val rowGenerator = new RowSourceCodeGenerator( model = schemaOnlyModel, @@ -80,11 +83,12 @@ object Generator { typeReplacements ) - outputSchemaCode(schemaName = schemaName, - folder = outputPath, - pkg = pkg, - tableGen = tableGenerator, - rowGen = rowGenerator) + outputSchemaCode( + schemaName = schemaName, + folder = outputPath, + pkg = pkg, + tableGen = tableGenerator, + rowGen = rowGenerator) } } finally { dc.db.close() diff --git a/src/main/scala/OutputHelpers.scala b/src/main/scala/OutputHelpers.scala index 6b0ab2e..4b97f79 100644 --- a/src/main/scala/OutputHelpers.scala +++ b/src/main/scala/OutputHelpers.scala @@ -21,10 +21,11 @@ trait TableOutputHelpers extends TableFileGenerator with OutputHelpers { self: S |""".stripMargin.trim() def writeTablesToFile(folder: String, pkg: String, fileName: String): Unit = { - writeStringToFile(content = packageTableCode(headerComment, pkg, schemaName, imports), - folder = folder, - pkg = s"$pkg.$schemaName", - fileName = fileName) + writeStringToFile( + content = packageTableCode(headerComment, pkg, schemaName, imports), + folder = folder, + pkg = s"$pkg.$schemaName", + fileName = fileName) } } @@ -47,9 +48,10 @@ trait RowOutputHelpers extends RowFileGenerator with OutputHelpers { self: Sourc def writeRowsToFile(folder: String, pkg: String, fileName: String): Unit = { - writeStringToFile(content = packageRowCode(headerComment, schemaName, pkg, imports), - folder = folder, - pkg = s"$pkg.$schemaName", - fileName = fileName) + writeStringToFile( + content = packageRowCode(headerComment, schemaName, pkg, imports), + folder = folder, + pkg = s"$pkg.$schemaName", + fileName = fileName) } } diff --git a/src/main/scala/SchemaParser.scala b/src/main/scala/SchemaParser.scala index bf555c3..a5f0205 100644 --- a/src/main/scala/SchemaParser.scala +++ b/src/main/scala/SchemaParser.scala @@ -19,8 +19,9 @@ object ModelTransformation { }) } else column)))) - def references(dbModel: m.Model, - tcMappings: Map[(String, String), (String, String)]): Map[(String, String), (m.Table, m.Column)] = { + def references( + dbModel: m.Model, + tcMappings: Map[(String, String), (String, String)]): Map[(String, String), (m.Table, m.Column)] = { def getTableColumn(tc: (String, String)): (m.Table, m.Column) = { val (tableName, columnName) = tc val table = dbModel.tables diff --git a/src/main/scala/TypedIdTable.scala b/src/main/scala/TypedIdTable.scala index ead4a49..64f90a2 100644 --- a/src/main/scala/TypedIdTable.scala +++ b/src/main/scala/TypedIdTable.scala @@ -2,10 +2,10 @@ import slick.codegen.SourceCodeGenerator import slick.{model => m} class TypedIdSourceCodeGenerator( - singleSchemaModel: m.Model, - databaseModel: m.Model, - idType: Option[String], - manualForeignKeys: Map[(String, String), (String, String)] + singleSchemaModel: m.Model, + databaseModel: m.Model, + idType: Option[String], + manualForeignKeys: Map[(String, String), (String, String)] ) extends SourceCodeGenerator(singleSchemaModel) { val manualReferences = ModelTransformation.references(databaseModel, manualForeignKeys) -- cgit v1.2.3 From f6d5aed87edf688b768f209eca894631d831b560 Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Thu, 1 Feb 2018 09:54:26 -0800 Subject: Apply citextColumnNoLength to indices columns --- src/main/scala/SchemaParser.scala | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/scala/SchemaParser.scala b/src/main/scala/SchemaParser.scala index a5f0205..5ea61bf 100644 --- a/src/main/scala/SchemaParser.scala +++ b/src/main/scala/SchemaParser.scala @@ -9,15 +9,22 @@ import slick.{model => m} object ModelTransformation { + def citextColumnNoLength(column: m.Column): m.Column = + if (column.options contains SqlType("citext")) { + column.copy(options = column.options.filter { + case _: Length => false + case _ => true + }) + } else column + def citextNoLength(dbModel: m.Model): m.Model = - dbModel.copy(tables = dbModel.tables.map(table => - table.copy(columns = table.columns.map(column => - if (column.options contains SqlType("citext")) { - column.copy(options = column.options.filter { - case _: Length => false - case _ => true - }) - } else column)))) + dbModel.copy( + tables = dbModel.tables.map( + table => + table.copy( + columns = table.columns.map(citextColumnNoLength), + indices = table.indices.map(index => index.copy(columns = index.columns.map(citextColumnNoLength))) + ))) def references( dbModel: m.Model, -- cgit v1.2.3 From fac4c03b45abe9dd3e5b2713dad7f1649992f6e2 Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Fri, 2 Feb 2018 11:25:31 -0800 Subject: Apply fix to foreign and primary keys --- src/main/scala/SchemaParser.scala | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/scala/SchemaParser.scala b/src/main/scala/SchemaParser.scala index 5ea61bf..e7ef031 100644 --- a/src/main/scala/SchemaParser.scala +++ b/src/main/scala/SchemaParser.scala @@ -19,12 +19,18 @@ object ModelTransformation { def citextNoLength(dbModel: m.Model): m.Model = dbModel.copy( - tables = dbModel.tables.map( - table => - table.copy( - columns = table.columns.map(citextColumnNoLength), - indices = table.indices.map(index => index.copy(columns = index.columns.map(citextColumnNoLength))) - ))) + tables = dbModel.tables.map(table => + table.copy( + primaryKey = table.primaryKey.map(pk => pk.copy(columns = pk.columns.map(citextColumnNoLength))), + columns = table.columns.map(citextColumnNoLength), + indices = table.indices.map(index => index.copy(columns = index.columns.map(citextColumnNoLength))), + foreignKeys = table.foreignKeys.map { fk => + fk.copy( + referencedColumns = fk.referencedColumns.map(citextColumnNoLength), + referencingColumns = fk.referencingColumns.map(citextColumnNoLength) + ) + } + ))) def references( dbModel: m.Model, -- cgit v1.2.3