Advertisement
arogachev

Yii2 Log Module DB Tables Structure Draft

Mar 30th, 2015
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.01 KB | None | 0 0
  1. <?php
  2.  
  3. use common\modules\log\components\Migration;
  4. use yii\db\Schema;
  5.  
  6. class m150121_083301_init extends Migration
  7. {
  8.     /**
  9.      * @inheritdoc
  10.      */
  11.     public function safeUp()
  12.     {
  13.         // Log models
  14.  
  15.         $this->createTable('log_models', [
  16.             'id' => Schema::TYPE_PK,
  17.             'class' => Schema::TYPE_STRING . ' NOT NULL',
  18.             'name' => Schema::TYPE_STRING . ' NOT NULL',
  19.         ]);
  20.  
  21.         // Log attributes
  22.  
  23.         $this->createTable('log_attributes', [
  24.             'id' => Schema::TYPE_PK,
  25.             'log_model_id' => Schema::TYPE_INTEGER . ' NOT NULL',
  26.             'name' => Schema::TYPE_STRING . ' NOT NULL',
  27.             'label' => Schema::TYPE_STRING . ' NOT NULL',
  28.             'is_list' => Schema::TYPE_BOOLEAN . ' NOT NULL',
  29.         ]);
  30.  
  31.         $this->addForeignKey(
  32.             null,
  33.             'log_attributes',
  34.             'log_model_id',
  35.             'log_models',
  36.             'id',
  37.             'CASCADE',
  38.             'CASCADE'
  39.         );
  40.  
  41.         // Log models changes
  42.  
  43.         $this->createTable('log_models_changes', [
  44.             'id' => Schema::TYPE_PK,
  45.             'type' => Schema::TYPE_INTEGER . 'NOT NULL',
  46.             'log_model_id' => Schema::TYPE_INTEGER . ' NOT NULL',
  47.             'model_id' => Schema::TYPE_INTEGER . ' NOT NULL',
  48.             'url' => Schema::TYPE_STRING,
  49.             'user_id' => Schema::TYPE_INTEGER,
  50.             'created_at' => Schema::TYPE_TIMESTAMP . ' NOT NULL DEFAULT CURRENT_TIMESTAMP',
  51.         ]);
  52.  
  53.         $this->addForeignKey(
  54.             null,
  55.             'log_models_changes',
  56.             'log_model_id',
  57.             'log_models',
  58.             'id',
  59.             'CASCADE',
  60.             'CASCADE'
  61.         );
  62.  
  63.         // Log attributes changes
  64.  
  65.         $this->createTable('log_attributes_changes', [
  66.             'id' => Schema::TYPE_PK,
  67.             'log_model_change_id' => Schema::TYPE_INTEGER . ' NOT NULL',
  68.             'log_attribute_id' => Schema::TYPE_INTEGER . ' NOT NULL',
  69.             'old_value' => Schema::TYPE_TEXT,
  70.             'new_value' => Schema::TYPE_TEXT,
  71.             'old_processed_value' => Schema::TYPE_TEXT,
  72.             'new_processed_value' => Schema::TYPE_TEXT,
  73.         ]);
  74.  
  75.         $this->addForeignKey(
  76.             null,
  77.             'log_attributes_changes',
  78.             'log_model_change_id',
  79.             'log_models_changes',
  80.             'id',
  81.             'CASCADE',
  82.             'CASCADE'
  83.         );
  84.  
  85.         $this->addForeignKey(
  86.             null,
  87.             'log_attributes_changes',
  88.             'log_attribute_id',
  89.             'log_attributes',
  90.             'id',
  91.             'CASCADE',
  92.             'CASCADE'
  93.         );
  94.  
  95.         // Log list attributes changes
  96.  
  97.         $this->createTable('log_list_attributes_changes', [
  98.             'id' => Schema::TYPE_PK,
  99.             'log_attribute_change_id' => Schema::TYPE_INTEGER . ' NOT NULL',
  100.             'type' => Schema::TYPE_INTEGER . ' NOT NULL',
  101.             'value' => Schema::TYPE_TEXT . ' NOT NULL',
  102.         ]);
  103.  
  104.         $this->addForeignKey(
  105.             null,
  106.             'log_many_to_many_changes',
  107.             'log_attribute_change_id',
  108.             'log_attributes_changes',
  109.             'id',
  110.             'CASCADE',
  111.             'CASCADE'
  112.         );
  113.     }
  114.  
  115.     /**
  116.      * @inheritdoc
  117.      */
  118.     public function safeDown()
  119.     {
  120.         $this->dropForeignKeyByLink('log_attributes', 'log_models', 'id');
  121.         $this->dropForeignKeyByLink('log_models_changes', 'log_models', 'id');
  122.         $this->dropForeignKeyByLink('log_attributes_changes', 'log_models_changes', 'id');
  123.         $this->dropForeignKeyByLink('log_attributes_changes', 'log_attributes', 'id');
  124.         $this->dropForeignKeyByLink('log_many_to_many_changes', 'log_attributes_changes', 'id');
  125.  
  126.         $this->dropTable('log_models');
  127.         $this->dropTable('log_attributes');
  128.         $this->dropTable('log_models_changes');
  129.         $this->dropTable('log_attributes_changes');
  130.         $this->dropTable('log_many_to_many_changes');
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement