Odyssey
2018100900_addWebContractFields.php
1 <?php
2 /**
3  * Notes:
4  * 1. Anything complicated needs to be done with SQL.
5  * 2. Always have a check to know if the migration needs to occur (up or down).
6  * 3. Use up() and down(), not change(), because of the SQL
7  */
8 use Phinx\Migration\AbstractMigration;
9 class AddWebContractFields extends AbstractMigration {
10  public function up() {
11  $tableName = "cuinfo";
12  $tableColumnExpires = "web_contract_expires";
13  $tableColumnTerm = "web_renewal_term";
14 
15  if ($this->hasTable($tableName)) {
16  // Add web contract columns if they do not already exist
17  $table = $this->table($tableName);
18  if (!$table->hasColumn($tableColumnExpires)) {
19  $this->query("ALTER TABLE $tableName ADD COLUMN $tableColumnExpires date;");
20  }
21 
22  if (!$table->hasColumn($tableColumnTerm)) {
23  $this->query("ALTER TABLE $tableName ADD COLUMN $tableColumnTerm integer;");
24  }
25  } else {
26  print "Skip $tableName, table not found.\n";
27  }
28  } // End "up" function.
29  public function down() {
30  $tableName = "cuinfo";
31  $tableColumnExpires = "web_contract_expires";
32  $tableColumnTerm = "web_renewal_term";
33 
34  if ($this->hasTable($tableName)) {
35  // Add web contract columnsif they do not already exist
36  $table = $this->table($tableName);
37  if ($table->hasColumn($tableColumnExpires)) {
38  $this->query("ALTER TABLE $tableName DROP COLUMN $tableColumnExpires;");
39  }
40 
41  if ($table->hasColumn($tableColumnTerm)) {
42  $this->query("ALTER TABLE $tableName DROP COLUMN $tableColumnTerm;");
43  }
44  } else {
45  print "Skip $tableName, table not found.\n";
46  }
47  } // End "down" function
48 }