Skip to content

Commit

Permalink
Merge pull request #22 from Kpoke/fix/create_raw_capture
Browse files Browse the repository at this point in the history
update raw capture migration
  • Loading branch information
Kpoke authored Jul 25, 2023
2 parents 56f08b5 + 551440f commit 14f179e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
11 changes: 11 additions & 0 deletions v1Tov2Migrations/helper/createRawCapture.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ const createRawCapture = async (tree, treeAttributes, sessionId, trx) => {

if (existingRawCapture) {
console.log('Raw capture already exists', tree.uuid);

// update status
await trx('field_data.raw_capture')
.update({ status })
.where({ id: rawCapture.id });

if (status === 'rejected') {
await trx('treetracker.capture')
.update({ status: 'deleted' })
.where({ id: rawCapture.id });
}
return existingRawCapture;
}

Expand Down
18 changes: 9 additions & 9 deletions v1Tov2Migrations/migrate_approved_captures.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ async function migrate() {
(rc.status = 'approved' and tc.id is null) or
(rc.status != 'approved' and pt.active = true and pt.approved = true)
) AND
rc.reference_id != 2463005
--pt.planting_organization_id = 1642
order by pt.id asc
--offset 0
--limit 10000
rc.reference_id != 2463005
--pt.planting_organization_id = 1642
order by pt.id asc
--offset 0
--limit 10000
`;
const rowCountResult = await knex.select(
knex.raw(`count(1) from (${base_query_string}) as src`),
Expand All @@ -35,10 +35,10 @@ async function migrate() {
}
console.log(`Migrating ${recordCount} records`);

const bar = new ProgressBar('Migrating [:bar] :percent :etas :current/:total (:rate)', {
width: 40,
total: recordCount,
});
const bar = new ProgressBar(
'Migrating [:bar] :percent :etas :current/:total (:rate)',
{ width: 40, total: recordCount },
);

const trx = await knex.transaction();
ws._write = async (rawCapture, enc, next) => {
Expand Down
45 changes: 37 additions & 8 deletions v1Tov2Migrations/migrate_raw_captures.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,39 @@ const createSession = require('./helper/createSession');
async function migrate() {
try {
// filter out trees with invalid planter_id
const base_query_string = `select t.* from public.trees t join planter p on t.planter_id = p.id left join field_data.raw_capture r on t.uuid = r.id::text
where r.id is null and t.image_url is not null and (p.email is not null or p.phone is not null)
and t.planter_id NOT IN (825,1253,1214)
order by t.id asc
const base_query_string = `
select
t.*
from
public.trees t
join planter p on t.planter_id = p.id
left join field_data.raw_capture r on t.uuid = r.id :: text
where
(
r.id is null
and t.image_url is not null
and (
p.email is not null
or p.phone is not null
)
and t.planter_id NOT IN (825, 1253, 1214)
)
or (
r.id is not null and
(
(
t.active = false
and r.status != 'rejected'
)
or (
t.active = true
and t.approved = true
and r.status != 'approved'
)
)
)
order by
t.id asc
`;
const rowCountResult = await knex.select(
knex.raw(`count(1) from (${base_query_string}) as src`),
Expand All @@ -27,10 +56,10 @@ async function migrate() {
}
console.log(`Migrating ${recordCount} records`);

const bar = new ProgressBar('Migrating [:bar] :percent :etas :current/:total (:rate)', {
width: 40,
total: recordCount,
});
const bar = new ProgressBar(
'Migrating [:bar] :percent :etas :current/:total (:rate)',
{ width: 40, total: recordCount },
);

const trx = await knex.transaction();
ws._write = async (tree, enc, next) => {
Expand Down

0 comments on commit 14f179e

Please sign in to comment.