/**
 * Migrate database table 
 * 
 * Partly borrowed from codeigniter 
 *
 * @return mixed
 **/
function migrate($model_obj)
{
    $model_name = get_class($model_obj);
    $migration_dir = conf('application_path') . 'migrations/' . strtolower($model_name);
    $target_version = $model_obj->get_version();
    $current_version = $model_obj->get_schema_version();
    // Check if directory exists
    if (!is_dir($migration_dir)) {
        throw new Exception('no migrations found in ' . $migration_dir);
    }
    $migration_list = array();
    // Get migration files
    foreach (glob($migration_dir . '/*_*.php') as $file) {
        $name = basename($file, '.php');
        $number = intval(strtok($name, '_'));
        $migration_list[$number] = $file;
    }
    if ($target_version > 0 && !isset($migration_list[$target_version])) {
        throw new Exception($model_name . ' migration ' . $target_version . ' not found');
    }
    if ($target_version > $current_version) {
        ksort($migration_list);
        $method = 'up';
    } else {
        krsort($migration_list);
        $method = 'down';
    }
    $migration_obj = new Migration($model_obj->get_table_name());
    foreach ($migration_list as $number => $file) {
        include_once $file;
        $name = basename($file, '.php');
        $class = 'Migration' . substr($name, strpos($name, '_'));
        if (!class_exists($class, FALSE)) {
            throw new Exception('migration class ' . $class . ' not found');
        }
        if ($method === 'up' && $number > $current_version && $number <= $target_version or $method === 'down' && $number <= $current_version && $number > $target_version) {
            $instance = new $class();
            // Check if we have up and down
            if (!method_exists($instance, 'up') || !method_exists($instance, 'down')) {
                throw new Exception("up() or down() missing from {$class}");
            }
            if (call_user_func(array($instance, $method)) === FALSE) {
                throw new Exception($instance->get_errors());
            }
            $migration_obj->version = $number;
            $migration_obj->save();
        }
    }
    if ($current_version != $target_version) {
        $migration_obj->version = $target_version;
        $migration_obj->save();
    }
}
Exemple #2
0
 /**
  *
  * 在数据迁移表中创建指定版本的数据
  */
 public function checkMigration($version, $module, $apply_time)
 {
     //插入需要的数据
     $migration_data = $this->findByVersionAndModule($version, $module);
     if (empty($migration_data)) {
         $migration = new Migration();
         $migration->version = $version;
         $migration->apply_time = $apply_time;
         $migration->module = $module;
         $migration->save();
     }
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Migration();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['price'])) {
         $model->attributes = $_POST['Migration'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
Exemple #4
0
 /**
  * Create table
  * 
  * Create table based on $this->rs array
  * and $this->rt array
  *
  * @param array assoc array with optional type strings
  * @return boolean TRUE on success, FALSE if failed
  * @author bochoven
  **/
 function create_table()
 {
     // Check if we instantiated this table before
     if (isset($GLOBALS['tables'][$this->tablename])) {
         return TRUE;
     }
     // Check if table exists and is up-to-date
     try {
         $dbh = $this->getdbh();
         $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
         // Check if table exists
         $this->prepare("SELECT * FROM " . $this->enquote($this->tablename) . " LIMIT 1");
         // Existing table, is it up-to date?
         if (conf('allow_migrations')) {
             if ($this->get_schema_version() !== $this->schema_version) {
                 try {
                     require_once conf('application_path') . 'helpers/database_helper.php';
                     migrate($this);
                     $model_name = get_class($this);
                     alert('Migrated ' . $model_name . ' to version ' . $this->schema_version);
                 } catch (Exception $e) {
                     error("Migration error: {$this->tablename}: " . $e->getMessage());
                     // Rollback any open transaction
                     try {
                         $dbh->rollBack();
                     } catch (Exception $e2) {
                     }
                 }
             }
         }
     } catch (Exception $e) {
         // If the prepare fails, the table does not exist.
         // Get columns
         $columns = array();
         foreach ($this->rs as $name => $val) {
             // Determine type automagically
             $type = $this->get_type($val);
             // Or set type from type array
             $columns[$name] = isset($this->rt[$name]) ? $this->rt[$name] : $type;
         }
         // Set primary key
         $columns[$this->pkname] = 'INTEGER PRIMARY KEY';
         // Table options, override per driver
         $tbl_options = '';
         // Driver specific options
         switch ($this->get_driver()) {
             case 'sqlite':
                 $columns[$this->pkname] .= ' AUTOINCREMENT';
                 break;
             case 'mysql':
                 $columns[$this->pkname] .= ' AUTO_INCREMENT';
                 $tbl_options = conf('mysql_create_tbl_opts');
                 break;
         }
         // Compile columns sql
         $sql = '';
         foreach ($columns as $name => $type) {
             $sql .= $this->enquote($name) . " {$type},";
         }
         $sql = rtrim($sql, ',');
         try {
             $dbh->exec(sprintf("CREATE TABLE %s (%s) %s", $this->enquote($this->tablename), $sql, $tbl_options));
             // Set indexes
             $this->set_indexes();
             // Store schema version in migration table
             $migration = new Migration($this->tablename);
             $migration->version = $this->schema_version;
             $migration->save();
             alert("Created table '{$this->tablename}' version {$this->schema_version}");
         } catch (Exception $e) {
             $dbh->exec('DROP TABLE ' . $this->enquote($this->tablename));
             error("Create table '{$this->tablename}' failed: " . $e->getMessage());
             return FALSE;
         }
     }
     // Store this table in the instantiated tables array
     $GLOBALS['tables'][$this->tablename] = $this->tablename;
     // Create table succeeded
     return TRUE;
 }
 private function migrateUp($version)
 {
     $class = 'pz6\\migrations\\' . $version;
     $migration = new $class();
     if ($migration->up() !== false) {
         //$this->writeUp($version);
         $migrations = new Migration();
         $migrations->migration = $version;
         return $migrations->save();
     }
     return false;
 }