161 lines
7.7 KiB
PHP
161 lines
7.7 KiB
PHP
<?php
|
|
|
|
use humhub\components\Migration;
|
|
|
|
class m260401_210000_initial extends Migration
|
|
{
|
|
public function safeUp()
|
|
{
|
|
$this->safeCreateTable('rescue_animal', [
|
|
'id' => $this->primaryKey(),
|
|
'contentcontainer_id' => $this->integer()->notNull(),
|
|
'animal_uid' => $this->string(190)->notNull(),
|
|
'name' => $this->string(190)->null(),
|
|
'species' => $this->string(120)->null(),
|
|
'breed' => $this->string(120)->null(),
|
|
'sex' => $this->string(32)->null(),
|
|
'status' => $this->string(32)->notNull()->defaultValue('intake'),
|
|
'in_possession' => $this->boolean()->notNull()->defaultValue(1),
|
|
'location_name' => $this->string(120)->null(),
|
|
'city' => $this->string(120)->null(),
|
|
'state' => $this->string(2)->null(),
|
|
'zip' => $this->string(10)->null(),
|
|
'previous_owner_user_id' => $this->integer()->null(),
|
|
'public_summary' => $this->text()->null(),
|
|
'medical_notes' => $this->text()->null(),
|
|
'created_at' => $this->dateTime()->null(),
|
|
'updated_at' => $this->dateTime()->null(),
|
|
]);
|
|
|
|
$this->safeCreateIndex('ux_rescue_animal_uid', 'rescue_animal', 'animal_uid', true);
|
|
$this->safeCreateIndex('idx_rescue_animal_container', 'rescue_animal', 'contentcontainer_id', false);
|
|
$this->safeCreateIndex('idx_rescue_animal_status', 'rescue_animal', 'status', false);
|
|
$this->safeAddForeignKey('fk_rescue_animal_container', 'rescue_animal', 'contentcontainer_id', 'contentcontainer', 'id', 'CASCADE', 'CASCADE');
|
|
|
|
if ($this->db->getSchema()->getTableSchema('user', true) !== null) {
|
|
$this->safeAddForeignKey('fk_rescue_animal_prev_owner', 'rescue_animal', 'previous_owner_user_id', 'user', 'id', 'SET NULL', 'CASCADE');
|
|
}
|
|
|
|
$this->safeCreateTable('rescue_animal_transfer', [
|
|
'id' => $this->primaryKey(),
|
|
'animal_id' => $this->integer()->notNull(),
|
|
'from_contentcontainer_id' => $this->integer()->notNull(),
|
|
'to_contentcontainer_id' => $this->integer()->notNull(),
|
|
'requested_by' => $this->integer()->null(),
|
|
'status' => $this->string(32)->notNull()->defaultValue('requested'),
|
|
'request_message' => $this->text()->null(),
|
|
'conditions_text' => $this->text()->null(),
|
|
'responded_at' => $this->dateTime()->null(),
|
|
'completed_at' => $this->dateTime()->null(),
|
|
'created_at' => $this->dateTime()->null(),
|
|
'updated_at' => $this->dateTime()->null(),
|
|
]);
|
|
|
|
$this->safeCreateIndex('idx_rescue_animal_transfer_animal', 'rescue_animal_transfer', 'animal_id', false);
|
|
$this->safeCreateIndex('idx_rescue_animal_transfer_status', 'rescue_animal_transfer', 'status', false);
|
|
$this->safeAddForeignKey('fk_rescue_animal_transfer_animal', 'rescue_animal_transfer', 'animal_id', 'rescue_animal', 'id', 'CASCADE', 'CASCADE');
|
|
$this->safeAddForeignKey('fk_rescue_animal_transfer_from_container', 'rescue_animal_transfer', 'from_contentcontainer_id', 'contentcontainer', 'id', 'CASCADE', 'CASCADE');
|
|
$this->safeAddForeignKey('fk_rescue_animal_transfer_to_container', 'rescue_animal_transfer', 'to_contentcontainer_id', 'contentcontainer', 'id', 'CASCADE', 'CASCADE');
|
|
|
|
if ($this->db->getSchema()->getTableSchema('user', true) !== null) {
|
|
$this->safeAddForeignKey('fk_rescue_animal_transfer_requested_by', 'rescue_animal_transfer', 'requested_by', 'user', 'id', 'SET NULL', 'CASCADE');
|
|
}
|
|
|
|
$this->safeCreateTable('rescue_animal_medical_visit', [
|
|
'id' => $this->primaryKey(),
|
|
'animal_id' => $this->integer()->notNull(),
|
|
'visit_at' => $this->dateTime()->null(),
|
|
'provider_name' => $this->string(190)->null(),
|
|
'notes' => $this->text()->null(),
|
|
'recommendations' => $this->text()->null(),
|
|
'created_by' => $this->integer()->null(),
|
|
'created_at' => $this->dateTime()->null(),
|
|
'updated_at' => $this->dateTime()->null(),
|
|
]);
|
|
|
|
$this->safeCreateIndex('idx_rescue_medical_visit_animal', 'rescue_animal_medical_visit', 'animal_id', false);
|
|
$this->safeAddForeignKey('fk_rescue_medical_visit_animal', 'rescue_animal_medical_visit', 'animal_id', 'rescue_animal', 'id', 'CASCADE', 'CASCADE');
|
|
|
|
$this->safeCreateTable('rescue_animal_progress_update', [
|
|
'id' => $this->primaryKey(),
|
|
'animal_id' => $this->integer()->notNull(),
|
|
'update_at' => $this->dateTime()->null(),
|
|
'weight' => $this->string(32)->null(),
|
|
'vitals' => $this->text()->null(),
|
|
'behavior_notes' => $this->text()->null(),
|
|
'meal_plan_changes' => $this->text()->null(),
|
|
'housing_changes' => $this->text()->null(),
|
|
'medical_concerns' => $this->text()->null(),
|
|
'post_to_space_feed' => $this->boolean()->notNull()->defaultValue(0),
|
|
'post_to_animal_feed' => $this->boolean()->notNull()->defaultValue(1),
|
|
'created_by' => $this->integer()->null(),
|
|
'created_at' => $this->dateTime()->null(),
|
|
'updated_at' => $this->dateTime()->null(),
|
|
]);
|
|
|
|
$this->safeCreateIndex('idx_rescue_progress_animal', 'rescue_animal_progress_update', 'animal_id', false);
|
|
$this->safeAddForeignKey('fk_rescue_progress_animal', 'rescue_animal_progress_update', 'animal_id', 'rescue_animal', 'id', 'CASCADE', 'CASCADE');
|
|
|
|
$this->seedFieldMetadata();
|
|
}
|
|
|
|
public function safeDown()
|
|
{
|
|
$this->safeDropTable('rescue_animal_progress_update');
|
|
$this->safeDropTable('rescue_animal_medical_visit');
|
|
$this->safeDropTable('rescue_animal_transfer');
|
|
$this->safeDropTable('rescue_animal');
|
|
}
|
|
|
|
private function seedFieldMetadata(): void
|
|
{
|
|
if ($this->db->getSchema()->getTableSchema('rescue_field_definition', true) === null) {
|
|
return;
|
|
}
|
|
|
|
$createdAt = date('Y-m-d H:i:s');
|
|
$rows = [
|
|
['name', 'Animal name', 'text', 0, 1, 100],
|
|
['species', 'Species', 'text', 0, 1, 110],
|
|
['breed', 'Breed', 'text', 0, 1, 120],
|
|
['sex', 'Sex', 'text', 0, 1, 130],
|
|
['status', 'Status', 'select', 1, 1, 140],
|
|
['in_possession', 'In possession', 'boolean', 1, 1, 150],
|
|
['location_name', 'Location name', 'text', 0, 1, 160],
|
|
['city', 'City', 'text', 0, 1, 170],
|
|
['state', 'State', 'text', 0, 1, 180],
|
|
['zip', 'ZIP', 'text', 0, 1, 190],
|
|
['public_summary', 'Public summary', 'textarea', 0, 1, 200],
|
|
['medical_notes', 'Medical notes', 'textarea', 0, 1, 210],
|
|
];
|
|
|
|
foreach ($rows as $row) {
|
|
[$fieldKey, $label, $inputType, $required, $isCore, $sortOrder] = $row;
|
|
$exists = (new \yii\db\Query())
|
|
->from('rescue_field_definition')
|
|
->where(['module_id' => 'animal_management', 'field_key' => $fieldKey])
|
|
->exists($this->db);
|
|
|
|
if ($exists) {
|
|
continue;
|
|
}
|
|
|
|
$this->insert('rescue_field_definition', [
|
|
'module_id' => 'animal_management',
|
|
'group_key' => 'animal_profile',
|
|
'field_key' => $fieldKey,
|
|
'label' => $label,
|
|
'input_type' => $inputType,
|
|
'required' => $required,
|
|
'is_core' => $isCore,
|
|
'is_active' => 1,
|
|
'visibility' => 'public',
|
|
'options' => '{}',
|
|
'sort_order' => $sortOrder,
|
|
'created_at' => $createdAt,
|
|
'updated_at' => $createdAt,
|
|
]);
|
|
}
|
|
}
|
|
}
|