SHARE
TWEET

Untitled

a guest Sep 17th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace upstatement\jobsboard\migrations;
  4.  
  5. use upstatement\jobsboard\Jobsboard;
  6.  
  7. use Craft;
  8. use craft\config\DbConfig;
  9. use craft\db\Migration;
  10.  
  11. class Install extends Migration
  12. {
  13.     // Public Properties
  14.     // =========================================================================
  15.  
  16.     /**
  17.      * @var string The database driver to use
  18.      */
  19.     public $driver;
  20.  
  21.     public function safeUp()
  22.     {
  23.         $this->driver = Craft::$app->getConfig()->getDb()->driver;
  24.         if ($this->createTables()) {
  25.             $this->addForeignKeys();
  26.             // Refresh the db schema caches
  27.             Craft::$app->db->schema->refresh();
  28.         }
  29.  
  30.         return true;
  31.     }
  32.  
  33.     public function safeDown()
  34.     {
  35.         $this->driver = Craft::$app->getConfig()->getDb()->driver;
  36.         $this->removeTables();
  37.  
  38.         return true;
  39.     }
  40.  
  41.     protected function createTables()
  42.     {
  43.         $tablesCreated = false;
  44.  
  45.         $tableSchema = Craft::$app->db->schema->getTableSchema('{{%jobsboard_jobs}}');
  46.         if ($tableSchema === null) {
  47.             $tablesCreated = true;
  48.             $this->createJobsTable();
  49.         }
  50.  
  51.         return $tablesCreated;
  52.     }
  53.  
  54.     protected function createJobsTable()
  55.     {
  56.       $this->createTable(
  57.           '{{%jobsboard_jobs}}',
  58.           [
  59.               'id' => $this->primaryKey(),
  60.               'dateCreated' => $this->dateTime()->notNull(),
  61.               'dateUpdated' => $this->dateTime()->notNull(),
  62.               'invoiceNumber' => $this->integer(),
  63.               'approved' => $this->boolean()->notNull()->defaultValue(true),
  64.               'draft' => $this->boolean()->notNull()->defaultValue(true),
  65.               'fieldLayoutId' => $this->integer(),
  66.               'employerEmail' => $this->string()->notNull(),
  67.               'expirationDate' => $this->date(),
  68.               'postDate' => $this->date(),
  69.           ]
  70.       );
  71.     }
  72.  
  73.     protected function addForeignKeys()
  74.     {
  75.         $this->addForeignKey(
  76.             $this->db->getForeignKeyName('{{%jobsboard_jobs}}', 'id'),
  77.             '{{%jobsboard_jobs}}',
  78.             'id',
  79.             '{{%elements}}',
  80.             'id',
  81.             'CASCADE',
  82.             null
  83.         );
  84.     }
  85.  
  86.     protected function removeTables()
  87.     {
  88.         $this->dropTableIfExists('{{%jobsboard_jobs}}');
  89.     }
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top