-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch integration into POC-switch-to-pnpm-workspaces
- Loading branch information
Showing
38 changed files
with
828 additions
and
66 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
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,42 @@ | ||
/* | ||
* 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 AnimalMovementPurpose extends Model { | ||
static get tableName() { | ||
return 'animal_movement_purpose'; | ||
} | ||
|
||
static get idColumn() { | ||
return 'id'; | ||
} | ||
|
||
// Optional JSON schema. This is not the database schema! Nothing is generated | ||
// based on this. This is only used for validation. Whenever a model instance | ||
// is created it is checked against this schema. http://json-schema.org/. | ||
static get jsonSchema() { | ||
return { | ||
type: 'object', | ||
required: ['key'], | ||
properties: { | ||
id: { type: 'integer' }, | ||
key: { type: 'string' }, | ||
}, | ||
additionalProperties: false, | ||
}; | ||
} | ||
} | ||
|
||
export default AnimalMovementPurpose; |
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,65 @@ | ||
/* | ||
* 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'; | ||
import TaskModel from './taskModel.js'; | ||
import AnimalMovementTaskPurposeRelationshipModel from './animalMovementTaskPurposeRelationshipModel.js'; | ||
|
||
class AnimalMovementTask extends Model { | ||
static get tableName() { | ||
return 'animal_movement_task'; | ||
} | ||
|
||
static get idColumn() { | ||
return 'task_id'; | ||
} | ||
|
||
// Optional JSON schema. This is not the database schema! Nothing is generated | ||
// based on this. This is only used for validation. Whenever a model instance | ||
// is created it is checked against this schema. http://json-schema.org/. | ||
static get jsonSchema() { | ||
return { | ||
type: 'object', | ||
required: [], | ||
properties: { | ||
task_id: { type: 'integer' }, | ||
}, | ||
additionalProperties: false, | ||
}; | ||
} | ||
|
||
static get relationMappings() { | ||
return { | ||
task: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: TaskModel, | ||
join: { | ||
from: 'animal_movement_task.task_id', | ||
to: 'task.task_id', | ||
}, | ||
}, | ||
purpose_relationships: { | ||
relation: Model.HasManyRelation, | ||
modelClass: AnimalMovementTaskPurposeRelationshipModel, | ||
join: { | ||
from: 'animal_movement_task.task_id', | ||
to: 'animal_movement_task_purpose_relationship.task_id', | ||
}, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
export default AnimalMovementTask; |
44 changes: 44 additions & 0 deletions
44
packages/api/src/models/animalMovementTaskPurposeRelationshipModel.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,44 @@ | ||
/* | ||
* 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 AnimalMovementTaskPurposeRelationship extends Model { | ||
static get tableName() { | ||
return 'animal_movement_task_purpose_relationship'; | ||
} | ||
|
||
static get idColumn() { | ||
return ['task_id', 'purpose_id']; | ||
} | ||
|
||
// Optional JSON schema. This is not the database schema! Nothing is generated | ||
// based on this. This is only used for validation. Whenever a model instance | ||
// is created it is checked against this schema. http://json-schema.org/. | ||
static get jsonSchema() { | ||
return { | ||
type: 'object', | ||
required: ['task_id', 'purpose_id'], | ||
properties: { | ||
task_id: { type: 'integer' }, | ||
purpose_id: { type: 'integer' }, | ||
other_purpose: { type: ['string', 'null'], minLength: 1, maxLength: 255 }, | ||
}, | ||
additionalProperties: false, | ||
}; | ||
} | ||
} | ||
|
||
export default AnimalMovementTaskPurposeRelationship; |
Oops, something went wrong.