diff --git a/v1Tov2Migrations/migrate_planter_info.js b/v1Tov2Migrations/migrate_planter_info.js index 3011e96..b5b65d2 100644 --- a/v1Tov2Migrations/migrate_planter_info.js +++ b/v1Tov2Migrations/migrate_planter_info.js @@ -10,8 +10,17 @@ const createWalletRegistrations = require('./helper/createWalletRegistrations'); async function migrate() { const base_query_string = `SELECT pp.* FROM public.planter pp left join treetracker.grower_account tg on pp.grower_account_uuid = tg.id - where (pp.email is not null or pp.phone is not null) - and tg.id is null order by organization_id asc + where + ( + ( pp.email is not null or pp.phone is not null ) + and + tg.id is null + ) + or + ( + pp.organization_id is not null and tg.organization_id is null + ) + order by organization_id asc `; const rowCountResult = await knex.select( @@ -34,6 +43,23 @@ async function migrate() { ws._write = async (planter, enc, next) => { try { if (planter.grower_account_uuid) { + // update organization_id + if (planter.organization_id) { + const org = await trx + .select() + .table('entity') + .where({ id: planter.organization_id }) + .first(); + + const organization_id = org.stakeholder_uuid; + + await trx('treetracker.grower_account') + .where({ id: planter.grower_account_uuid }) + .update({ + organization_id, + }); + } + bar.tick(); if (bar.complete) { await trx.commit();