Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 8a77a26

Browse files
author
Julien Jomier
committed
ENH: Upgrading SQL
1 parent 2df29b6 commit 8a77a26

File tree

2 files changed

+183
-2
lines changed

2 files changed

+183
-2
lines changed

core/database/upgrade/3.0.10.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
class Upgrade_3_0_10 extends MIDASUpgrade
4+
{
5+
public function preUpgrade()
6+
{
7+
8+
}
9+
10+
public function mysql()
11+
{
12+
//$this->db->query("DROP TABLE metadatatype");
13+
$this->RenameTableField('metadata','metadatatype_id','metadatatype','int(11)','integer','0');
14+
}
15+
16+
17+
public function pgsql()
18+
{
19+
//$this->db->query("DROP TABLE metadatatype");
20+
$this->RenameTableField('metadata','metadatatype_id','metadatatype','int(11)','integer','0');
21+
}
22+
23+
public function postUpgrade()
24+
{
25+
26+
}
27+
}
28+
?>
29+
30+

library/MIDAS/models/MIDASUpgrade.php

Lines changed: 153 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66
class MIDASUpgrade
77
{
8-
98
protected $db;
9+
protected $dbtype;
1010

1111
/**
1212
* @method public __construct()
@@ -21,6 +21,7 @@ public function __construct($db, $module)
2121
{
2222
$this->loadModuleElements();
2323
}
24+
$this->dbtype = Zend_Registry::get('configDatabase')->database->adapter;
2425
} // end __construct()
2526

2627
/** preUpgrade called before the upgrade*/
@@ -148,5 +149,155 @@ public function loadElements()
148149
}
149150
}
150151
}//end loadElements
151-
} //end class MIDASDatabasePdo
152+
153+
154+
/**
155+
* @method public AddTableField()
156+
* Add a field to a table
157+
*/
158+
function AddTableField($table,$field,$mySQLType,$pgSqlType,$default)
159+
{
160+
$sql = '';
161+
if($default !== false)
162+
{
163+
$sql = " DEFAULT '".$default."'";
164+
}
165+
166+
if($this->dbtype == "PDO_PGSQL")
167+
{
168+
$this->db->query("ALTER TABLE \"".$table."\" ADD \"".$field."\" ".$pgSqlType.$sql);
169+
}
170+
else
171+
{
172+
$this->db->query("ALTER TABLE ".$table." ADD ".$field." ".$mySQLType.$sql);
173+
}
174+
}
175+
176+
/**
177+
* @method public RemoveTableField()
178+
* Remove a field from a table
179+
*/
180+
function RemoveTableField($table,$field)
181+
{
182+
if($this->dbtype == "PDO_PGSQL")
183+
{
184+
$this->db->query("ALTER TABLE \"".$table."\" DROP COLUMN \"".$field."\"");
185+
}
186+
else
187+
{
188+
$this->db->query("ALTER TABLE ".$table." DROP ".$field);
189+
}
190+
}
191+
192+
/**
193+
* @method public RenameTableField()
194+
* Rename a field from a table
195+
*/
196+
function RenameTableField($table,$field,$newfield,$mySQLType,$pgSqlType,$default)
197+
{
198+
if($this->dbtype == "PDO_PGSQL")
199+
{
200+
$this->db->query("ALTER TABLE \"".$table."\" RENAME \"".$field."\" TO \"".$newfield."\"");
201+
$this->db->query("ALTER TABLE \"".$table."\" ALTER COLUMN \"".$newfield."\" TYPE ".$pgSqlType);
202+
$this->db->query("ALTER TABLE \"".$table."\" ALTER COLUMN \"".$newfield."\" SET DEFAULT ".$default);
203+
}
204+
else
205+
{
206+
$this->db->query("ALTER TABLE ".$table." CHANGE ".$field." ".$newfield." ".$mySQLType." DEFAULT '".$default."'");
207+
}
208+
}
209+
210+
/**
211+
* @method public CheckIndexExists()
212+
* Check if the index exists.
213+
* Only works for MySQL
214+
*/
215+
function CheckIndexExists($table,$field)
216+
{
217+
if($this->dbtype == "PDO_MYSQL")
218+
{
219+
$rowset = $this->db->fetchAll("SHOW INDEX FROM ".$tablename);
220+
foreach($rowset as $index_array)
221+
{
222+
if($index_array['Column_name'] == $columnname)
223+
{
224+
return true;
225+
}
226+
}
227+
}
228+
return false;
229+
} // end CheckIndexExists()
230+
231+
/**
232+
* @method public AddTableIndex()
233+
* Add an index to a table
234+
*/
235+
function AddTableIndex($table,$field)
236+
{
237+
if(!$this->CheckIndexExists($table,$field))
238+
{
239+
if($this->dbtype == "PDO_PGSQL")
240+
{
241+
@$this->db->query("CREATE INDEX ".$table."_".$field."_idx ON \"".$table."\" (\"".$field."\")");
242+
}
243+
else
244+
{
245+
$this->db->query("ALTER TABLE ".$table." ADD INDEX ( ".$field." )");
246+
}
247+
}
248+
}
249+
250+
/**
251+
* @method public RemoveTableIndex()
252+
* Remove an index from a table
253+
*/
254+
function RemoveTableIndex($table,$field)
255+
{
256+
if($this->CheckIndexExists($table,$field))
257+
{
258+
if($this->dbtype == "PDO_PGSQL")
259+
{
260+
$this->db->query("DROP INDEX ".$table."_".$field."_idx");
261+
}
262+
else
263+
{
264+
$this->db->query("ALTER TABLE ".$table." DROP INDEX ".$field);
265+
}
266+
}
267+
}
268+
269+
/**
270+
* @method public AddTablePrimaryKey()
271+
* Add a primary key to a table
272+
*/
273+
function AddTablePrimaryKey($table,$field)
274+
{
275+
if($this->dbtype == "PDO_PGSQL")
276+
{
277+
$this->db->query("ALTER TABLE \"".$table."\" ADD PRIMARY KEY (\"".$field."\")");
278+
}
279+
else
280+
{
281+
$this->db->query("ALTER TABLE ".$table." ADD PRIMARY KEY ( ".$field." )");
282+
}
283+
}
284+
285+
/**
286+
* @method public RemoveTablePrimaryKey()
287+
* Remove a primary key from a table
288+
*/
289+
function RemoveTablePrimaryKey($table)
290+
{
291+
if($this->dbtype == "PDO_PGSQL")
292+
{
293+
$this->db->query("ALTER TABLE \"".$table."\" DROP CONSTRAINT \"value_pkey\"");
294+
$this->db->query("ALTER TABLE \"".$table."\" DROP CONSTRAINT \"".$table."_pkey\"");
295+
}
296+
else
297+
{
298+
$this->db->query("ALTER TABLE ".$table." DROP PRIMARY KEY");
299+
}
300+
}
301+
302+
} //end class MIDASUpgrade
152303
?>

0 commit comments

Comments
 (0)