Exemple #1
0
 public function autoIncrement()
 {
     if (count($this->columns) > 1) {
         throw new \Exception("Cannot make an auto incementing composite key.");
     }
     $this->getDriver()->addAutoPrimaryKey(\yentu\Parameters::wrap(array('table' => $this->table->getName(), 'schema' => $this->table->getSchema()->getName(), 'column' => $this->columns[0])));
     return $this;
 }
 public function __call($name, $arguments)
 {
     if (preg_match("/^(add|drop|change|executeQuery|reverseQuery)/", $name)) {
         $details = Parameters::wrap($arguments[0]);
         $this->description->{$name}($details);
         $name = "_{$name}";
         new \ReflectionMethod($this, $name);
         return $this->{$name}($details);
     } else {
         throw new \Exception("Failed to execute method '{$name}'");
     }
 }
Exemple #3
0
 protected function addChange($property, $attribute, $value, $callback = null)
 {
     if (!$this->isNew()) {
         $currentDescription = $this->buildDescription();
         $newDescription = $currentDescription;
         $newDescription[$attribute] = $value;
         $class = new \ReflectionClass($this);
         $name = $class->getShortName();
         $this->changes[] = \yentu\Parameters::wrap(array('method' => "change{$name}" . str_replace('_', '', $attribute), 'args' => array('from' => $currentDescription, 'to' => $newDescription)));
     }
     (new \ReflectionProperty($this, $property))->setValue($this, $value);
     return $this;
 }
Exemple #4
0
 public function __construct($name, $table)
 {
     $this->table = $table;
     $this->name = $name;
     $column = Parameters::wrap($this->getDriver()->doesColumnExist(array('table' => $table->getName(), 'schema' => $table->getSchema()->getName(), 'name' => $name)), ['default', 'length', 'nulls', 'type']);
     if ($column === false) {
         $this->new = true;
     } else {
         $this->default = $column['default'];
         $this->length = $column['length'];
         $this->type = $column['type'];
         $this->nulls = $column['nulls'];
     }
 }
Exemple #5
0
 public function createConfigFile($params)
 {
     $params = \yentu\Parameters::wrap($params, ['port', 'file', 'host', 'dbname', 'user', 'password']);
     mkdir(Yentu::getPath(''));
     mkdir(Yentu::getPath('config'));
     mkdir(Yentu::getPath('migrations'));
     $configFile = new \yentu\CodeWriter();
     $configFile->add('return [');
     $configFile->addIndent();
     $configFile->add("'db' => [");
     $configFile->addIndent();
     $configFile->add("'driver' => '{$params['driver']}',");
     $configFile->add("'host' => '{$params['host']}',");
     $configFile->add("'port' => '{$params['port']}',");
     $configFile->add("'dbname' => '{$params['dbname']}',");
     $configFile->add("'user' => '{$params['user']}',");
     $configFile->add("'password' => '{$params['password']}',");
     $configFile->add("'file' => '{$params['file']}',");
     $configFile->decreaseIndent();
     $configFile->add(']');
     $configFile->decreaseIndent();
     $configFile->add('];');
     file_put_contents(Yentu::getPath("config/default.conf.php"), $configFile);
 }
Exemple #6
0
 protected function _dropColumn($details)
 {
     $description = $this->getDescription();
     if (count($description['tables'][$details['table']]['columns']) === 0) {
         $this->_addColumn(Parameters::wrap(array('table' => $details['table'], 'name' => '__yentu_placeholder_col', 'type' => 'integer'), ['schema', 'length', 'nulls']));
     }
     $this->query(sprintf('ALTER TABLE %s DROP COLUMN `%s`', $this->buildTableName($details['table'], $details['schema']), $details['name']));
 }
Exemple #7
0
 /**
  * Convert the arguments of a migration event to a string description.
  * 
  * @param string $command
  * @param array $arguments
  * @return string
  */
 private static function getDetails($command, $arguments)
 {
     $dir = '';
     $destination = '';
     $arguments = Parameters::wrap($arguments, ['name' => null]);
     if ($command == 'add') {
         $dir = 'to';
     } else {
         if ($command == 'drop') {
             $dir = 'from';
         }
     }
     if (isset($arguments['table']) && isset($arguments['schema'])) {
         $destination = "table " . ($arguments['schema'] != '' ? "{$arguments['schema']}." : '') . "{$arguments['table']}'";
     } elseif (isset($arguments['schema']) && !isset($arguments['table'])) {
         $destination = "schema '{$arguments['schema']}'";
     }
     if (is_string($arguments)) {
         return $arguments;
     }
     if (isset($arguments['column'])) {
         $item = $arguments['column'];
     } else {
         $item = $arguments['name'];
     }
     return "'{$item}' {$dir} {$destination}";
 }
Exemple #8
0
 /**
  * 
  * @param array $details
  */
 protected function _changeColumnNulls($details)
 {
     $details = Parameters::wrap($details, ['nulls']);
     $query = 'ALTER TABLE %s ALTER COLUMN "%s" ';
     if ($details['nulls'] === false) {
         $tableName = $this->buildTableName($details['table'], $details['schema']);
         $columnName = $details['name'];
         $null = ' SET NOT NULL';
         $this->query(sprintf($query . $null, $tableName, $columnName));
     } else {
         if (isset($details['to'])) {
             if ($details['to']['nulls'] === false) {
                 $tableName = $this->buildTableName($details['to']['table'], $details['to']['schema']);
                 $columnName = $details['to']['name'];
                 $null = ' SET NOT NULL';
             } else {
                 $tableName = $this->buildTableName($details['to']['table'], $details['to']['schema']);
                 $columnName = $details['to']['name'];
                 $null = ' DROP NOT NULL';
             }
             $this->query(sprintf($query . $null, $tableName, $columnName));
         }
     }
 }
Exemple #9
0
 /**
  * Generate an SQL query stub which represent a column definition.
  * 
  * @param array $details
  * @return string
  */
 private function getColumnDef($details)
 {
     $details = Parameters::wrap($details, ['length', 'default']);
     return trim(sprintf("`%s` %s %s %s", $details['name'], $this->convertTypes($details['type'], self::CONVERT_TO_DRIVER, $details['length']), $details['nulls'] === false ? 'NOT NULL' : '', $details['default'] === null ? null : "DEFAULT {$details['default']}"));
 }