How-to: Fix references of old domain names after migration

When you move data to a new server with a new domain name, old forms cannot be deployed and/or data cannot be submitted anymore. Users face this issue when deploying old forms:

Your form cannot be deployed because it contains errors:
HTTPConnectionPool(host=‘…’): Max retries exceeded with url: /api/v1/forms/1 (Caused by NewConnectionError(‘: Failed to establish a new connection: [Errno -2] Name or service not know’, ))

It happens because the domain name is saved in project metadata at its creation. It’s not overridden after each deployment.

Update PostgreSQL (for KPI and KoBoCaT)

Enter PostgreSQL container and update _deployment_data field

user@computer:~/kobo-install$ ./run.py -cb exec postgres bash
root@postgres:/# psql -U <user> -d <kpi_database>
<kpi_database>=# UPDATE kpi_asset set _deployment_data = REPLACE(_deployment_data::text, '<string_to_replace>', '<new_string>')::jsonb;
<kpi_database>=\q
root@postgres:/# exit

Example 1:

  • old domain name: mydomain.tld
  • new domain name: my-new-domain.tld
  • PostgreSQL user: kobo
  • KPI database: koboform
user@computer:~/kobo-install$ ./run.py -cb exec postgres bash
root@postgres:/# psql -U kobo -d koboform
koboform=# UPDATE kpi_asset set _deployment_data = REPLACE(_deployment_data::text, '.mydomain.tld', '.my-new-domain.tld')::jsonb;
koboform=# UPDATE kpi_asset set _deployment_data = REPLACE(_deployment_data::text, '.mydomain.internal', '.my-new-domain.internal')::jsonb;
koboform=# UPDATE kpi_asset set _deployment_data = REPLACE(_deployment_data::text, '.docker.internal', '.my-new-domain.internal')::jsonb;
koboform=\q
root@postgres:/# exit

Notes:

  1. The leading dot in string is important.
  2. Last query is optional. Run it only if you migrate from the old database (prior to v2.x)

Example 2:

  • old subdomain name: kf.mydomain.tld
  • new subdomain name: koboform.mydomain.tld
  • PostgreSQL user: kobo
  • KPI database: koboform
user@computer:~/kobo-install$ ./run.py -cb exec postgres bash
root@postgres:/# psql -U kobo -d koboform
koboform=# UPDATE kpi_asset set _deployment_data = REPLACE(_deployment_data::text, 'kf.mydomain.tld', 'koboform.mydomain.tld')::jsonb;
koboform=# UPDATE kpi_asset set _deployment_data = REPLACE(_deployment_data::text, 'kf.mydomain.internal', 'koboform.mydomain.internal')::jsonb;
koboform=# UPDATE kpi_asset set _deployment_data = REPLACE(_deployment_data::text, 'kf.docker.internal', 'koboform.mydomain.internal')::jsonb;
koboform=\q
root@postgres:/# exit

Notes:
2. Last query is optional. Run it only if you migrate from the old database (prior to v2.x)

WARNING: These SQL queries are pretty expensive on database. It will take a while to run on big database.

Update redis (for Enketo Express)

Enter redis_main container, install vim, copy the lua script and run it.

user@computer:~/kobo-install$ ./run.py -cb exec redis_main bash
root@redis_main:/# apt update && apt install vim
root@redis_main:/# vim /tmp/search_and_replace.lua

Copy and paste this script. (Adapt <string_to_replace> and <new_string> accordingly)

local output = {}
local ee_ids = redis.call('keys', 'id:*')
for _, key in ipairs(ee_ids) do
    local url = redis.call('hget', key, 'openRosaServer')
    local fixed_url = string.gsub(url, '<string_to_replace>', '<new_string>')
    if url ~= fixed_url then
        redis.call('hset', key, 'openRosaServer', fixed_url)
        table.insert(output, key .. ' ' .. fixed_url)
    end
end
return output

In vim,

  • Press Escape
  • Type :set paste
  • Press Enter
  • Press i
  • Paste the script above
  • Press Escape
  • Type :wq
  • Press Enter

Run the script

root@redis_main:/# redis-cli -a <redis_password> --eval /tmp/search_and_replace.lua 0
6 Likes