1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# _sbt_ plugin for slick schema code generation
Extends/customizes the
[Slick schema code generator](http://slick.lightbend.com/doc/3.1.1/code-generation.html) and wraps it in a parameterized plugin. See [original source code here](https://github.com/slick/slick/tree/master/slick-codegen/src/main/scala/slick/codegen).
## Configuration
### project/plugins.sbt
```sbt
resolvers += "releases" at "https://drivergrp.jfrog.io/drivergrp/releases"
credentials += Credentials("Artifactory Realm", "drivergrp.jfrog.io", "sbt-publisher", "***REMOVED***")
addSbtPlugin("xyz.driver" % "sbt-slick-codegen" % "0.8")
// Replace with the appropriate jdbc driver for your database:
libraryDependencies += "org.postgresql" % "postgresql" % "9.3-1104-jdbc41"
```
Note that the jdbc-driver needs to be added separately to `plugins.sbt` and `build.sbt` as a dependency for the project and codegen, respectively.
(also note that postgres jdbc driver `9.4.x` will not expose columns in certain metadata "tables", causing generated code not to compile)
### build.sbt
Minimally, define `codegenURI`, `codegenPackage`, and `codegenOutputPath` like so:
```sbt
enablePlugins(CodegenPlugin)
codegenURI := "file:src/main/resources/conf/database.conf#database"
codegenPackage := "xyz.driver.schemas"
codegenOutputPath := (baseDirectory.value / "src" / "main" / "scala").getPath
```
Use `settings -V codegen` to view documentation for all available codegen settings.
## Using
### Local dev/testing instance with sqitch and docker
Code should be committed locally (rather than being placed in a sourceMangaged directory and being generated as part of build), so that it can be read, reviewed, and debugged. This requires having a locally accessible database running from which to generate code. Configure such a dabase like so:
0. Install docker.
1. Start a vanilla docker instance
```
docker run -it -d -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_DB=postgres -e POSTGRES_PASSWORD=postgres postgres
```
add `127.0.0.1 postgres postgres` to `/etc/hosts` to make it accessible locally as "postgres".
2. Configure database in typesafe config
If sqitch is already configured, modify step 1 to make docker instance look like the appropriate database in `application.conf`. For the above docker postgres instance, the following configuration will work:
```
database {
driver = "slick.driver.PostgresDriver$"
db {
url = "jdbc:postgresql://postgres:5432/postgres"
driver = org.postgresql.Driver
user = "postgres"
password = "postgres"
# other options as appropriate
}
}
```
In `build.sbt` make sure `codegenURI := "typesafeConfig#database"` where `typesafeConfig` is the url of the typesafe config and `database` is the name of the database configuration in that file to use.
3. Run codegen with `sbt genTables`
Rememeber to run migrations first! If you just added a new schema/table in a migration, be sure to update `codegenSchemaWhitelist`. Other than automated code formatting, the output source shouldn't be modified, since `genTables` should be run every time the schema is updated and will overwrite any manual changes.
|