-
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 #3522 from LiteFarmOrg/LF-4523/Animals_internal_id…
…entifier_changes LF-4523: Animals internal identifier changes after data updates
- Loading branch information
Showing
3 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
packages/api/db/migration/20241108052304_update_animal_union_batch_internal_id_view.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,63 @@ | ||
/* | ||
* 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.dropView('animal_union_batch_id_view'); | ||
|
||
// Recreate animal_union_batch_id_view VIEW | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
const sqlFilePath = path.join( | ||
__dirname, | ||
'../sql/20241108052304_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) => { | ||
try { | ||
await knex.schema.dropView('animal_union_batch_id_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 down:', error); | ||
throw error; // Rethrow the error to ensure the migration fails | ||
} | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/api/db/sql/20241108052304_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,40 @@ | ||
/* | ||
* 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/>. | ||
*/ | ||
|
||
CREATE VIEW animal_union_batch_id_view AS | ||
SELECT | ||
*, | ||
ROW_NUMBER() OVER (PARTITION BY farm_id ORDER BY created_at, id, batch)::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, id, batch; |
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