-
Notifications
You must be signed in to change notification settings - Fork 78
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 #3373 from LiteFarmOrg/LF-4168-add-necessary-colum…
…ns-for-animal-creation-to-animal-batch-tables-and-update-ap-is Lf 4168 add necessary columns for animal creation to animal batch tables and update ap is
- Loading branch information
Showing
22 changed files
with
445 additions
and
58 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
packages/api/db/migration/20240809121112_add-animal-and-batch-creation-details.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,110 @@ | ||
/* | ||
* Copyright (c) 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/>. | ||
*/ | ||
|
||
export const up = async function (knex) { | ||
const otherUse = await knex('animal_use').select('*').where({ key: 'OTHER' }).first(); | ||
const otherIdentifierType = await knex('animal_identifier_type') | ||
.select('*') | ||
.where({ key: 'OTHER' }) | ||
.first(); | ||
|
||
/*---------------------------------------- | ||
Update animal with new details | ||
----------------------------------------*/ | ||
await knex.schema.alterTable('animal', (table) => { | ||
table | ||
.integer('identifier_type_id') | ||
.references('id') | ||
.inTable('animal_identifier_type') | ||
.nullable(); | ||
table.string('identifier_type_other').nullable(); | ||
table.check( | ||
`(identifier_type_other IS NOT NULL AND identifier_type_id = ${otherIdentifierType.id}) OR (identifier_type_other IS NULL)`, | ||
[], | ||
'identifier_type_other_id_check', | ||
); | ||
table | ||
.enu('organic_status', ['Non-Organic', 'Transitional', 'Organic']) | ||
.notNullable() | ||
.defaultTo('Non-Organic'); | ||
table.string('supplier').nullable(); | ||
table.float('price').unsigned().nullable(); | ||
}); | ||
|
||
await knex.schema.createTable('animal_use_relationship', (table) => { | ||
table.integer('animal_id').references('id').inTable('animal').notNullable(); | ||
table.integer('use_id').references('id').inTable('animal_use').notNullable(); | ||
table.primary(['animal_id', 'use_id']); | ||
table.string('other_use'); | ||
table.check( | ||
`(other_use IS NOT NULL AND use_id = ${otherUse.id}) OR (other_use IS NULL)`, | ||
[], | ||
'other_use_id_check', | ||
); | ||
}); | ||
|
||
/*---------------------------------------- | ||
Update animal batch with new details | ||
----------------------------------------*/ | ||
await knex.schema.alterTable('animal_batch', (table) => { | ||
table | ||
.enu('organic_status', ['Non-Organic', 'Transitional', 'Organic']) | ||
.notNullable() | ||
.defaultTo('Non-Organic'); | ||
table.string('supplier').nullable(); | ||
table.float('price').unsigned().nullable(); | ||
table.string('dam').nullable(); | ||
table.string('sire').nullable(); | ||
}); | ||
|
||
await knex.schema.createTable('animal_batch_use_relationship', (table) => { | ||
table.integer('animal_batch_id').references('id').inTable('animal_batch').notNullable(); | ||
table.integer('use_id').references('id').inTable('animal_use').notNullable(); | ||
table.primary(['animal_batch_id', 'use_id']); | ||
table.string('other_use'); | ||
table.check( | ||
`(other_use IS NOT NULL AND use_id = ${otherUse.id}) OR (other_use IS NULL)`, | ||
[], | ||
'other_use_id_check', | ||
); | ||
}); | ||
}; | ||
|
||
export const down = async function (knex) { | ||
/*---------------------------------------- | ||
Undo: Update animal with new details | ||
----------------------------------------*/ | ||
await knex.schema.alterTable('animal', (table) => { | ||
table.dropChecks(['identifier_type_other_id_check']); | ||
table.dropColumns([ | ||
'identifier_type_id', | ||
'identifier_type_other', | ||
'organic_status', | ||
'supplier', | ||
'price', | ||
]); | ||
}); | ||
|
||
await knex.schema.dropTable('animal_use_relationship'); | ||
|
||
/*---------------------------------------- | ||
Undo: Update animal batch with new details | ||
----------------------------------------*/ | ||
await knex.schema.alterTable('animal_batch', (table) => { | ||
table.dropColumns(['organic_status', 'supplier', 'price', 'dam', 'sire']); | ||
}); | ||
|
||
await knex.schema.dropTable('animal_batch_use_relationship'); | ||
}; |
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
44 changes: 44 additions & 0 deletions
44
packages/api/src/models/animalBatchUseRelationshipModel.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 AnimalBatchUseRelationshipModel extends Model { | ||
static get tableName() { | ||
return 'animal_batch_use_relationship'; | ||
} | ||
|
||
static get idColumn() { | ||
return ['animal_batch_id', 'use_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: ['animal_batch_id', 'use_id'], | ||
properties: { | ||
animal_batch_id: { type: 'integer' }, | ||
use_id: { type: 'integer' }, | ||
other_use: { type: ['string', 'null'] }, | ||
}, | ||
additionalProperties: false, | ||
}; | ||
} | ||
} | ||
|
||
export default AnimalBatchUseRelationshipModel; |
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,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 AnimalUseRelationshipModel extends Model { | ||
static get tableName() { | ||
return 'animal_use_relationship'; | ||
} | ||
|
||
static get idColumn() { | ||
return ['animal_id', 'use_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: ['animal_id', 'use_id'], | ||
properties: { | ||
animal_id: { type: 'integer' }, | ||
use_id: { type: 'integer' }, | ||
other_use: { type: ['string', 'null'] }, | ||
}, | ||
additionalProperties: false, | ||
}; | ||
} | ||
} | ||
|
||
export default AnimalUseRelationshipModel; |
Oops, something went wrong.