4 require_once 'DBSchema/Generic.php';
6 class MTrackDBSchema_pgsql extends MTrackDBSchema_Generic {
8 'autoinc' => 'SERIAL UNIQUE',
9 'timestamp' => 'timestamp with time zone',
13 function alterTable(MTrackDBSchema_Table $from, MTrackDBSchema_Table $to)
18 /* if keys have changed, we drop the old key definitions before changing the columns */
19 if (is_array($from->keys)) foreach ($from->keys as $k) {
20 if (!isset($to->keys[$k->name]) || $to->keys[$k->name] != $k) {
21 if ($k->type == 'primary') {
22 $actions[] = "DROP CONSTRAINT {$from->name}_pkey";
24 $sql[] = "DROP INDEX $k->name";
29 foreach ($from->fields as $f) {
30 if (!isset($to->fields[$f->name])) {
31 $actions[] = "DROP COLUMN $f->name";
35 foreach ($to->fields as $f) {
36 if (isset($from->fields[$f->name])) continue;
37 $actions[] = "ADD COLUMN " . $this->computeFieldCreate($f);
40 /* changed and new keys */
41 if (is_array($from->keys)) foreach ($from->keys as $k) {
42 if (isset($to->keys[$k->name]) && $to->keys[$k->name] != $k) {
43 if ($k->type == 'primary') {
44 $actions[] = "ADD primary key (" . join(', ', $k->fields) . ")";
46 $sql[] = $this->computeIndexCreate($to, $k);
50 if (is_array($to->keys)) foreach ($to->keys as $k) {
51 if (isset($from->keys[$k->name])) continue;
52 if ($k->type == 'primary') {
53 $actions[] = "ADD primary key (" . join(', ', $k->fields) . ")";
55 $sql[] = $this->computeIndexCreate($to, $k);
59 if (count($actions)) {
60 $sql[] = "ALTER TABLE $from->name " . join(",\n", $actions);
62 echo "Need to alter $from->name\n";
65 foreach ($sql as $s) {