Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
upgrade_suiteid.php
1 <?php
2 
3 $root_dir = dirname(dirname(dirname(dirname(__FILE__))));
4 
5 require_once $root_dir.'/core/include/init.inc';
6 
7 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
8 $dbType = MatrixDAL::getDbType();
9 if ($dbType == 'pgsql') {
10  // Postgres.
11  // Remove old indexes and keys.
12  MatrixDAL::executeSql('ALTER TABLE ONLY sq_suite_product DROP CONSTRAINT suite_product_pk');
13  MatrixDAL::executeSql('DROP INDEX sq_suite_product_type');
14 
15  // Create a new sequence.
16  // SELECT FROM information_schema.sequences won't work with postgres 8.1, so this is the only way to check
17  try {
18  $sequence = MatrixDAL::executeSqlAssoc("SELECT * from sq_suite_seq");
19  } catch (Exception $e) {
20  MatrixDAL::executeSql('CREATE SEQUENCE sq_suite_seq INCREMENT BY 1');
21  }
22 
23 
24  // Add new columns.
25  MatrixDAL::executeSql('ALTER TABLE sq_suite_product ADD COLUMN suiteid INTEGER');
26  MatrixDAL::executeSql('ALTER TABLE sq_suite_product ADD COLUMN url VARCHAR(2000)');
27  MatrixDAL::executeSql('ALTER TABLE sq_suite_product ADD COLUMN token VARCHAR(30)');
28 
29  // Remove unused column.
30  MatrixDAL::executeSql('ALTER TABLE sq_suite_product DROP COLUMN knows_me_as');
31 
32  // Populate data into not null columns.
33  MatrixDAL::executeSql("UPDATE sq_suite_product SET suiteid=nextval('sq_suite_seq')");
34  MatrixDAL::executeSql("UPDATE sq_suite_product SET url=''");
35  $products = MatrixDAL::executeSqlAssoc('SELECT suiteid, connection FROM sq_suite_product');
36  foreach ($products as $product) {
37  $suiteid = array_get_index($product, 'suiteid', NULL);
38  $connection = array_get_index($product, 'connection', NULL);
39  if ($suiteid === NULL || $connection === NULL) {
40  continue;
41  }
42 
43  $connection = @unserialize($connection);
44  if ($connection === FALSE) {
45  continue;
46  }
47 
48  $url = array_get_index($connection, 'url', NULL);
49  if ($url === NULL) {
50  continue;
51  }
52 
53  unset($connection['url']);
54  $query = MatrixDAL::preparePdoQuery('UPDATE sq_suite_product SET url=:url, connection=:connection WHERE suiteid=:id');
55  MatrixDAL::bindValueToPdo($query, 'url', $url);
56  MatrixDAL::bindValueToPdo($query, 'connection', serialize($connection));
57  MatrixDAL::bindValueToPdo($query, 'id', $suiteid);
59  }//end foreach
60 
61  // Set the not null constraint on columns.
62  MatrixDAL::executeSql('ALTER TABLE sq_suite_product ALTER COLUMN suiteid SET NOT NULL');
63  MatrixDAL::executeSql('ALTER TABLE sq_suite_product ALTER COLUMN url SET NOT NULL');
64 
65  // Set the new constraints and keys
66  MatrixDAL::executeSql('ALTER TABLE ONLY sq_suite_product ADD CONSTRAINT suite_product_pk PRIMARY KEY (suiteid)');
67  MatrixDAL::executeSql('CREATE INDEX sq_suite_product_type ON sq_suite_product (systemid, type, status)');
68 } else {
69  // Oracle.
70  // Remove old indexes and keys.
71  MatrixDAL::executeSql('ALTER TABLE sq_suite_product DROP CONSTRAINT suite_product_pk');
72  MatrixDAL::executeSql('DROP INDEX sq_suite_product_type');
73 
74  // Create a new sequence.
75  $sequence = MatrixDAL::executeSqlAssoc("SELECT sequence_name FROM user_sequences WHERE sequence_name='SQ_SUITE_SEQ'");
76  if (empty($sequence)) {
77  MatrixDAL::executeSql('CREATE SEQUENCE sq_suite_seq INCREMENT BY 1');
78  }
79 
80  // Add new columns.
81  MatrixDAL::executeSql('ALTER TABLE sq_suite_product ADD suiteid INTEGER');
82  MatrixDAL::executeSql('ALTER TABLE sq_suite_product ADD url VARCHAR2(2000)');
83  MatrixDAL::executeSql('ALTER TABLE sq_suite_product ADD token VARCHAR2(30)');
84 
85  // Remove unused column.
86  MatrixDAL::executeSql('ALTER TABLE sq_suite_product DROP COLUMN knows_me_as');
87 
88  // Populate data into not null columns.
89  MatrixDAL::executeSql("UPDATE sq_suite_product SET suiteid=sq_suite_seq.nextVal");
90  MatrixDAL::executeSql("UPDATE sq_suite_product SET url=''");
91  $products = MatrixDAL::executeSqlAssoc('SELECT suiteid, connection FROM sq_suite_product');
92  foreach ($products as $product) {
93  $suiteid = array_get_index($product, 'suiteid', NULL);
94  $connection = array_get_index($product, 'connection', NULL);
95  if ($suiteid === NULL || $connection === NULL) {
96  continue;
97  }
98 
99  $connection = @unserialize($connection);
100  if ($connection === FALSE) {
101  continue;
102  }
103 
104  $url = array_get_index($connection, 'url', NULL);
105  if ($url === NULL) {
106  continue;
107  }
108 
109  unset($connection['url']);
110  $query = MatrixDAL::preparePdoQuery('UPDATE sq_suite_product SET url=:url, connection=:connection WHERE suiteid=:id');
111  MatrixDAL::bindValueToPdo($query, 'url', $url);
112  MatrixDAL::bindValueToPdo($query, 'connection', serialize($connection));
113  MatrixDAL::bindValueToPdo($query, 'id', $suiteid);
114  MatrixDAL::execPdoQuery($query);
115  }//end foreach
116 
117  // Set the not null constraint on columns.
118  MatrixDAL::executeSql('ALTER TABLE sq_suite_product MODIFY suiteid NOT NULL');
119  MatrixDAL::executeSql('ALTER TABLE sq_suite_product MODIFY url NOT NULL');
120 
121  // Set the new constraints and keys
122  MatrixDAL::executeSql('ALTER TABLE sq_suite_product ADD CONSTRAINT suite_product_pk PRIMARY KEY (suiteid)');
123  MatrixDAL::executeSql('CREATE INDEX sq_suite_product_type ON sq_suite_product (systemid, type, status)');
124 }//end if
125 $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
126 
127 ?>