aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzachdriver <zach@driver.xyz>2018-02-02 13:05:33 -0800
committerGitHub <noreply@github.com>2018-02-02 13:05:33 -0800
commitde9166e3d29e91b898aabc1022925dafcf9f379b (patch)
tree5b7279d43ea1819671da21e44f1d6a94771d1bc1
parent0ef6befb187e2302209793fa9a9c207cea0118d3 (diff)
parentfac4c03b45abe9dd3e5b2713dad7f1649992f6e2 (diff)
downloadslick-codegen-plugin-de9166e3d29e91b898aabc1022925dafcf9f379b.tar.gz
slick-codegen-plugin-de9166e3d29e91b898aabc1022925dafcf9f379b.tar.bz2
slick-codegen-plugin-de9166e3d29e91b898aabc1022925dafcf9f379b.zip
Merge pull request #34 from drivergroup/zsmith/citext-index
Apply citextNoLength to indices columns
-rw-r--r--project/plugins.sbt2
-rw-r--r--src/main/scala/CodegenPlugin.scala10
-rw-r--r--src/main/scala/Generators.scala52
-rw-r--r--src/main/scala/Main.scala52
-rw-r--r--src/main/scala/OutputHelpers.scala18
-rw-r--r--src/main/scala/SchemaParser.scala34
-rw-r--r--src/main/scala/TypedIdTable.scala8
7 files changed, 99 insertions, 77 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..e7ef031 100644
--- a/src/main/scala/SchemaParser.scala
+++ b/src/main/scala/SchemaParser.scala
@@ -9,18 +9,32 @@ 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(
+ 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,
- 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)