Skip to content

Commit

Permalink
Close SQL statements during migrations (#2524)
Browse files Browse the repository at this point in the history
We weren't correctly closing SQL statements when comparing DBs and
migrating them. All the DB handlers for normal operation (read/write
channels, payments, etc) are already correctly closed after execution.

Fixes #2425
  • Loading branch information
t-bast authored Dec 13, 2022
1 parent cdedbdf commit 683328d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ trait JdbcUtils {
migrate: (ResultSet, PreparedStatement) => Unit)(implicit logger: Logger): Int = {
val insertStatement = destination.prepareStatement(migrateSql)
val batchSize = 50
JdbcUtils.using(source.prepareStatement(s"SELECT * FROM $sourceTable")) { queryStatement =>
using(source.prepareStatement(s"SELECT * FROM $sourceTable")) { queryStatement =>
val rs = queryStatement.executeQuery()
var inserted = 0
var batchCount = 0
Expand Down Expand Up @@ -194,11 +194,6 @@ trait JdbcUtils {
if (rs.wasNull()) None else Some(result)
}

def getUUIDNullable(label: String): Option[UUID] = {
val result = rs.getString(label)
if (rs.wasNull()) None else Some(UUID.fromString(result))
}

def getMilliSatoshiNullable(label: String): Option[MilliSatoshi] = {
val result = rs.getLong(label)
if (rs.wasNull()) None else Some(MilliSatoshi(result))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.acinq.eclair.db.migration

import fr.acinq.eclair.db.Databases.{PostgresDatabases, SqliteDatabases}
import fr.acinq.eclair.db.DualDatabases
import fr.acinq.eclair.db.jdbc.JdbcUtils.using
import fr.acinq.eclair.db.pg.PgUtils
import grizzled.slf4j.Logging
import scodec.bits.ByteVector
Expand All @@ -16,30 +17,27 @@ object CompareDb extends Logging {
table2: String,
hash1: ResultSet => ByteVector,
hash2: ResultSet => ByteVector): Boolean = {
val rs1 = conn1.prepareStatement(s"SELECT * FROM $table1").executeQuery()
val rs2 = conn2.prepareStatement(s"SELECT * FROM $table2").executeQuery()

var hashes1 = List.empty[ByteVector]
while (rs1.next()) {
hashes1 = hash1(rs1) +: hashes1
using(conn1.prepareStatement(s"SELECT * FROM $table1")) { statement =>
val rs = statement.executeQuery()
while (rs.next()) hashes1 = hash1(rs) +: hashes1
}

var hashes2 = List.empty[ByteVector]
while (rs2.next()) {
hashes2 = hash2(rs2) +: hashes2
using(conn2.prepareStatement(s"SELECT * FROM $table2")) { statement =>
val rs = statement.executeQuery()
while (rs.next()) hashes2 = hash2(rs) +: hashes2
}

val res = hashes1.sorted == hashes2.sorted

if (res) {
if (hashes1.sorted == hashes2.sorted) {
logger.info(s"tables $table1/$table2 are identical")
true
} else {
val diff1 = hashes1 diff hashes2
val diff2 = hashes2 diff hashes1
logger.warn(s"tables $table1/$table2 are different diff1=${diff1.take(3).map(_.toHex.take(128))} diff2=${diff2.take(3).map(_.toHex.take(128))}")
false
}

res
}

// @formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package fr.acinq.eclair.db.migration
import fr.acinq.eclair.db.Databases.{PostgresDatabases, SqliteDatabases}
import fr.acinq.eclair.db.DualDatabases
import fr.acinq.eclair.db.jdbc.JdbcUtils
import fr.acinq.eclair.db.jdbc.JdbcUtils.using
import fr.acinq.eclair.db.pg.PgUtils
import grizzled.slf4j.Logging

import java.sql.{Connection, PreparedStatement, ResultSet}

object MigrateDb extends Logging {

private def getVersion(conn: Connection,
dbName: String): Int = {
val statement = conn.prepareStatement(s"SELECT version FROM versions WHERE db_name='$dbName'")
val res = statement.executeQuery()
res.next()
res.getInt("version")
private def getVersion(conn: Connection, dbName: String): Int = {
using(conn.prepareStatement(s"SELECT version FROM versions WHERE db_name='$dbName'")) { statement =>
val res = statement.executeQuery()
res.next()
res.getInt("version")
}
}

def checkVersions(source: Connection,
Expand Down

0 comments on commit 683328d

Please sign in to comment.