Skip to content

Instantly share code, notes, and snippets.

@mwillbanks
Created August 13, 2015 18:55
Show Gist options
  • Save mwillbanks/5a1f54e86476fdf05fd8 to your computer and use it in GitHub Desktop.
Save mwillbanks/5a1f54e86476fdf05fd8 to your computer and use it in GitHub Desktop.
ZF2: Create DDL from Existing Database (Using for PHPUnit myself)
<?php
$mysqli = new mysqli('HOST', 'USER', 'PASSWORD', 'information_schema');
if (mysqli_connect_errno()) {
printf('Connect failed: %s' . PHP_EOL, mysqli_connect_error());
exit();
}
$tables = [];
$result = $mysqli->query("SELECT table_name, column_name, column_default, is_nullable, data_type, character_maximum_length, numeric_precision, numeric_scale from columns where table_schema = 'SCHEMA' order by table_name");
if (!$result) {
printf('Error: %s' . PHP_EOL, $mysqli->error);
exit();
}
while ($row = $result->fetch_row()) {
list($table, $column, $default, $nullable, $type, $length, $precision, $scale) = $row;
$nullable = ($nullable == 'NO') ? false : true;
if (!isset($tables[$table])) {
$tables[$table] = [];
}
switch ($type) {
case 'bigint':
$tables[$table][] = sprintf("Column\\BigInteger('%s', %s, %s)", $column, var_export($nullable), var_export($default));
break;
case 'binary':
$tables[$table][] = sprintf("Column\\Binary('%s', %d, %s, %s)", $column, $length, var_export($nullable), var_export($default));
break;
case 'bit':
if ($default == "b'0'") {
$default = false;
} else if ($default == "b'1'") {
$default = true;
} else {
$default = null;
}
$tables[$table][] = sprintf("Column\\Boolean('%s', %s, %s)", $column, var_export($nullable), var_export($default));
break;
case 'blob':
case 'mediumblob':
case 'longblob':
$tables[$table][] = sprintf("Column\\Blob('%s', %d, %s, %s)", $column, $length, var_export($nullable), var_export($default));
break;
case 'char':
$tables[$table][] = sprintf("Column\\Char('%s', %d, %s, %s)", $column, $length, var_export($nullable), var_export($default));
break;
case 'date':
$tables[$table][] = sprintf("Column\\Date('%s', %s, %s)", $column, var_export($nullable), var_export($default));
break;
case 'datetime':
$tables[$table][] = sprintf("Column\\Datetime('%s', %s, %s)", $column, var_export($nullable), var_export($default));
break;
case 'decimal':
$tables[$table][] = sprintf("Column\\Decimal('%s', null, null, %s, %s)", $column, var_export($nullable), var_export($default));
break;
case 'double':
case 'float':
$tables[$table][] = sprintf("Column\\Floating('%s', null, null, %s, %s)", $column, var_export($nullable), var_export($default));
break;
case 'enum':
$tables[$table][] = sprintf("Column\\Varchar('%s', %d, %s, %s)", $column, $length, var_export($nullable), var_export($default));
break;
case 'int':
case 'smallint':
case 'mediumint':
case 'tinyint':
$tables[$table][] = sprintf("Column\\Integer('%s', %s, %s)", $column, var_export($nullable), var_export($default));
break;
case 'text':
case 'mediumtext';
case 'longtext':
$tables[$table][] = sprintf("Column\\Text('%s', %d, %s, %s)", $column, $length, var_export($nullable), var_export($default));
break;
case 'timestamp':
$tables[$table][] = sprintf("Column\\Timestamp('%s', %s, %s)", $column, var_export($nullable), var_export($default));
break;
case 'varchar':
$tables[$table][] = sprintf("Column\\Varchar('%s', %d, %s, %s)", $column, $length, var_export($nullable), var_export($default));
break;
default:
throw new RuntimeException('Unknown type: ' . $type);
}
}
foreach ($tables as $tableName => $table) {
echo sprintf('$table = new Ddl\CreateTable(\'%s\');', $tableName) . PHP_EOL;
foreach ($table as $column) {
echo sprintf('$table->addColumn(new %s);', $column) . PHP_EOL;
}
echo '$tables[] = $table;' . PHP_EOL . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment