A lot of small fixes, improvements on startup tests, migration

This commit is contained in:
kexkey
2021-11-27 00:31:18 -05:00
parent bfd9261890
commit ef4cd781da
17 changed files with 178 additions and 82 deletions

View File

@@ -136,8 +136,8 @@ CREATE TABLE cyphernode_props (
CREATE INDEX idx_cp_property ON cyphernode_props (property);
CREATE UNIQUE INDEX idx_cp_propval ON cyphernode_props (property, value);
INSERT INTO cyphernode_props (property, value) VALUES ('version', '0.1');
INSERT INTO cyphernode_props (property, value) VALUES ('pay_index', '0');
INSERT INTO cyphernode_props (id, property, value) VALUES (1, 'version', '0.1');
INSERT INTO cyphernode_props (id, property, value) VALUES (2, 'pay_index', '0');
CREATE TABLE ln_invoice (
id SERIAL PRIMARY KEY,

View File

@@ -1,14 +1,19 @@
#!/bin/sh
echo "Checking for labels for watched addresses support in DB..."
. ./trace.sh
trace "[sqlmigrate20210808_0.7.0-0.8.0.sh] Checking for labels for watched addresses support in DB..."
count=$(sqlite3 $DB_FILE "select count(*) from pragma_table_info('watching') where name='label'")
if [ "${count}" -eq "0" ]; then
# label not there, we have to migrate
echo "Migrating database for labels for watched addresses support..."
echo "Backing up current DB..."
# label not there, we have to migrate
trace "[sqlmigrate20210808_0.7.0-0.8.0.sh] Migrating database for labels for watched addresses support..."
trace "[sqlmigrate20210808_0.7.0-0.8.0.sh] Backing up current DB..."
cp $DB_FILE $DB_FILE-sqlmigrate20210808_0.7.0-0.8.0
echo "Altering DB..."
cat sqlmigrate20210808_0.7.0-0.8.0.sql | sqlite3 $DB_FILE
trace "[sqlmigrate20210808_0.7.0-0.8.0.sh] Altering DB..."
cat sqlmigrate20210808_0.7.0-0.8.0.sql | sqlite3 $DB_FILE
returncode=$?
trace_rc ${returncode}
exit ${returncode}
else
echo "Database labels for watched addresses support migration already done, skipping!"
trace "[sqlmigrate20210808_0.7.0-0.8.0.sh] Database labels for watched addresses support migration already done, skipping!"
fi

View File

@@ -1,14 +1,19 @@
#!/bin/sh
echo "Checking for new indexes in DB..."
. ./trace.sh
trace "[sqlmigrate20210928_0.7.0-0.8.0.sh] Checking for new indexes in DB..."
sqlite3 $DB_FILE ".indexes" | grep "idx_watching_watching" > /dev/null
if [ "$?" -eq "1" ]; then
# idx_watching_watching index not found
echo "Migrating database with new indexes..."
echo "Backing up current DB..."
cp $DB_FILE $DB_FILE-sqlmigrate20210928_0.7.0-0.8.0
echo "Altering DB..."
trace "[sqlmigrate20210928_0.7.0-0.8.0.sh] Migrating database with new indexes..."
trace "[sqlmigrate20210928_0.7.0-0.8.0.sh] Backing up current DB..."
cp $DB_FILE $DB_FILE-sqlmigrate20210928_0.7.0-0.8.0
trace "[sqlmigrate20210928_0.7.0-0.8.0.sh] Altering DB..."
cat sqlmigrate20210928_0.7.0-0.8.0.sql | sqlite3 $DB_FILE
returncode=$?
trace_rc ${returncode}
exit ${returncode}
else
echo "New indexes migration already done, skipping!"
trace "[sqlmigrate20210928_0.7.0-0.8.0.sh] New indexes migration already done, skipping!"
fi

View File

@@ -1,26 +1,39 @@
#!/bin/sh
date
echo "Waiting for postgres to be ready..."
(while true ; do psql -h postgres -U cyphernode -c "select 1;" ; [ "$?" -eq "0" ] && break ; sleep 10; done) &
wait
. ./trace.sh
echo "Checking if postgres is setup..."
trace "[sqlmigrate20211105_0.7.0-0.8.0.sh] Waiting for postgres to be ready..."
while true ; do psql -h postgres -U cyphernode -c "select 1;" ; [ "$?" -eq "0" ] && break ; sleep 10; done
trace "[sqlmigrate20211105_0.7.0-0.8.0.sh] Checking if postgres is setup..."
psql -h postgres -U cyphernode -c "\d" | grep "cyphernode_props" > /dev/null
if [ "$?" -eq "1" ]; then
# if cyphernode_props table doesn't exist, it's probably because database hasn't been setup yet
echo "Creating postgres database..."
trace "[sqlmigrate20211105_0.7.0-0.8.0.sh] Creating postgres database..."
psql -h postgres -f cyphernode.postgresql -U cyphernode
returncode=$?
trace_rc ${returncode}
[ "${returncode}" -eq "0" ] || exit ${returncode}
else
trace "[sqlmigrate20211105_0.7.0-0.8.0.sh] PostgreSQL database already created, skipping!"
fi
date
echo "Extracting and converting sqlite3 data..."
date
trace "[sqlmigrate20211105_0.7.0-0.8.0.sh] Checking if postgres is loaded/imported..."
lastval=$(psql -qAtX -h postgres -U cyphernode -c "select last_value from pg_sequences where sequencename='cyphernode_props_id_seq'")
returncode=$?
if [ -z "${lastval}" ] || [ "${lastval}" -lt "2" ]; then
# if cyphernode_props_id_seq isn't set, it's probably because database hasn't been loaded/imported yet
trace "[sqlmigrate20211105_0.7.0-0.8.0.sh] Extracting and converting sqlite3 data..."
cat sqlmigrate20211105_0.7.0-0.8.0_sqlite3-extract.sql | sqlite3 $DB_FILE
returncode=$?
trace_rc ${returncode}
[ "${returncode}" -eq "0" ] || exit ${returncode}
trace "[sqlmigrate20211105_0.7.0-0.8.0.sh] Creating import file for postgres..."
mv sqlmigrate20211105_0.7.0-0.8.0_sqlite3-extracted-data.sql ${DB_PATH}/
sed -ie 's/^\(INSERT.*\);$/\1 ON CONFLICT DO NOTHING;/g' ${DB_PATH}/sqlmigrate20211105_0.7.0-0.8.0_sqlite3-extracted-data.sql
date
echo "...appending postgresql sequences..."
trace "[sqlmigrate20211105_0.7.0-0.8.0.sh] Appending postgresql sequence creation..."
echo "
select setval('cyphernode_props_id_seq', (SELECT MAX(id) FROM cyphernode_props));
select setval('ln_invoice_id_seq', (SELECT MAX(id) FROM ln_invoice));
@@ -34,10 +47,11 @@ select setval('batcher_id_seq', (SELECT MAX(id) FROM batcher));
commit;
" >> ${DB_PATH}/sqlmigrate20211105_0.7.0-0.8.0_sqlite3-extracted-data.sql
date
echo "Importing sqlite3 data into postgresql..."
psql -h postgres -f ${DB_PATH}/sqlmigrate20211105_0.7.0-0.8.0_sqlite3-extracted-data.sql -U cyphernode
date
trace "[sqlmigrate20211105_0.7.0-0.8.0.sh] Importing sqlite3 data into postgresql..."
psql -v ON_ERROR_STOP=on -h postgres -f ${DB_PATH}/sqlmigrate20211105_0.7.0-0.8.0_sqlite3-extracted-data.sql -U cyphernode
returncode=$?
trace_rc ${returncode}
[ "${returncode}" -eq "0" ] || exit ${returncode}
else
echo "New indexes migration already done, skipping!"
trace "[sqlmigrate20211105_0.7.0-0.8.0.sh] PostgreSQL database already loaded, skipping!"
fi

View File

@@ -17,11 +17,10 @@ select id,address,amount,tx_id,inserted_ts,webhook_url,case when calledback=1 th
select id,txid,case when watching=1 then 'TRUE' else 'FALSE' end as watching,callback1conf,case when calledback1conf=1 then 'TRUE' else 'FALSE' end as calledback1conf,callbackxconf,case when calledbackxconf=1 then 'TRUE' else 'FALSE' end as calledbackxconf,nbxconf,inserted_ts from watching_by_txid;
.mode insert stamp
select id,hash,callbackUrl,case when requested=1 then 'TRUE' else 'FALSE' end as requested,case when upgraded=1 then 'TRUE' else 'FALSE' end as upgraded,case when calledback=1 then 'TRUE' else 'FALSE' end as calledback,inserted_ts from stamp;
.mode insert ln_invoice
select id,label,bolt11,payment_hash,msatoshi,status,pay_index,msatoshi_received,paid_at,description,expires_at,callback_url,case when calledback=1 then 'TRUE' else 'FALSE' end as calledback,case when callback_failed=1 then 'TRUE' else 'FALSE' end as callback_failed,inserted_ts from ln_invoice;
-- cyphernode_props rows were already inserted in db creation, let's update them here
.headers off
.mode list cyphernode_props
select 'update cyphernode_props set value=''' || value || ''', inserted_ts=''' || inserted_ts || ''' where id=' || id || ';' from cyphernode_props;
.headers on
.mode insert ln_invoice
select id,label,bolt11,payment_hash,msatoshi,status,pay_index,msatoshi_received,paid_at,description,expires_at,callback_url,case when calledback=1 then 'TRUE' else 'FALSE' end as calledback,case when callback_failed=1 then 'TRUE' else 'FALSE' end as callback_failed,inserted_ts from ln_invoice;
.quit

View File

@@ -11,7 +11,7 @@ compute_fees() {
trace "[compute_fees] pruned=${pruned}"
# We want null instead of 0.00000000 in this case.
echo "null"
exit 0
return
fi
local txid=${1}

View File

@@ -771,9 +771,10 @@ main() {
fi
done
trace "[main] exiting"
return 0
return ${returncode}
}
main
returncode=$?
trace "[requesthandler] exiting"
exit $?
exit ${returncode}

View File

@@ -1,5 +1,7 @@
#!/bin/sh
. ./trace.sh
trim() {
echo -e "$1" | sed -e 's/^[[:space:]]*//' | sed -e 's/[[:space:]]*$//'
}
@@ -7,12 +9,12 @@ trim() {
createCurlConfig() {
if [[ ''$1 == '' ]]; then
echo "Missing file name: Check your *_BTC_NODE_RPC_CFG"
trace "[startproxy] Missing file name: Check your *_BTC_NODE_RPC_CFG"
return
fi
if [[ ''$2 == '' ]]; then
echo "Missing content: Check your *_BTC_NODE_RPC_USER"
trace "[startproxy] Missing content: Check your *_BTC_NODE_RPC_USER"
return
fi
@@ -21,17 +23,45 @@ createCurlConfig() {
}
if [ -e ${DB_PATH}/.dbfailed ]; then
touch /container_monitor/proxy_dbfailed
trace "[startproxy] A previous database creation/migration failed. Stopping."
trace "[startproxy] A file called .dbfailed has been created. Fix the migration errors, remove .dbfailed and retry."
trace "[startproxy] Exiting."
sleep 30
exit 1
else
rm -f /container_monitor/proxy_dbfailed
fi
if [ ! -e ${DB_FILE} ]; then
echo "DB not found, creating..."
trace "[startproxy] DB not found, creating..."
cat cyphernode.sql | sqlite3 $DB_FILE
psql -h postgres -f cyphernode.postgresql -U cyphernode
returncode=$?
trace_rc ${returncode}
else
echo "DB found, migrating..."
trace "[startproxy] DB found, migrating..."
for script in sqlmigrate*.sh; do
sh $script
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -ne "0" ]; then
break
fi
done
fi
if [ "${returncode}" -ne "0" ]; then
touch ${DB_PATH}/.dbfailed
touch /container_monitor/proxy_dbfailed
trace "[startproxy] Database creation/migration failed. Stopping."
trace "[startproxy] A file called .dbfailed has been created in your proxy datapath. Fix the migration errors, remove .dbfailed and retry."
trace "[startproxy] Exiting."
sleep 30
exit ${returncode}
fi
chmod 0600 $DB_FILE
createCurlConfig ${WATCHER_BTC_NODE_RPC_CFG} ${WATCHER_BTC_NODE_RPC_USER}

View File

@@ -1,7 +1,6 @@
#!/bin/sh
trace()
{
trace() {
if [ -n "${TRACING}" ]; then
local str="$(date -Is) $$ ${1}"
echo "${str}" 1>&2
@@ -9,8 +8,7 @@ trace()
fi
}
trace_rc()
{
trace_rc() {
if [ -n "${TRACING}" ]; then
local str="$(date -Is) $$ Last return code: ${1}"
echo "${str}" 1>&2