Skip to content

Commit 82ea98d

Browse files
authored
Merge pull request #185 from tharindu-nw/icp2-auth
Initialize and copy credentials db during build
2 parents edda09e + 7fef1e6 commit 82ea98d

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

build.gradle

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ task packageICP {
198198
}
199199

200200

201-
// Copy database file if it exists
201+
// Copy database files if they exist
202202
if (file('icp_server/database/icpdb.mv.db').exists()) {
203203
def dbDir = file("${rootDir}/bin/database")
204204
if (!dbDir.exists()) {
@@ -209,6 +209,17 @@ task packageICP {
209209
into dbDir
210210
}
211211
}
212+
213+
if (file('icp_server/database/credentialsdb.mv.db').exists()) {
214+
def dbDir = file("${rootDir}/bin/database")
215+
if (!dbDir.exists()) {
216+
dbDir.mkdirs()
217+
}
218+
copy {
219+
from 'icp_server/database/credentialsdb.mv.db'
220+
into dbDir
221+
}
222+
}
212223

213224
// Copy scripts from distribution/scripts folder to bin directory only
214225
def scriptsDir = file('distribution/scripts')

icp_server/build.gradle

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ task dbDown {
192192
}
193193
}
194194

195-
// H2 database initialization task
195+
// H2 database initialization tasks
196196
task initH2Database {
197-
description = 'Initialize H2 database with schema from h2_init.sql'
197+
description = 'Initialize main H2 database with schema from h2_init.sql'
198198

199199
doLast {
200200
logger.quiet('╔════════════════════════════════════════╗')
201-
logger.quiet('║ Initializing H2 Database... ')
201+
logger.quiet('║ Initializing Main H2 Database... ║')
202202
logger.quiet('╚════════════════════════════════════════╝')
203203

204204
def h2InitScript = file('resources/db/init-scripts/h2_init.sql')
@@ -225,24 +225,76 @@ task initH2Database {
225225
onerror: 'abort'
226226
)
227227

228-
logger.quiet('✓ H2 database initialized successfully!')
228+
logger.quiet('Main H2 database initialized successfully!')
229229
logger.lifecycle("Database location: ${h2DbFile.absolutePath}.mv.db")
230230
}
231231
}
232232

233+
task initH2CredentialsDatabase {
234+
description = 'Initialize credentials H2 database with schema from credentials_h2_init.sql'
235+
236+
doLast {
237+
logger.quiet('╔════════════════════════════════════════╗')
238+
logger.quiet('║ Initializing Credentials H2 DB... ║')
239+
logger.quiet('╚════════════════════════════════════════╝')
240+
241+
def credentialsInitScript = file('resources/db/init-scripts/credentials_h2_init.sql')
242+
def credentialsDbFile = file('database/credentialsdb')
243+
244+
if (!credentialsInitScript.exists()) {
245+
throw new Exception("Credentials init script not found at: ${credentialsInitScript.absolutePath}")
246+
}
247+
248+
logger.lifecycle("Using init script: ${credentialsInitScript.absolutePath}")
249+
logger.lifecycle("Database file: ${credentialsDbFile.absolutePath}")
250+
251+
// Execute H2 initialization using Ant SQL task
252+
ant.sql(
253+
driver: 'org.h2.Driver',
254+
url: "jdbc:h2:file:${credentialsDbFile.absolutePath};MODE=MySQL;DB_CLOSE_DELAY=-1",
255+
userid: 'sa',
256+
password: '',
257+
src: credentialsInitScript.absolutePath,
258+
classpath: buildscript.configurations.classpath.asPath,
259+
print: true,
260+
showheaders: false,
261+
showtrailers: false,
262+
onerror: 'abort'
263+
)
264+
265+
logger.quiet('✓ Credentials H2 database initialized successfully!')
266+
logger.lifecycle("Database location: ${credentialsDbFile.absolutePath}.mv.db")
267+
}
268+
}
269+
270+
task initAllH2Databases {
271+
description = 'Initialize both main and credentials H2 databases'
272+
dependsOn initH2Database, initH2CredentialsDatabase
273+
274+
doLast {
275+
logger.quiet('')
276+
logger.quiet('╔════════════════════════════════════════╗')
277+
logger.quiet('║ All H2 Databases Initialized! ✓ ║')
278+
logger.quiet('╠════════════════════════════════════════╣')
279+
logger.quiet('║ • Main DB: database/icpdb.mv.db ║')
280+
logger.quiet('║ • Credentials: database/credentialsdb║')
281+
logger.quiet('╚════════════════════════════════════════╝')
282+
}
283+
}
284+
233285
task cleanH2Database {
234-
description = 'Clean/delete H2 database files'
286+
description = 'Clean/delete all H2 database files (main and credentials)'
235287

236288
doLast {
237289
logger.lifecycle('Cleaning H2 database files...')
238290

239-
def dbFiles = fileTree(dir: '.', include: 'icp_database*.db')
291+
def dbFiles = fileTree(dir: 'database', include: '*.db')
240292
dbFiles.each { file ->
241293
logger.lifecycle("Deleting: ${file.name}")
242294
file.delete()
243295
}
244296

245-
logger.quiet('✓ H2 database files cleaned')
297+
logger.quiet('All H2 database files cleaned')
246298
}
247299
}
248300

0 commit comments

Comments
 (0)