Skip to content

Commit

Permalink
2.0 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
c006 committed Apr 5, 2017
1 parent 7d9a454 commit 20112a2
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 268 deletions.
5 changes: 0 additions & 5 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
class Module extends \yii\base\Module
{

/**
*
*/
const VERSION = '0.0.2-dev';

/**
* @var string
*/
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Yii2 Migration Utility
===================

**Current Version - v1.2.1**
**Current Version - v2.0.0**

+ Add JqueryAsset to DefaultController

Expand All @@ -24,7 +24,7 @@ It automatically writes out all:
+ column types
+ column defaults
+ primary keys
+ composite keys
+ composite PKs
+ foreign key
+ indexes
+ Table data
Expand Down Expand Up @@ -73,7 +73,9 @@ The tables must already exist in website schema.
Demo
-------

Demo: [http://demo.c006.us](http://demo.c006.us)
Demo: [http://c006.us](http://c006.us)

Demo is currently down


Usage
Expand Down Expand Up @@ -104,6 +106,8 @@ Contributors
+ [Deele](https://github.com/Deele)
+ Sedov Sergey
+ fedemotta
+ derekisbusy
+ kvasyl



Expand Down
84 changes: 0 additions & 84 deletions assets/c006-migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,4 @@
*/


jQuery(function () {
jQuery('#migrationutility-databasetables').bind('change',
function () {
var val = jQuery(this).find('option:selected').text();
if (val) {
var $elm = jQuery('#migrationutility-tables');
var val2 = $elm.val().replace(val + ',', '');
val = val2 + ',' + val;
val = val.replace(/,+/gi, ',').replace(/\s+/gi, '').replace(/^,/, '');
$elm.val(val);
}
});
});

jQuery(function () {

jQuery.fn.selectText = function () {
this.find('input').each(function () {
if ($(this).prev().length == 0 || !$(this).prev().hasClass('p_copy')) {
$('<p class="p_copy" style="position: absolute; z-index: -1;"></p>').insertBefore($(this));
}
$(this).prev().html($(this).val());
});
var doc = document;
var element = this[0];
console.log(this, element);
if (doc.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
};

jQuery('#button-add-all')
.click(function () {
var $tables = jQuery('#migrationutility-tables');
$tables.val("");
jQuery("#migrationutility-databasetables > option")
.each(function () {
if (this.text != 'migration') {
$tables.val($tables.val() + ',' + this.text);
}
});
$tables.val($tables.val().replace(/^,+/, ''));
});
jQuery('#button-select-all')
.click(function () {
jQuery('#code-output').selectText();
});
jQuery('#button-select-all-drop')
.click(function () {
jQuery('#code-output-drop').selectText();
});
jQuery('#button-tables-convert')
.click(function () {
var $this = jQuery('#migrationutility-tables');
var $parent = $this.parent();
if ($this.attr('type') == "text") {
var $textarea = jQuery(document.createElement('textarea'));
$textarea.attr('id', $this.attr('id'));
$textarea.attr('type', 'textarea');
$textarea.attr('class', $this.attr('class'));
$textarea.attr('name', $this.attr('name'));
$textarea.html($this.val().replace(/\s+/g, '').replace(/,/g, "\n"));
$this.remove();
jQuery($textarea).insertAfter($parent.find('> label'));
} else {
var $input = jQuery(document.createElement('input'));
$input.attr('id', $this.attr('id'));
$input.attr('type', 'text');
$input.attr('class', $this.attr('class'));
$input.attr('name', $this.attr('name'));
$input.val($this.html().replace(/[\r\n]/g, ", "));
$this.remove();
jQuery($input).insertAfter($parent.find('> label'));
}
jQuery('#migrationutility-tables').blur();
});
});
90 changes: 47 additions & 43 deletions controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
class DefaultController extends Controller
{

/**
*
*/
Expand All @@ -37,21 +36,23 @@ public function actionIndex()
$output_drop = new OutputString();
$tables_value = '';
if (isset($_POST['MigrationUtility'])) {
$post = $_POST['MigrationUtility'];
$array = [];
$array['inserts'] = [];
$array['fk'] = [];
$array['indexes'] = [];
$array['pk'] = [];

$foreignKeyOnUpdate = $_POST['MigrationUtility']['ForeignKeyOnUpdate'];
$foreignKeyOnDelete = $_POST['MigrationUtility']['ForeignKeyOnDelete'];
$tables_value = $_POST['MigrationUtility']['tables'];
$ifThen = 1; //$_POST['MigrationUtility']['addIfThenStatements'];
$addTableInserts = $_POST['MigrationUtility']['addTableInserts'];
$foreignKeyOnUpdate = $post['ForeignKeyOnUpdate'];
$foreignKeyOnDelete = $post['ForeignKeyOnDelete'];
$tables_value = $post['tables'];
$ifThen = 1; //$post['addIfThenStatements'];
$addTableInserts = $post['addTableInserts'];
$tableOptions = [];
$tableOptions['mysql'] = [$_POST['MigrationUtility']['mysql'], $_POST['MigrationUtility']['mysql_options']];
$tableOptions['mssql'] = [$_POST['MigrationUtility']['mssql'], $_POST['MigrationUtility']['mssql_options']];
$tableOptions['pgsql'] = [$_POST['MigrationUtility']['pgsql'], $_POST['MigrationUtility']['pgsql_options']];
$tableOptions['sqlite'] = [$_POST['MigrationUtility']['sqlite'], $_POST['MigrationUtility']['sqlite_options']];
$tableOptions['mysql'] = [$post['mysql'], $post['mysql_options']];
$tableOptions['mssql'] = [$post['mssql'], $post['mssql_options']];
$tableOptions['pgsql'] = [$post['pgsql'], $post['pgsql_options']];
$tableOptions['sqlite'] = [$post['sqlite'], $post['sqlite_options']];

$tables = trim($tables_value);
$tables = preg_replace('/\s+/', ',', $tables);
Expand All @@ -78,7 +79,8 @@ public function actionIndex()
}

$output->addStr('/* ' . strtoupper($dbType) . ' */');
$output->addStr('if (!in_array(\'' . $table . '\', $tables)) { ');
$output->addStr('if (!in_array(Yii::$app->db->tablePrefix.\'' . $table . '\', $tables)) { ');

if ($ifThen) {
$output->addStr('if ($dbType == "' . $dbType . '") {');
$output->tabLevel++;
Expand All @@ -91,11 +93,14 @@ public function actionIndex()
$appUtility = new AppUtility($column, $dbType);
$output->addStr($appUtility->string . "',");
if ($column->isPrimaryKey) {
$output->addStr($k . " => 'PRIMARY KEY (`" . $column->name . "`)',");
$array['pk'][] = $column->name;
}
$k++;

}
if (sizeof($array['pk'])) {
$output->addStr($k . " => 'PRIMARY KEY (`" . join('`,`', $array['pk']) . "`)',");
}
$array['pk'] = [];

$output->tabLevel--;
$output->addStr('], $tableOptions_' . strtolower($dbType) . ');');
Expand All @@ -122,8 +127,6 @@ public function actionIndex()
}
}
}


}

$table_indexes = Yii::$app->db->createCommand('SHOW INDEX FROM `' . $table . '`')->queryAll();
Expand Down Expand Up @@ -230,34 +233,35 @@ public function actionIndex()

protected function mysql_direct($table)
{
print_r(Yii::$app->db);
exit;

$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
/* TESTING ONLY */
// print_r(Yii::$app->db);
// exit;
//
// $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
// or die('Could not connect: ' . mysql_error());
// echo 'Connected successfully';
// mysql_select_db('my_database') or die('Could not select database');
//
//// Performing SQL query
// $query = 'SELECT * FROM my_table';
// $result = mysql_query($query) or die('Query failed: ' . mysql_error());
//
//// Printing results in HTML
// echo "<table>\n";
// while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
// echo "\t<tr>\n";
// foreach ($line as $col_value) {
// echo "\t\t<td>$col_value</td>\n";
// }
// echo "\t</tr>\n";
// }
// echo "</table>\n";
//
//// Free resultset
// mysql_free_result($result);
//
//// Closing connection
// mysql_close($link);

}

Expand Down
81 changes: 81 additions & 0 deletions migrations/m000000_000000_c006_testing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace c006\preferences\migrations;

use Yii;
use yii\db\Migration;

class m000000_000000_c006_testing extends Migration
{

/**
* ~ Console command ~
*
* php yii migrate --migrationPath=@vendor/c006/yii2-prefs/migrations
*
*/

/**
*
*/
public function up()
{

self::down();

$tables = Yii::$app->db->schema->getTableNames();
$dbType = $this->db->driverName;
$tableOptions_mysql = "CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB";
$tableOptions_mssql = "";
$tableOptions_pgsql = "";
$tableOptions_sqlite = "";
/* MYSQL */
if (!in_array(Yii::$app->db->tablePrefix.'new_table_2', $tables)) {
if ($dbType == "mysql") {
$this->createTable('{{%new_table_2}}', [
'id' => 'INT(10) UNSIGNED NOT NULL AUTO_INCREMENT',
'column_varchar' => 'VARCHAR(200) NULL',
'column_char' => 'CHAR(3) NULL',
'column_tinyint' => 'TINYINT(1) NULL',
4 => 'PRIMARY KEY (`id`)',
], $tableOptions_mysql);
}
}

/* MYSQL */
if (!in_array(Yii::$app->db->tablePrefix.'new_table', $tables)) {
if ($dbType == "mysql") {
$this->createTable('{{%new_table}}', [
'id' => 'INT(11) NOT NULL AUTO_INCREMENT',
'id2' => 'INT(11) NOT NULL',
'column1' => 'VARCHAR(100) NULL',
'column2' => 'CHAR(3) NOT NULL',
4 => 'PRIMARY KEY (`id`,`id2`)',
], $tableOptions_mysql);
}
}


$this->execute('SET foreign_key_checks = 0');
$this->insert('{{%new_table_2}}',['id'=>'1','column_varchar'=>'A','column_char'=>'B','column_tinyint'=>'1']);
$this->insert('{{%new_table_2}}',['id'=>'2','column_varchar'=>'AA','column_char'=>'BB','column_tinyint'=>'0']);
$this->insert('{{%new_table}}',['id'=>'1','id2'=>'0','column1'=>'A','column2'=>'bbb']);
$this->insert('{{%new_table}}',['id'=>'2','id2'=>'1','column1'=>'b','column2'=>'ccc']);
$this->execute('SET foreign_key_checks = 1;');

}

/**
*
*/
public function down()
{
$this->execute('SET foreign_key_checks = 0');
$this->execute('DROP TABLE IF EXISTS `new_table_2`');
$this->execute('SET foreign_key_checks = 1;');
$this->execute('SET foreign_key_checks = 0');
$this->execute('DROP TABLE IF EXISTS `new_table`');
$this->execute('SET foreign_key_checks = 1;');
}

}
Loading

0 comments on commit 20112a2

Please sign in to comment.