refactor: streamline LNBits migration SQL statements

- Updated the migration script for LNBits configuration to use an array for SQL statements, improving readability and maintainability.
- Consolidated the insertion and deletion operations for user configuration related to LNBits and Lightning Network wallet options.
This commit is contained in:
padreug 2025-09-12 21:07:59 +02:00
parent 67f62169d1
commit abe45c49f1

View file

@ -1,36 +1,34 @@
const db = require('./db') const db = require('./db')
exports.up = function (next) { exports.up = function (next) {
const sql = ` const sql = [
INSERT INTO user_config (name, display_name, type, data_type, config_type, enabled, secret, options) `INSERT INTO user_config (name, display_name, type, data_type, config_type, enabled, secret, options)
VALUES VALUES
('lnbitsEndpoint', 'LNBits Server URL', 'text', 'string', 'wallets', false, false, null), ('lnbitsEndpoint', 'LNBits Server URL', 'text', 'string', 'wallets', false, false, null),
('lnbitsAdminKey', 'LNBits Admin Key', 'text', 'string', 'wallets', false, true, null) ('lnbitsAdminKey', 'LNBits Admin Key', 'text', 'string', 'wallets', false, true, null)
ON CONFLICT (name) DO NOTHING; ON CONFLICT (name) DO NOTHING`,
-- Add LNBits as a valid wallet option for Lightning Network `INSERT INTO user_config (name, display_name, type, data_type, config_type, enabled, secret, options)
INSERT INTO user_config (name, display_name, type, data_type, config_type, enabled, secret, options) VALUES
VALUES ('LN_wallet', 'Lightning Network Wallet', 'text', 'string', 'wallets', true, false,
('LN_wallet', 'Lightning Network Wallet', 'text', 'string', 'wallets', true, false, '[{"code": "lnbits", "display": "LNBits"}, {"code": "galoy", "display": "Galoy (Blink)"}, {"code": "bitcoind", "display": "Bitcoin Core"}]')
'[{"code": "lnbits", "display": "LNBits"}, {"code": "galoy", "display": "Galoy (Blink)"}, {"code": "bitcoind", "display": "Bitcoin Core"}]') ON CONFLICT (name)
ON CONFLICT (name) DO UPDATE SET options = EXCLUDED.options
DO UPDATE SET options = EXCLUDED.options WHERE user_config.options NOT LIKE '%lnbits%'`
WHERE user_config.options NOT LIKE '%lnbits%'; ]
`
db.multi(sql, next) db.multi(sql, next)
} }
exports.down = function (next) { exports.down = function (next) {
const sql = ` const sql = [
DELETE FROM user_config `DELETE FROM user_config
WHERE name IN ('lnbitsEndpoint', 'lnbitsAdminKey'); WHERE name IN ('lnbitsEndpoint', 'lnbitsAdminKey')`,
-- Remove LNBits from wallet options `UPDATE user_config
UPDATE user_config SET options = REPLACE(options, ', {"code": "lnbits", "display": "LNBits"}', '')
SET options = REPLACE(options, ', {"code": "lnbits", "display": "LNBits"}', '') WHERE name = 'LN_wallet'`
WHERE name = 'LN_wallet'; ]
`
db.multi(sql, next) db.multi(sql, next)
} }