/
StandardTestDBs.scala
230 lines (208 loc) · 9.38 KB
/
StandardTestDBs.scala
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package com.typesafe.slick.testkit.util
import java.io.File
import java.util.logging.{Level, Logger}
import java.sql.SQLException
import slick.dbio._
import slick.driver._
import slick.memory.MemoryDriver
import slick.jdbc.{StaticQuery => Q, SimpleJdbcAction, ResultSetAction, ResultSetInvoker}
import slick.jdbc.GetResult._
import slick.jdbc.meta.MTable
import org.junit.Assert
import scala.concurrent.ExecutionContext
object StandardTestDBs {
lazy val H2Mem = new H2TestDB("h2mem", false) {
val url = "jdbc:h2:mem:test1"
override def isPersistent = false
}
lazy val H2MemKeepAlive = new H2TestDB("h2mem", true) {
val url = "jdbc:h2:mem:test1"
}
lazy val H2Disk = new H2TestDB("h2disk", false) {
val dbName = "h2-"+confName
val url = "jdbc:h2:"+TestkitConfig.testDBPath+"/"+dbName
override def cleanUpBefore() = TestDB.deleteDBFiles(dbName)
// Recreating the DB is faster than dropping everything individually
override def dropUserArtifacts(implicit session: profile.Backend#Session) = {
session.close()
cleanUpBefore()
}
}
lazy val HsqldbMem = new HsqlDB("hsqldbmem") {
val dbName = "test1"
val url = "jdbc:hsqldb:mem:"+dbName+";user=SA;password=;shutdown=true"
override def isPersistent = false
}
lazy val HsqldbDisk = new HsqlDB("hsqldbdisk") {
val dbName = "hsqldb-"+confName
val url = "jdbc:hsqldb:file:"+TestkitConfig.testDBPath+"/"+dbName+";user=SA;password=;shutdown=true;hsqldb.applog=0"
override def cleanUpBefore() = TestDB.deleteDBFiles(dbName)
// Recreating the DB is faster than dropping everything individually
override def dropUserArtifacts(implicit session: profile.Backend#Session) = {
session.close()
cleanUpBefore()
}
}
lazy val SQLiteMem = new SQLiteTestDB("jdbc:sqlite:file:slick_test?mode=memory&cache=shared", "sqlitemem") {
override def isPersistent = false
}
lazy val SQLiteDisk = {
val confName = "sqlitedisk"
val prefix = "sqlite-"+confName
new SQLiteTestDB("jdbc:sqlite:"+TestkitConfig.testDBPath+"/"+prefix+".db", confName) {
override def cleanUpBefore() = TestDB.deleteDBFiles(prefix)
}
}
lazy val DerbyMem = new DerbyDB("derbymem") {
val dbName = "test1"
val url = "jdbc:derby:memory:"+dbName+";create=true"
override def cleanUpBefore() = {
val dropUrl = "jdbc:derby:memory:"+dbName+";drop=true"
try { await(profile.backend.Database.forURL(dropUrl, driver = jdbcDriver).run(SimpleJdbcAction(_.connection))) }
catch { case e: SQLException => }
}
}
lazy val DerbyDisk = new DerbyDB("derbydisk") {
val dbName = "derby-"+confName
val url = "jdbc:derby:"+TestkitConfig.testDBPath+"/"+dbName+";create=true"
override def cleanUpBefore() = {
val dropUrl = "jdbc:derby:"+TestkitConfig.testDBPath+"/"+dbName+";shutdown=true"
try { await(profile.backend.Database.forURL(dropUrl, driver = jdbcDriver).run(SimpleJdbcAction(_.connection))) }
catch { case e: SQLException => }
TestDB.deleteDBFiles(dbName)
}
}
lazy val Postgres = new ExternalJdbcTestDB("postgres") {
val driver = PostgresDriver
override def localTables(implicit ec: ExecutionContext): DBIO[Vector[String]] =
ResultSetAction[(String,String,String, String)](_.conn.getMetaData().getTables("", "public", null, null)).map { ts =>
ts.filter(_._4.toUpperCase == "TABLE").map(_._3).sorted
}
override def getLocalSequences(implicit session: profile.Backend#Session) = {
val tables = ResultSetInvoker[(String,String,String, String)](_.conn.getMetaData().getTables("", "public", null, null))
tables.buildColl[List].filter(_._4.toUpperCase == "SEQUENCE").map(_._3).sorted
}
override def capabilities = super.capabilities - TestDB.capabilities.jdbcMetaGetFunctions
}
lazy val MySQL = new ExternalJdbcTestDB("mysql") {
val driver = MySQLDriver
// Recreating the DB is faster than dropping everything individually
override def dropUserArtifacts(implicit session: profile.Backend#Session) = {
session.close()
cleanUpBefore()
}
}
lazy val MSAccess = new AccessDB("access")
lazy val Heap = new RelationalTestDB {
type Driver = MemoryDriver
val driver: Driver = MemoryDriver
val confName: String = "heap"
def createDB: profile.Backend#Database = profile.backend.Database(ExecutionContext.global)
def dropUserArtifacts(implicit session: profile.Backend#Session) {
val db = session.database
db.getTables.foreach(t => db.dropTable(t.name))
}
def assertTablesExist(tables: String*)(implicit session: profile.Backend#Session) {
val all = session.database.getTables.map(_.name).toSet
for(t <- tables) {
if(!all.contains(t)) Assert.fail("Table "+t+" should exist")
}
}
def assertNotTablesExist(tables: String*)(implicit session: profile.Backend#Session) {
val all = session.database.getTables.map(_.name).toSet
for(t <- tables) {
if(all.contains(t)) Assert.fail("Table "+t+" should not exist")
}
}
}
}
abstract class H2TestDB(confName: String, keepAlive: Boolean) extends InternalJdbcTestDB(confName) {
val driver = H2Driver
val jdbcDriver = "org.h2.Driver"
override def capabilities = super.capabilities - TestDB.capabilities.jdbcMetaGetFunctions - TestDB.capabilities.jdbcMetaGetClientInfoProperties
override def createDB(): profile.Backend#Database = database.forURL(url, driver = jdbcDriver, keepAliveConnection = keepAlive)
}
class SQLiteTestDB(dburl: String, confName: String) extends InternalJdbcTestDB(confName) {
val driver = SQLiteDriver
val url = dburl
val jdbcDriver = "org.sqlite.JDBC"
override def localTables(implicit ec: ExecutionContext): DBIO[Vector[String]] =
super.localTables.map(_.filter(s => !s.toLowerCase.contains("sqlite_")))
override def dropUserArtifacts(implicit session: profile.Backend#Session) = {
for(t <- getLocalTables)
(Q.u+"drop table if exists "+driver.quoteIdentifier(t)).execute
for(t <- getLocalSequences)
(Q.u+"drop sequence if exists "+driver.quoteIdentifier(t)).execute
}
}
@deprecated("AccessDriver will be removed when we drop support for Java versions < 8", "2.1")
class AccessDB(confName: String) extends ExternalJdbcTestDB(confName) {
val driver = AccessDriver
val dir = new File(TestkitConfig.testDir)
val dbPath = dir.getAbsolutePath.replace("\\", "/")
lazy val emptyDBFile = confString("emptyDBFile")
lazy val testDBFile = confString("testDBFile")
override def cleanUpBefore() {
cleanUpAfter()
TestDB.copy(new File(emptyDBFile), new File(testDBFile))
}
override def cleanUpAfter() = TestDB.deleteDBFiles(testDB)
override def dropUserArtifacts(implicit session: profile.Backend#Session) = {
session.close()
cleanUpBefore()
}
/* Works in some situations but fails with "Optional feature not implemented" in others */
override def canGetLocalTables = false
override def localTables(implicit ec: ExecutionContext): DBIO[Vector[String]] =
MTable.getTables.map(_.map(_.name.name).sorted)
override def capabilities = super.capabilities - TestDB.capabilities.jdbcMeta
}
abstract class DerbyDB(confName: String) extends InternalJdbcTestDB(confName) {
val driver = DerbyDriver
System.setProperty("derby.stream.error.method", classOf[DerbyDB].getName + ".DEV_NULL")
val jdbcDriver = "org.apache.derby.jdbc.EmbeddedDriver"
override def localTables(implicit ec: ExecutionContext): DBIO[Vector[String]] =
ResultSetAction[(String,String,String, String)](_.conn.getMetaData().getTables(null, "APP", null, null)).map { ts =>
ts.map(_._3).sorted
}
override def dropUserArtifacts(implicit session: profile.Backend#Session) = {
try {
try { (Q.u+"create table \"__derby_dummy\"(x integer primary key)").execute }
catch { case ignore: SQLException => }
val constraints = (Q[(String, String)]+"""
select c.constraintname, t.tablename
from sys.sysconstraints c, sys.sysschemas s, sys.systables t
where c.schemaid = s.schemaid and c.tableid = t.tableid and s.schemaname = 'APP'
""").buildColl[List]
for((c, t) <- constraints if !c.startsWith("SQL"))
(Q.u+"alter table "+driver.quoteIdentifier(t)+" drop constraint "+driver.quoteIdentifier(c)).execute
for(t <- getLocalTables)
(Q.u+"drop table "+driver.quoteIdentifier(t)).execute
for(t <- getLocalSequences)
(Q.u+"drop sequence "+driver.quoteIdentifier(t)).execute
} catch {
case e: Exception =>
println("[Caught Exception while dropping user artifacts in Derby: "+e+"]")
session.close()
cleanUpBefore()
}
}
}
object DerbyDB {
val DEV_NULL = new java.io.OutputStream { def write(b: Int) {} };
}
abstract class HsqlDB(confName: String) extends InternalJdbcTestDB(confName) {
val driver = HsqldbDriver
val jdbcDriver = "org.hsqldb.jdbcDriver"
override def localTables(implicit ec: ExecutionContext): DBIO[Vector[String]] =
ResultSetAction[(String,String,String, String)](_.conn.getMetaData().getTables(null, "PUBLIC", null, null)).map { ts =>
ts.map(_._3).sorted
}
override def cleanUpBefore() {
// Try to turn Hsqldb logging off -- does not work :(
System.setProperty("hsqldb.reconfig_logging", "false")
Logger.getLogger("org.hsqldb.persist.Logger").setLevel(Level.OFF)
Logger.getLogger("org.hsqldb").setLevel(Level.OFF)
Logger.getLogger("hsqldb").setLevel(Level.OFF)
}
}