-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3125 from LiteFarmOrg/LF-4081/Support_animal_inte…
…rnal_identifier LF-4081: Support animal internal identifier
- Loading branch information
Showing
14 changed files
with
394 additions
and
61 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...n/20240207214919_create_animal_union_batch_internal_id_view_modify_animal_batch_tables.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2024 LiteFarm.org | ||
* This file is part of LiteFarm. | ||
* | ||
* LiteFarm is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* LiteFarm is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { fileURLToPath } from 'url'; | ||
import path, { dirname } from 'path'; | ||
import fs from 'fs'; | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
export const up = async function (knex) { | ||
try { | ||
await knex.schema.alterTable('animal', (table) => { | ||
table.string('photo_url'); | ||
table.dropChecks(['name_identifier_check']); | ||
}); | ||
|
||
await knex.schema.alterTable('animal_batch', (table) => { | ||
table.string('photo_url'); | ||
table.string('name').nullable().alter(); | ||
}); | ||
|
||
// Create animal_union_batch_id_view VIEW | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
const sqlFilePath = path.join( | ||
__dirname, | ||
'../sql/20240207214919_animal_union_batch_id_view.sql', | ||
); | ||
const sqlQuery = fs.readFileSync(sqlFilePath).toString(); | ||
await knex.raw(sqlQuery); | ||
} catch (error) { | ||
console.error('Error in migration up:', error); | ||
throw error; // Rethrow the error to ensure the migration fails | ||
} | ||
}; | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
export const down = async (knex) => { | ||
await knex.schema.dropView('animal_union_batch_id_view'); | ||
|
||
await knex.schema.alterTable('animal', (table) => { | ||
table.dropColumn('photo_url'); | ||
// ?? does not work in alterTable | ||
table.check('name IS NOT NULL OR identifier IS NOT NULL', [], 'name_identifier_check'); | ||
}); | ||
|
||
await knex.schema.alterTable('animal_batch', (table) => { | ||
table.dropColumn('photo_url'); | ||
table.string('name').notNullable().alter(); | ||
}); | ||
}; |
25 changes: 25 additions & 0 deletions
25
packages/api/db/sql/20240207214919_animal_union_batch_id_view.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
CREATE VIEW animal_union_batch_id_view AS | ||
SELECT | ||
*, | ||
ROW_NUMBER() OVER (PARTITION BY farm_id ORDER BY created_at)::INTEGER AS internal_identifier | ||
FROM ( | ||
SELECT | ||
id, | ||
farm_id, | ||
FALSE AS batch, | ||
created_at | ||
FROM | ||
animal a | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
id, | ||
farm_id, | ||
TRUE AS batch, | ||
created_at | ||
FROM | ||
animal_batch ab | ||
) animal_union_batch_id_view | ||
ORDER BY | ||
created_at; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2024 LiteFarm.org | ||
* This file is part of LiteFarm. | ||
* | ||
* LiteFarm is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* LiteFarm is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Model from './baseFormatModel.js'; | ||
|
||
class animalUnionBatchIdViewModel extends Model { | ||
static get tableName() { | ||
return 'animal_union_batch_id_view'; | ||
} | ||
} | ||
|
||
export default animalUnionBatchIdViewModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2024 LiteFarm.org | ||
* This file is part of LiteFarm. | ||
* | ||
* LiteFarm is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* LiteFarm is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import knex from './knex.js'; | ||
|
||
/** | ||
* Assigns internal identifiers to records. | ||
* @param {Array<Object>} records - The array of animals or animal batches to which internal identifiers will be assigned. | ||
* Each record is expected to contain an 'id' property. | ||
* @param {string} kind - The kind of records being processed ('animal' or 'batch'). | ||
*/ | ||
export const assignInternalIdentifiers = async (records, kind) => { | ||
await Promise.all( | ||
records.map(async (record) => { | ||
const [internalIdentifier] = await knex('animal_union_batch_id_view') | ||
.pluck('internal_identifier') | ||
.where('id', record.id) | ||
.andWhere({ batch: kind === 'batch' }); | ||
|
||
record.internal_identifier = internalIdentifier; | ||
}), | ||
); | ||
}; |
Oops, something went wrong.