'INT(11) NOT NULL AUTO_INCREMENT', 'timestamp' => 'datetime', 'blob' => 'longtext', // eak what blob is stored? 'text' => 'VARCHAR(128)', ); function determineVersion() { $ret = parent::determineVersion(); if ($ret) { return $ret; } parse_str(str_replace(array(";",":"), "&", $this->db->dsn), $dsn); try { $this->db->query("CREATE DATABASE {$dsn['dbname']}"); } catch (Exception $e) { // technically this should not get here.. // as pdo's dsn design is borked.. } // assume we need to create database.. return null; } function computeFieldCreate($f) { $str = "\t`$f->name` "; $str .= isset($this->typemap[$f->type]) ? $this->typemap[$f->type] : $f->type; if (isset($f->nullable) && $f->nullable == '0') { $str .= ' NOT NULL '; } if (!isset($f->default)) { return $str; } if (!strlen($f->default)) { return $str . " DEFAULT ''"; } return $str . ($f->default == 'CURRENT_TIMESTAMP' ? '' : 'DEFAULT '. $f->default); } // may as well rewrite this.. as we have too loop throug hit anyway.. function createTable(MTrackDBSchema_Table $table) { echo "Create $table->name\n"; $pri_key = null; $sql = array(); foreach ($table->fields as $f) { if ($f->type == 'autoinc') { $pri_key = $f->name; } $str = $this->computeFieldCreate($f); $sql[] = $str; } if (is_array($table->keys)) { foreach ($table->keys as $kn=>$k) { if ($k->type != 'primary') continue; // if ($pri_key !== null) continue; $sql[] = "\tprimary key (`" . join('`, `', $k->fields) . "`)"; } } $sql = "CREATE TABLE $table->name (\n" . join(",\n", $sql) . ")\n"; echo $sql; $this->db->exec($sql); } function alterTable(MTrackDBSchema_Table $from, MTrackDBSchema_Table $to) { $sql = array(); $actions = array(); //print_r($to); /* if keys have changed, we drop the old key definitions before changing the columns */ if (is_array($from->keys)) foreach ($from->keys as $k) { if (!isset($to->keys[$k->name]) || $to->keys[$k->name] != $k) { if ($k->type == 'primary') { $actions[] = "DROP PRIMARY KEY"; } else { $actions[] = "DROP INDEX $k->name"; } } } foreach ($from->fields as $f) { if (isset($to->fields[$f->name])) { continue; } // let's check to see if it got renamed.. $found = false; foreach($to->fields as $ff) { if ($ff->oldname == $f->name) { $actions[] = "CHANGE COLUMN `$f->name` " . $this->computeFieldCreate($ff); unset($to->fields[$ff->name]); $found = true; } } if ($found) continue; $actions[] = "DROP COLUMN $f->name"; } foreach ($to->fields as $f) { if (isset($from->fields[$f->name])) continue; $actions[] = "ADD COLUMN " . $this->computeFieldCreate($f); } /* changed and new keys */ if (is_array($from->keys)) foreach ($from->keys as $k) { if (isset($to->keys[$k->name]) && $to->keys[$k->name] != $k) { if ($k->type == 'primary') { $actions[] = "ADD primary key (" . join(', ', $k->fields) . ")"; } else { $sql[] = $this->computeIndexCreate($to, $k); } } } if (is_array($to->keys)) foreach ($to->keys as $k) { if (isset($from->keys[$k->name])) continue; if ($k->type == 'primary') { $actions[] = "ADD primary key (" . join(', ', $k->fields) . ")"; } else { $sql[] = $this->computeIndexCreate($to, $k); } } if (count($actions)) { $sql[] = "ALTER TABLE $from->name " . join(",\n", $actions); } echo "Need to alter $from->name\n"; echo "SQL:\n"; var_dump($sql); foreach ($sql as $s) { $this->db->exec($s); } } }