Skip to content

Commit 88f7e2d

Browse files
Removed jooq dependency
1 parent e9dfcd7 commit 88f7e2d

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ repositories {
1111

1212
dependencies {
1313
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
14-
implementation 'org.jooq:jooq:3.12.3'
1514
implementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
1615
}
1716

src/main/kotlin/DatabaseCopier.kt

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
1-
import org.jooq.DSLContext
2-
import org.jooq.impl.DSL
1+
import java.sql.Connection
2+
import java.sql.DriverManager
33

44
class DatabaseCopier(
55
private val host: String,
66
private val port: Int,
77
private val user: String,
88
private val password: String
99
) {
10+
private fun <R> withConnection(url: String, user: String, password: String, block: (Connection) -> R) {
11+
DriverManager.getConnection(url, user, password).use {
12+
block(it)
13+
}
14+
}
15+
16+
private fun Connection.execute(query: String) {
17+
this.createStatement().use {
18+
it.execute(query)
19+
}
20+
}
1021

1122
fun copy(from: String, to: String) {
1223

1324
synchronized(this) {
1425

1526
val url = jdbcUrl(host, port, from)
1627

17-
DSL.using(url, user, password).use({ db: DSLContext ->
18-
28+
withConnection(url, user, password) { db ->
1929
withExclusiveConnectionTo(db, to, from) {
2030
db.execute("drop database if exists $to;")
2131
db.execute("create database $to template $from;")
2232
}
23-
24-
})
33+
}
2534
}
2635
}
2736

37+
2838
private fun jdbcUrl(host: String, port: Int, database: String) =
2939
"jdbc:postgresql://$host:$port/$database?ApplicationName=database-copier"
3040

31-
private fun withExclusiveConnectionTo(db: DSLContext, vararg databaseNames: String, fn: () -> Unit) {
41+
private fun withExclusiveConnectionTo(db: Connection, vararg databaseNames: String, fn: () -> Unit) {
3242

3343
val whereDatNames = databaseNames.asSequence().map({ "datname = '$it'" }).joinToString(" or ")
3444

0 commit comments

Comments
 (0)