Skip to content

Instantly share code, notes, and snippets.

@sbatson5
Last active September 6, 2019 20:11
Show Gist options
  • Select an option

  • Save sbatson5/27d5126168847a5c6dc08212d8618476 to your computer and use it in GitHub Desktop.

Select an option

Save sbatson5/27d5126168847a5c6dc08212d8618476 to your computer and use it in GitHub Desktop.
Install script for CraftCMS plugin
<?php
namespace upstatement\jobsboard\migrations;
use upstatement\jobsboard\Jobsboard;
use Craft;
use craft\config\DbConfig;
use craft\db\Migration;
class Install extends Migration
{
// Public Properties
// =========================================================================
/**
* @var string The database driver to use
*/
public $driver;
public function safeUp()
{
$this->driver = Craft::$app->getConfig()->getDb()->driver;
if ($this->createTables()) {
$this->addForeignKeys();
// Refresh the db schema caches
Craft::$app->db->schema->refresh();
}
return true;
}
public function safeDown()
{
$this->driver = Craft::$app->getConfig()->getDb()->driver;
$this->removeTables();
return true;
}
protected function createTables()
{
$tablesCreated = false;
$tableSchema = Craft::$app->db->schema->getTableSchema('{{%jobsboard_jobs}}');
if ($tableSchema === null) {
$tablesCreated = true;
$this->createJobsTable();
}
return $tablesCreated;
}
protected function createJobsTable()
{
$this->createTable(
'{{%jobsboard_jobs}}',
[
'id' => $this->primaryKey(),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
'invoiceNumber' => $this->integer(),
'approved' => $this->boolean()->notNull()->defaultValue(true),
'draft' => $this->boolean()->notNull()->defaultValue(true),
'fieldLayoutId' => $this->integer(),
'employerEmail' => $this->string()->notNull(),
'expirationDate' => $this->date(),
'postDate' => $this->date(),
]
);
}
protected function addForeignKeys()
{
$this->addForeignKey(
$this->db->getForeignKeyName('{{%jobsboard_jobs}}', 'id'),
'{{%jobsboard_jobs}}',
'id',
'{{%elements}}',
'id',
'CASCADE',
null
);
}
protected function removeTables()
{
$this->dropTableIfExists('{{%jobsboard_jobs}}');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment