Skip to content

Commit 66f4065

Browse files
Merge pull request #8 from adRise/zixuanzhang/sc-905229/feature/slick-code-gen-requires-explicit-table-schema
[905229] Schema must be specified when generating Slick code for tables
2 parents 1811820 + a03ac06 commit 66f4065

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/main/scala/com/github/tototoshi/sbt/slick/CodegenPlugin.scala

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,16 @@ object CodegenPlugin extends sbt.AutoPlugin with PluginDBSupport {
6161
lazy val slickCodegenIncludedTableTypes: SettingKey[Seq[String]] =
6262
settingKey[Seq[String]]("Table types that slick schema can be generated for")
6363

64-
lazy val slickCodegenExcludedTables: SettingKey[Seq[String]] =
65-
settingKey[Seq[String]]("Tables that should be excluded")
64+
lazy val slickCodegenExcludedTables: SettingKey[Seq[(String, String)]] =
65+
settingKey[Seq[(String, String)]](
66+
"Tables that should be excluded" +
67+
"\nkey is the schema name, value is the table name."
68+
)
6669

67-
lazy val slickCodegenIncludedTables: SettingKey[Seq[String]] =
68-
settingKey[Seq[String]](
69-
"Tables that should be included. If this list is not nil, only the included tables minus excluded will be taken."
70+
lazy val slickCodegenIncludedTables: SettingKey[Seq[(String, String)]] =
71+
settingKey[Seq[(String, String)]](
72+
"Tables that should be included. If this list is not nil, only the included tables minus excluded will be taken." +
73+
"\nkey is the schema name, value is the table name."
7074
)
7175

7276
/** Define a new configuration scope for slick code gen,
@@ -166,10 +170,21 @@ object CodegenPlugin extends sbt.AutoPlugin with PluginDBSupport {
166170
throw new RuntimeException("Failed to run slick-codegen: " + e.getMessage, e)
167171
}
168172

173+
def containsTable(seq: Seq[(String, String)], t: MTable): Boolean = {
174+
seq.exists { case (schema, name) => t.name.schema.contains(schema) && t.name.name == name }
175+
}
176+
169177
val tables = MTable
170178
.getTables(None, None, None, Some(tableTypes))
171-
.map(ts => ts.filter(t => included.isEmpty || (included contains t.name.name)))
172-
.map(ts => ts.filterNot(t => excluded contains t.name.name))
179+
.map (
180+
ts =>
181+
// only include specified tables
182+
// if included is empty, then include all tables
183+
// if included is not empty, then only include the specified tables
184+
(if (included.isEmpty) ts else ts.filter(t => containsTable(included, t)))
185+
// if excluded is not empty, then exclude the specified tables
186+
.filterNot(t => containsTable(excluded, t))
187+
)
173188

174189
val dbio = for {
175190
m <- driver.createModel(Some(tables))

0 commit comments

Comments
 (0)