public function generate()
{
$relations = $this->generateRelations();
$db = $this->getDbConnection();
$tables = [];
foreach ($this->getTableNames() as $tableName) {
$tableSchema = $db->getTableSchema($tableName);
$columns = $this->generateColumns($tableSchema);
if (isset($columns[0])) {
$primary = $columns[0];
unset($columns[0]);
} else {
$primary = null;
}
$tables[$tableSchema->name] = ['name' => $this->generateTableName($tableSchema->name), 'columns' => $columns, 'primary' => $primary, 'relations' => isset($relations[$tableSchema->name]) ? $relations[$tableSchema->name] : []];
}
$migrationName = 'm' . $this->migrationTime . '_' . $this->migrationName;
$file = rtrim(Yii::getAlias($this->migrationPath), '/') . "/{$migrationName}.php";
$files = new CodeFile($file, $this->render('migration.php', ['tables' => $this->reorderTables($tables, $relations), 'migrationName' => $migrationName]));
return [$files];
}