forked from Netflix/titus-control-plane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jooq.gradle
93 lines (83 loc) · 3.05 KB
/
jooq.gradle
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import com.opentable.db.postgres.embedded.EmbeddedPostgres
import java.sql.Connection
import java.sql.ResultSet
import java.sql.Statement
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.opentable.components:otj-pg-embedded:0.13.+'
classpath group: 'org.yaml', name: 'snakeyaml', version: '1.19'
}
}
ext.flywayVersion = '6.5.+'
ext.jooqVersion = '3.13.2'
ext.psql = '42.2.8'
// Begin: Jooq code generation using EmbeddedPostgres database and Flyway migration
ext.dbProfile = null
ext.dbSchemas = null
ext.jdbcUrl = ''
ext.jdbcUser = null
ext.jdbcPassword = null
ext.flyway = null
ext.jooq = null
ext.dataSource = null
ext.pg = null
ext.jdbcPort = -1
def startEmbeddedPostgresDatabase() {
dbProfile = project.getProperty('dbProfile')
println 'Resolving RDBS test configuration...'
def profileFile = new File(System.getenv("HOME"), ".titus-jooq.yaml")
def useLocal = false
if (profileFile.exists()) {
def cfg = new org.yaml.snakeyaml.Yaml().load(profileFile.newInputStream())
println 'Loaded profiles ' + cfg
def profileCfg = cfg.profiles[dbProfile]
if (profileCfg != null) {
println 'Database profile found: ' + profileCfg
jdbcPort = 5432
jdbcUrl = profileCfg.databaseUrl
jdbcUser = profileCfg.user
jdbcPassword = profileCfg.password
useLocal = true
}
}
if (!useLocal) {
pg = EmbeddedPostgres.start()
jdbcUser = 'postgres'
jdbcPassword = 'postgres'
jdbcPort = pg.getPort()
jdbcUrl = 'jdbc:postgresql://localhost:' + pg.getPort() + '/postgres'
println 'Postgres ' + jdbcUrl
dataSource = pg.getPostgresDatabase()
// The following code checks we are able to run a test query to the DB
Connection connection = dataSource.getConnection()
Statement statement = connection.createStatement()
ResultSet rs = statement.executeQuery("SELECT 1")
assert rs.next()
assert rs.getInt(1) == 1
println 'Embedded Postgresql DB started on port: ' + jdbcPort
}
}
task startDb {
group 'Embedded Postgresql DB'
description 'Starts an instance of Embedded Postgresql DB'
doFirst {
startEmbeddedPostgresDatabase()
}
}
task stopDb {
group 'Embedded Postgresql DB'
description 'Stops a running instance of Embedded Postgresql DB'
if (pg != null) {
doLast {
// If you need the embedded DB to still be running for debugging code generation or DB migration,
// comment the following lines out so the DB is active. Connect to the port number in the output using your local postgres client.
// Alternatively, grep the process list (ps) for the word "embedded-pg" to get the postgresql command line arguments.
// You will need to manually terminate the postgresql instances running locally.
println 'Stopping embedded Postgresql DB running on port ' + jdbcPort
if (pg != null) pg.close()
}
}
}