|
@@ -32,6 +32,18 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
* Tipo de sentenca sql delete.
|
|
|
*/
|
|
|
const DELETE = "DELETE";
|
|
|
+ /**
|
|
|
+ * Tipo de sentenca sql replace.
|
|
|
+ */
|
|
|
+ const REPLACE = "REPLACE";
|
|
|
+ /**
|
|
|
+ * Tipo de sentenca sql insert or update.
|
|
|
+ */
|
|
|
+ const INSERTORUPDATE = "INSERTORUPDATE";
|
|
|
+ /**
|
|
|
+ * Tipo de sentenca sql insert ignore.
|
|
|
+ */
|
|
|
+ const INSERTIGNORE = "INSERTIGNORE";
|
|
|
/**
|
|
|
* @var int $line Contiene el numero de linea que estoy ejecutando.
|
|
|
*/
|
|
@@ -40,10 +52,6 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
* @var bool Me dice si tengo que mostrar los parametros en las consultas.
|
|
|
*/
|
|
|
private $showParameters = false;
|
|
|
- /**
|
|
|
- * @var bool Me dice si tengo que mostrar las queryies.
|
|
|
- */
|
|
|
- private $showQuery = true;
|
|
|
/**
|
|
|
* @var array $errorLineExecution Contiene una descripcion del error que ocaciono una linea del script YAML.
|
|
|
*/
|
|
@@ -194,7 +202,8 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
|
|
|
/**
|
|
|
* Procesa un yaml para generar las sentencias DML que luego seran ejecutadas en la base de datos.
|
|
|
- * El directorio origen es <code>app/DoctrineMigrations</code> en adelante.
|
|
|
+ * El directorio origen es <code>app/DoctrineMigrations</code> en adelante. SE UTILIZA EN LA
|
|
|
+ * FUNCION "up" o "down". NO FUNCIONA EN LAS FUNCIONES "pre" y "post".
|
|
|
* @param string $dir Contiene el nombre del directorio a leer.
|
|
|
* @param string $fileName Contiene el nombre del archivo a incorporar.
|
|
|
*/
|
|
@@ -228,16 +237,21 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
$this->createUpdates($value[MigrationsBase::UPDATE]);
|
|
|
} else if (strtoupper($key) === MigrationsBase::DELETE) {
|
|
|
$this->createDeletes($value[MigrationsBase::DELETE]);
|
|
|
+ } else if (strtoupper($key) === MigrationsBase::REPLACE) {
|
|
|
+ $this->createReplace($value[MigrationsBase::REPLACE]);
|
|
|
+ } else if (strtoupper($key) === MigrationsBase::INSERTORUPDATE) {
|
|
|
+ $this->createInsertOrUpdate($value[MigrationsBase::INSERTORUPDATE]);
|
|
|
+ } else if (strtoupper($key) === MigrationsBase::INSERTIGNORE) {
|
|
|
+ $this->createInsertIgnore($value[MigrationsBase::INSERTIGNORE]);
|
|
|
} else {
|
|
|
- die("Valor no esperado");
|
|
|
+ throw new Exception("Los valores permitidos son: insert, insertorupdate, insertignore, update, delete.");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Crea los insert a partir de una estructura yaml. Se puede utilizar la palabra clave "ignore", "replace" o "orupdate".
|
|
|
- * El "replace" sobreescribe al "ignore" y el "orupdate" sobreescribe al "replace".
|
|
|
+ * Crea los insert a partir de una estructura yaml.
|
|
|
* @param array $arrayInsert Contiene la estructura yaml para los insert.
|
|
|
*/
|
|
|
private function createInserts($arrayInsert)
|
|
@@ -272,12 +286,6 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
$ignore = " IGNORE ";
|
|
|
}
|
|
|
}
|
|
|
- } else if ($field === 'replace') {
|
|
|
- $value = strtolower($value);
|
|
|
- if ($value === '1' || $value === 'true') {
|
|
|
- $insert = "REPLACE";
|
|
|
- $ignore = " ";
|
|
|
- }
|
|
|
} else if ($field === 'orupdate') {
|
|
|
$value = strtolower($value);
|
|
|
if ($value === '1' || $value === 'true') {
|
|
@@ -289,7 +297,6 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
$arrayPrepare[':' . $field] = $value;
|
|
|
$fields = $fields . $field . ", ";
|
|
|
$valuesFields = $valuesFields . ":" . $field . ", ";
|
|
|
- $orUpdateValues = $orUpdateValues . $field . " = " . ":" . $field . ", ";
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -299,16 +306,143 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
if (strlen($valuesFields) > 1) {
|
|
|
$valuesFields = substr($valuesFields, 0, strlen($valuesFields) - 2);
|
|
|
}
|
|
|
+ if (strlen($fields) > 1 && strlen($valuesFields) > 1) {
|
|
|
+ $sql = $insert . $ignore . "INTO " . $table . " (" . $fields . ") VALUES (" . $valuesFields . ");";
|
|
|
+ $this->executeSQL($sql, MigrationsBase::INSERT, $arrayPrepare);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Crea los insert ignore a partir de una estructura yaml.
|
|
|
+ * @param array $arrayInsert Contiene la estructura yaml para los insert.
|
|
|
+ */
|
|
|
+ private function createInsertIgnore($arrayInsert)
|
|
|
+ {
|
|
|
+ foreach ($arrayInsert as $table => $inserts) {
|
|
|
+ // recorro las tablas
|
|
|
+ foreach ($inserts as $key => $valueKey) {
|
|
|
+ // recorro cada uno de los insert que quiero hacer
|
|
|
+ // almacena los campos
|
|
|
+ $fields = "";
|
|
|
+ // almacena el valor de los campos
|
|
|
+ $valuesFields = "";
|
|
|
+ // contiene la primer palabra de la sentencia (INSERT/REPLACE)
|
|
|
+ $insert = "INSERT IGNORE ";
|
|
|
+ // contiene los valores del insert or update
|
|
|
+ $orUpdateValues = "";
|
|
|
+ // contiene los valores para el bind del stament
|
|
|
+ $arrayPrepare = array();
|
|
|
+ foreach ($valueKey as $field => $value) {
|
|
|
+ // recorro los datos a insertar
|
|
|
+ $field = strtolower(trim($field));
|
|
|
+ $value = trim($value);
|
|
|
+ if (strlen($field) > 0 && strlen($value) > 0) {
|
|
|
+ $arrayPrepare[':' . $field] = $value;
|
|
|
+ $fields = $fields . $field . ", ";
|
|
|
+ $valuesFields = $valuesFields . ":" . $field . ", ";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (strlen($fields) > 1) {
|
|
|
+ $fields = substr($fields, 0, strlen($fields) - 2);
|
|
|
+ }
|
|
|
+ if (strlen($valuesFields) > 1) {
|
|
|
+ $valuesFields = substr($valuesFields, 0, strlen($valuesFields) - 2);
|
|
|
+ }
|
|
|
+ if (strlen($fields) > 1 && strlen($valuesFields) > 1) {
|
|
|
+ $sql = $insert . "INTO " . $table . " (" . $fields . ") VALUES (" . $valuesFields . ");";
|
|
|
+ $this->executeSQL($sql, MigrationsBase::INSERT, $arrayPrepare);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Crea los insert or update a partir de una estructura yaml.
|
|
|
+ * @param array $arrayInsert Contiene la estructura yaml para los insert.
|
|
|
+ */
|
|
|
+ private function createInsertOrUpdate($arrayInsert)
|
|
|
+ {
|
|
|
+ foreach ($arrayInsert as $table => $inserts) {
|
|
|
+ // recorro las tablas
|
|
|
+ foreach ($inserts as $key => $valueKey) {
|
|
|
+ // recorro cada uno de los insert que quiero hacer
|
|
|
+ // almacena los campos
|
|
|
+ $fields = "";
|
|
|
+ // almacena el valor de los campos
|
|
|
+ $valuesFields = "";
|
|
|
+ // contiene la primer palabra de la sentencia INSERT
|
|
|
+ $insert = "INSERT";
|
|
|
+ // contiene los valores del insert or update
|
|
|
+ $orUpdateValues = "";
|
|
|
+ // contiene los valores para el bind del stament
|
|
|
+ $arrayPrepare = array();
|
|
|
+ foreach ($valueKey as $field => $value) {
|
|
|
+ // recorro los datos a insertar
|
|
|
+ $field = strtolower(trim($field));
|
|
|
+ $value = trim($value);
|
|
|
+ if (strlen($field) > 0 && strlen($value) > 0) {
|
|
|
+ $arrayPrepare[':' . $field] = $value;
|
|
|
+ $fields = $fields . $field . ", ";
|
|
|
+ $valuesFields = $valuesFields . ":" . $field . ", ";
|
|
|
+ $orUpdateValues = $orUpdateValues . $field . " = " . ":" . $field . ", ";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (strlen($fields) > 1) {
|
|
|
+ $fields = substr($fields, 0, strlen($fields) - 2);
|
|
|
+ }
|
|
|
+ if (strlen($valuesFields) > 1) {
|
|
|
+ $valuesFields = substr($valuesFields, 0, strlen($valuesFields) - 2);
|
|
|
+ }
|
|
|
if (strlen($orUpdateValues) > 1) {
|
|
|
$orUpdateValues = substr($orUpdateValues, 0, strlen($orUpdateValues) - 2);
|
|
|
}
|
|
|
if (strlen($fields) > 1 && strlen($valuesFields) > 1) {
|
|
|
- $sql = $insert . $ignore . "INTO " . $table . " (" . $fields . ") VALUES (" . $valuesFields . ")";
|
|
|
- if ($orUpdate) {
|
|
|
- $sql .= " ON DUPLICATE KEY UPDATE " . $orUpdateValues . ";";
|
|
|
- } else {
|
|
|
- $sql .= ";";
|
|
|
+ $sql = $insert . " INTO " . $table . " (" . $fields . ") VALUES (" . $valuesFields . ")";
|
|
|
+ $sql .= " ON DUPLICATE KEY UPDATE " . $orUpdateValues . ";";
|
|
|
+ $this->executeSQL($sql, MigrationsBase::INSERT, $arrayPrepare);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Crea los replace a partir de una estructura yaml.
|
|
|
+ * @param array $arrayInsert Contiene la estructura yaml para los insert.
|
|
|
+ */
|
|
|
+ private function createReplace($arrayInsert)
|
|
|
+ {
|
|
|
+ foreach ($arrayInsert as $table => $inserts) {
|
|
|
+ // recorro las tablas
|
|
|
+ foreach ($inserts as $key => $valueKey) {
|
|
|
+ // recorro cada uno de los insert que quiero hacer
|
|
|
+ // almacena los campos
|
|
|
+ $fields = "";
|
|
|
+ // almacena el valor de los campos
|
|
|
+ $valuesFields = "";
|
|
|
+ // contiene la primer palabra de la sentencia INSERT
|
|
|
+ $insert = "REPLACE";
|
|
|
+ // contiene los valores para el bind del stament
|
|
|
+ $arrayPrepare = array();
|
|
|
+ foreach ($valueKey as $field => $value) {
|
|
|
+ // recorro los datos a insertar
|
|
|
+ $field = strtolower(trim($field));
|
|
|
+ $value = trim($value);
|
|
|
+ if (strlen($field) > 0 && strlen($value) > 0) {
|
|
|
+ $arrayPrepare[':' . $field] = $value;
|
|
|
+ $fields = $fields . $field . ", ";
|
|
|
+ $valuesFields = $valuesFields . ":" . $field . ", ";
|
|
|
}
|
|
|
+ }
|
|
|
+ if (strlen($fields) > 1) {
|
|
|
+ $fields = substr($fields, 0, strlen($fields) - 2);
|
|
|
+ }
|
|
|
+ if (strlen($valuesFields) > 1) {
|
|
|
+ $valuesFields = substr($valuesFields, 0, strlen($valuesFields) - 2);
|
|
|
+ }
|
|
|
+ if (strlen($fields) > 1 && strlen($valuesFields) > 1) {
|
|
|
+ $sql = $insert . " INTO " . $table . " (" . $fields . ") VALUES (" . $valuesFields . ");";
|
|
|
$this->executeSQL($sql, MigrationsBase::INSERT, $arrayPrepare);
|
|
|
}
|
|
|
}
|
|
@@ -421,7 +555,7 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
}
|
|
|
}
|
|
|
} catch (\Symfony\Component\Debug\Exception\ContextErrorException $e) {
|
|
|
- var_dump($e);
|
|
|
+ var_export($e);
|
|
|
}
|
|
|
return $valores;
|
|
|
}
|
|
@@ -449,7 +583,7 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
}
|
|
|
}
|
|
|
} catch (\Symfony\Component\Debug\Exception\ContextErrorException $e) {
|
|
|
- var_dump($e);
|
|
|
+ var_export($e);
|
|
|
}
|
|
|
return $valores;
|
|
|
}
|
|
@@ -469,15 +603,11 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
$version = "";
|
|
|
}
|
|
|
echo "\n\tInicio del procesamiento de la migracion: " . $version;
|
|
|
- $this->connection->beginTransaction();
|
|
|
try {
|
|
|
$this->interpretYaml($dir, $file);
|
|
|
if (count($this->getErrorLineExecution()) > 0) {
|
|
|
- $this->connection->rollBack();
|
|
|
- //se produjeron errores
|
|
|
echo "\n\t\tSe produjeron errores de sql.\n";
|
|
|
} else {
|
|
|
- $this->connection->commit();
|
|
|
echo "\n\t\tMigracion correcta.\n";
|
|
|
}
|
|
|
} catch (\Throwable $ex) {
|
|
@@ -498,14 +628,7 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
*/
|
|
|
public function executeSQL($sql, $type, $arrayPrepare = null)
|
|
|
{
|
|
|
- $this->connection->executeUpdate($sql, (array)$arrayPrepare);
|
|
|
- if ($this->isShowQuery() && count($arrayPrepare) > 0) {
|
|
|
- $tmp = $sql;
|
|
|
- foreach ($arrayPrepare as $key => $value) {
|
|
|
- $tmp = str_replace($key, $value, $tmp);
|
|
|
- }
|
|
|
- echo " -> $tmp\n";
|
|
|
- }
|
|
|
+ $this->addSql($sql, (array)$arrayPrepare);
|
|
|
$this->sumLine();
|
|
|
}
|
|
|
|
|
@@ -779,21 +902,4 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
|
|
|
} catch (\Throwable $e) {
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- public function isShowQuery()
|
|
|
- {
|
|
|
- return $this->showQuery;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param bool $showQuery
|
|
|
- */
|
|
|
- public function setShowQuery($showQuery)
|
|
|
- {
|
|
|
- $this->showQuery = $showQuery;
|
|
|
- }
|
|
|
-
|
|
|
}
|