From fc1f5ccaa94aba0c0ecc0b4e9af885dfea320df1 Mon Sep 17 00:00:00 2001 From: Stewart Stewart Date: Fri, 1 Dec 2017 09:54:20 -0800 Subject: make generated SchemaDescription include statement for creating SchemaName --- src/main/scala/Generators.scala | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/scala/Generators.scala b/src/main/scala/Generators.scala index 5bc192f..e02cbd6 100644 --- a/src/main/scala/Generators.scala +++ b/src/main/scala/Generators.scala @@ -65,19 +65,23 @@ class TableSourceCodeGenerator(schemaOnlyModel: m.Model, // TODO: fix upstream val tableCode = super.code.lines.drop(1).mkString("\n") - val tripleQuote = "\"\"\"" - val namespaceDDL = - s"""|val createNamespaceSchema = { - | implicit val GRUnit = slick.jdbc.GetResult(_ => ()) - | sql${tripleQuote}CREATE SCHEMA IF NOT EXISTS "$schemaName";${tripleQuote}.as[Unit] - |} - | - |val dropNamespaceSchema = { - | implicit val GRUnit = slick.jdbc.GetResult(_ => ()) - | sql${tripleQuote}DROP SCHEMA "$schemaName" CASCADE;${tripleQuote}.as[Unit] - |} """ - - tableCode + "\n\n" + namespaceDDL + 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 schemaDDL: Vector[String] = + s"""|lazy val schema: profile.SchemaDescription = { + | val schemaDDL = profile.DDL( + | create1 = "create schema " + profile.quoteIdentifier("$schemaName"), + | drop2 = "drop schema " + profile.quoteIdentifier("$schemaName")) + | schemaDDL ++ ${tablesDDL.trim} + |}""".stripMargin.lines.toVector + (before ++ schemaDDL ++ after).mkString("\n") + } catch { + case _: MatchError => throw new Exception("failed to modify schemaddl line") + } + } else tableCode } override def Table = new this.TypedIdTable(_) { table => -- cgit v1.2.3