mirror of
https://github.com/ACINQ/eclair.git
synced 2025-02-23 06:35:11 +01:00
Track version of each logical database (#495)
This commit is contained in:
parent
9f8cd3b580
commit
f3e32b887c
6 changed files with 41 additions and 3 deletions
|
@ -27,7 +27,11 @@ class SqliteChannelsDb(sqlite: Connection) extends ChannelsDb {
|
|||
|
||||
import SqliteUtils._
|
||||
|
||||
val DB_NAME = "channels"
|
||||
val CURRENT_VERSION = 1
|
||||
|
||||
using(sqlite.createStatement()) { statement =>
|
||||
require(getVersion(statement, DB_NAME, CURRENT_VERSION) == CURRENT_VERSION) // there is only one version currently deployed
|
||||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS local_channels (channel_id BLOB NOT NULL PRIMARY KEY, data BLOB NOT NULL)")
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,11 @@ class SqliteNetworkDb(sqlite: Connection) extends NetworkDb {
|
|||
|
||||
import SqliteUtils._
|
||||
|
||||
val DB_NAME = "network"
|
||||
val CURRENT_VERSION = 1
|
||||
|
||||
using(sqlite.createStatement()) { statement =>
|
||||
require(getVersion(statement, DB_NAME, CURRENT_VERSION) == CURRENT_VERSION) // there is only one version currently deployed
|
||||
statement.execute("PRAGMA foreign_keys = ON")
|
||||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS nodes (node_id BLOB NOT NULL PRIMARY KEY, data BLOB NOT NULL)")
|
||||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS channels (short_channel_id INTEGER NOT NULL PRIMARY KEY, txid STRING NOT NULL, data BLOB NOT NULL, capacity_sat INTEGER NOT NULL)")
|
||||
|
|
|
@ -19,7 +19,7 @@ package fr.acinq.eclair.db.sqlite
|
|||
import java.sql.Connection
|
||||
|
||||
import fr.acinq.bitcoin.BinaryData
|
||||
import fr.acinq.eclair.db.sqlite.SqliteUtils.using
|
||||
import fr.acinq.eclair.db.sqlite.SqliteUtils.{getVersion, using}
|
||||
import fr.acinq.eclair.db.{Payment, PaymentsDb}
|
||||
import grizzled.slf4j.Logging
|
||||
|
||||
|
@ -37,7 +37,11 @@ import scala.collection.immutable.Queue
|
|||
*/
|
||||
class SqlitePaymentsDb(sqlite: Connection) extends PaymentsDb with Logging {
|
||||
|
||||
val DB_NAME = "payments"
|
||||
val CURRENT_VERSION = 1
|
||||
|
||||
using(sqlite.createStatement()) { statement =>
|
||||
require(getVersion(statement, DB_NAME, CURRENT_VERSION) == CURRENT_VERSION) // there is only one version currently deployed
|
||||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS payments (payment_hash BLOB NOT NULL PRIMARY KEY, amount_msat INTEGER NOT NULL, timestamp INTEGER NOT NULL)")
|
||||
}
|
||||
|
||||
|
|
|
@ -22,13 +22,17 @@ import java.sql.Connection
|
|||
import fr.acinq.bitcoin.Crypto
|
||||
import fr.acinq.bitcoin.Crypto.PublicKey
|
||||
import fr.acinq.eclair.db.PeersDb
|
||||
import fr.acinq.eclair.db.sqlite.SqliteUtils.using
|
||||
import fr.acinq.eclair.db.sqlite.SqliteUtils.{getVersion, using}
|
||||
import fr.acinq.eclair.wire.LightningMessageCodecs.socketaddress
|
||||
import scodec.bits.BitVector
|
||||
|
||||
class SqlitePeersDb(sqlite: Connection) extends PeersDb {
|
||||
|
||||
val DB_NAME = "peers"
|
||||
val CURRENT_VERSION = 1
|
||||
|
||||
using(sqlite.createStatement()) { statement =>
|
||||
require(getVersion(statement, DB_NAME, CURRENT_VERSION) == CURRENT_VERSION) // there is only one version currently deployed
|
||||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS peers (node_id BLOB NOT NULL PRIMARY KEY, data BLOB NOT NULL)")
|
||||
}
|
||||
|
||||
|
|
|
@ -21,12 +21,16 @@ import java.sql.Connection
|
|||
import fr.acinq.bitcoin.BinaryData
|
||||
import fr.acinq.eclair.channel.Command
|
||||
import fr.acinq.eclair.db.PendingRelayDb
|
||||
import fr.acinq.eclair.db.sqlite.SqliteUtils.{codecSequence, using}
|
||||
import fr.acinq.eclair.db.sqlite.SqliteUtils.{codecSequence, getVersion, using}
|
||||
import fr.acinq.eclair.wire.CommandCodecs.cmdCodec
|
||||
|
||||
class SqlitePendingRelayDb(sqlite: Connection) extends PendingRelayDb {
|
||||
|
||||
val DB_NAME = "pending_relay"
|
||||
val CURRENT_VERSION = 1
|
||||
|
||||
using(sqlite.createStatement()) { statement =>
|
||||
require(getVersion(statement, DB_NAME, CURRENT_VERSION) == CURRENT_VERSION) // there is only one version currently deployed
|
||||
// note: should we use a foreign key to local_channels table here?
|
||||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS pending_relay (channel_id BLOB NOT NULL, htlc_id INTEGER NOT NULL, data BLOB NOT NULL, PRIMARY KEY(channel_id, htlc_id))")
|
||||
}
|
||||
|
|
|
@ -39,6 +39,24 @@ object SqliteUtils {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Several logical databases (channels, network, peers) may be stored in the same physical sqlite database.
|
||||
* We keep track of their respective version using a dedicated table.
|
||||
*
|
||||
* @param statement
|
||||
* @param db_name
|
||||
* @param currentVersion
|
||||
* @return
|
||||
*/
|
||||
def getVersion(statement: Statement, db_name: String, currentVersion: Int): Int = {
|
||||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS versions (db_name TEXT NOT NULL PRIMARY KEY, version INTEGER NOT NULL)")
|
||||
// if there was no version for the current db, then insert the current version
|
||||
statement.executeUpdate(s"INSERT OR IGNORE INTO versions VALUES ('$db_name', $currentVersion)")
|
||||
// if there was a previous version installed, this will return a different value from current version
|
||||
val res = statement.executeQuery(s"SELECT version FROM versions WHERE db_name='$db_name'")
|
||||
res.getInt("version")
|
||||
}
|
||||
|
||||
/**
|
||||
* This helper assumes that there is a "data" column available, decodable with the provided codec
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue