Odyssey
2017033000_scheduledtxn.php
1 <?php
2 /* Notes:
3  * 1. Anything complicated needs to be done with SQL.
4  * 2. Always have a check to know if the migration needs to occur (up or down).
5  * 3. Use up() and down(), not change(), because of the SQL
6  */
7 use Phinx\Migration\AbstractMigration;
8 
9 class ScheduledTxn extends AbstractMigration {
10 
11  /* This is the process for moving forward. We are re-creating the table so if there is
12  * existing data will need to create a temporary table, copy the data, drop the old table,
13  * rename the temporary table.
14  */
15  public function up() {
16 
17  // create scheduledtxn table if it doesn't exist
18  $exists = $this->hasTable("cu_scheduledtxn");
19  print_r($exists, true);
20 
21  if (!$exists) {
22  $sql = "
23  CREATE TABLE cu_scheduledtxn (
24  id integer,
25  cu varchar(10) NOT NULL,
26  name varchar(100),
27  feature_code varchar(10) NOT NULL,
28  user_id integer NOT NULL,
29  create_date timestamp without time zone,
30  approved_by integer,
31  approved_date timestamp without time zone,
32  last_edit_by integer,
33  last_edit_date date,
34  start_date date,
35  end_date date,
36  next_trigger_date date,
37  interval_count smallint,
38  failure_count smallint,
39  status char(1),
40  repeating_parameters varchar,
41  txn_data varchar
42  );";
43  $sqlRs = $this->query($sql);
44 
45  // create sequence
46  $sql = "CREATE SEQUENCE cu_scheduledtxn_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1";
47  $sqlRs = $this->query($sql);
48 
49  // create sequence onwership
50  $sql = "ALTER SEQUENCE cu_scheduledtxn_id_seq OWNED BY cu_scheduledtxn.id";
51  $sqlRs = $this->query($sql);
52 
53  // set default value for id column
54  $sql = "ALTER TABLE ONLY cu_scheduledtxn ALTER COLUMN id SET DEFAULT nextval('cu_scheduledtxn_id_seq'::regclass)";
55  $sqlRs = $this->query($sql);
56 
57  // set primary key constraint on id column
58  $sql = "ALTER TABLE ONLY cu_scheduledtxn ADD CONSTRAINT cu_scheduledtxn_pkey PRIMARY KEY (id)";
59  $sqlRs = $this->query($sql);
60 
61  // get the list of credit unions
62  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
63  for ( $i = 0; $i < count( $cuList ); $i++ ) {
64  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
65 
66  // drop cu memorize hdr/dtl tables
67  if ($this->hasTable("{$cu}memorizedhdr")) {
68  $this->dropTable("{$cu}memorizedhdr");
69  }
70  if ($this->hasTable("{$cu}memorizeddtl")) {
71  $this->dropTable("{$cu}memorizeddtl");
72  }
73  }
74  }
75  }
76 
77  /* To execute this you need to run the following command:
78  * php vendor/bin/phinx rollback (using correct path to phinx)
79  * Or, to roll back to a specific time:
80  * php vendor/bin/phinx rollback -t 20151123211207
81  */
82  public function down() {
83  // drop cu_scheduledtxn table
84  $this->dropTable("cu_scheduledtxn");
85 
86  // get the list of credit unions
87  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
88  for ( $i = 0; $i < count( $cuList ); $i++ ) {
89  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
90 
91  // drop cu memorize hdr/dtl tables
92  if ($this->hasTable("{$cu}memorizedhdr")) {
93  $this->dropTable("{$cu}memorizedhdr");
94  }
95  if ($this->hasTable("{$cu}memorizeddtl")) {
96  $this->dropTable("{$cu}memorizeddtl");
97  }
98  }
99  }
100 }
101 
102 ?>