Skip to content

Commit 1c41bad

Browse files
committed
Obtain foreign key constraint name from postgres
This is the only foreign key constraint in our DB, so we can safely simply look at the first result.
1 parent 9a3a0d1 commit 1c41bad

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

eclair-core/src/main/scala/fr/acinq/eclair/db/pg/PgChannelsDb.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ class PgChannelsDb(implicit ds: DataSource, lock: PgLock) extends ChannelsDb wit
5656
def migration1112(statement: Statement): Unit = {
5757
// We start by dropping for foreign key constraint on htlc_infos, otherwise we won't be able to move recently
5858
// closed channels to a different table.
59-
statement.executeUpdate("ALTER TABLE local.htlc_infos DROP CONSTRAINT htlc_infos_channel_id_fkey")
59+
statement.executeQuery("SELECT conname FROM pg_catalog.pg_constraint WHERE contype = 'f'").map(rs => rs.getString("conname")).headOption match {
60+
case Some(foreignKeyConstraint) => statement.executeUpdate(s"ALTER TABLE local.htlc_infos DROP CONSTRAINT $foreignKeyConstraint")
61+
case None => logger.warn("couldn't find foreign key constraint for htlc_infos table: DB migration may fail")
62+
}
6063
// We can now move closed channels to a dedicated table.
6164
statement.executeUpdate("CREATE TABLE local.closed_channels (channel_id TEXT NOT NULL PRIMARY KEY, remote_node_id TEXT NOT NULL, funding_txid TEXT NOT NULL, funding_output_index BIGINT NOT NULL, funding_tx_index BIGINT NOT NULL, funding_key_path TEXT NOT NULL, channel_features TEXT NOT NULL, is_channel_opener BOOLEAN NOT NULL, commitment_format TEXT NOT NULL, announced BOOLEAN NOT NULL, capacity_satoshis BIGINT NOT NULL, closing_txid TEXT NOT NULL, closing_type TEXT NOT NULL, closing_script TEXT NOT NULL, local_balance_msat INTEGER NOT NULL, remote_balance_msat INTEGER NOT NULL, closing_amount_satoshis INTEGER NOT NULL, created_at TIMESTAMP WITH TIME ZONE NOT NULL, closed_at TIMESTAMP WITH TIME ZONE NOT NULL)")
6265
statement.executeUpdate("CREATE INDEX closed_channels_remote_node_id_idx ON local.closed_channels(remote_node_id)")

0 commit comments

Comments
 (0)