Skip to content

Commit

Permalink
Merge pull request #22 from autn/release/version-2.0
Browse files Browse the repository at this point in the history
Release/version 2.0
  • Loading branch information
autn authored Jun 21, 2016
2 parents 06c0c49 + 9b1addc commit ba330f9
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 12 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"type": "project",
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.*"
"laravel/framework": "5.*",
"ifsnop/mysqldump-php":"2.*"
},
"require-dev": {
"laravel/laravel": "5.2.*",
Expand Down
3 changes: 2 additions & 1 deletion data/composer-laravel.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"type": "project",
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*"
"laravel/framework": "5.2.*",
"ifsnop/mysqldump-php":"2.*"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
108 changes: 108 additions & 0 deletions data/schema-php.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
-- mysqldump-php https://github.com/ifsnop/mysqldump-php
--
-- Host: localhost Database: gcl2
-- ------------------------------------------------------
-- Server version 5.6.30-0ubuntu0.14.04.1
-- Date: Tue, 21 Jun 2016 09:51:42 +0000

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `migrations`
--

/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `migrations` (
`migration` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`batch` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `migrations`
--

LOCK TABLES `migrations` WRITE;
/*!40000 ALTER TABLE `migrations` DISABLE KEYS */;
SET autocommit=0;
INSERT INTO `migrations` VALUES ('2014_10_12_000000_create_users_table',1),('2014_10_12_100000_create_password_resets_table',1);
/*!40000 ALTER TABLE `migrations` ENABLE KEYS */;
UNLOCK TABLES;
COMMIT;

--
-- Table structure for table `password_resets`
--

/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `password_resets` (
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`token` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
KEY `password_resets_email_index` (`email`),
KEY `password_resets_token_index` (`token`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `password_resets`
--

LOCK TABLES `password_resets` WRITE;
/*!40000 ALTER TABLE `password_resets` DISABLE KEYS */;
SET autocommit=0;
/*!40000 ALTER TABLE `password_resets` ENABLE KEYS */;
UNLOCK TABLES;
COMMIT;

--
-- Table structure for table `users`
--

/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `users`
--

LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
SET autocommit=0;
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
UNLOCK TABLES;
COMMIT;

/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on: Tue, 21 Jun 2016 09:51:42 +0000
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,7 @@ This is full options:
--dbconnect: Database connect to run
--force: Run without confirmation
--force: Run without confirmation (true/false)
--method: Name of method (mysqldump/php). If your server not install mysql (remote to other database server), you must select `php` method
```
32 changes: 23 additions & 9 deletions src/Schema/Console/Commands/DumpSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Autn\Schema\Console\Commands;

use Ifsnop\Mysqldump\Mysqldump as IMysqldump;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use PDO;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class DumpSql extends Command
{
Expand All @@ -16,7 +17,7 @@ class DumpSql extends Command
*
* @var string
*/
protected $signature = 'db:schema {--path= : Path to save file} {--dbconnect= : Name of database} {--force= : Force }';
protected $signature = 'db:schema {--path= : Path to save file} {--dbconnect= : Name of database} {--force= : Force (true/false) } {--method= : Name of method (mysqldump/php) }';

/**
* The console command description.
Expand Down Expand Up @@ -45,6 +46,7 @@ public function handle()
$pathparam = $this->option('path');
$dbconnect = $this->option('dbconnect');
$force = $this->option('force');
$method = $this->option('method');

if (!$dbconnect) {
$dbconnect = 'mysql';
Expand Down Expand Up @@ -83,12 +85,24 @@ public function handle()
Artisan::call('optimize');
Artisan::call('migrate:refresh', [ '--database' => $dbconnect, '--force' => true ]);

try {
exec("mysqldump --user=$username --password=$password --host=$host $database > " . $path . '/' . $filename);
if (!$method || $method == 'mysqldump') {
try {
exec("mysqldump --user=$username --password=$password --host=$host $database > " . $path . '/' . $filename);

$this->info('Generate successed, the file saved to: ' . $path . '/' . $filename);
} catch (Exception $e) {
$this->info($e->getMessage()); //@codeCoverageIgnore
$this->info('Generate successed, the file saved to: ' . $path . '/' . $filename);
} catch (Exception $e) {
$this->info($e->getMessage()); //@codeCoverageIgnore
}
} elseif ($method == 'php') {
try {
$dump = new IMysqldump("mysql:host=$host;dbname=$database", $username, $password);
$dump->start($path . '/' . $filename);
$this->info('Generate successed, the file saved to: ' . $path . '/' . $filename);
} catch (\Exception $e) {
$this->info('Mysqldump-php error: ' . $e->getMessage()); //@codeCoverageIgnore
}
} else {
$this->info('The method you selected does not support. You can select below methods: `mysqldump` or `php`');
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,28 @@ public function testWithPathAndDbconnectOptions()
fclose($schemaDbFile);
unlink(base_path() . '/database/factories/schema.sql');
}

public function testWithMethodOption()
{
$dbFile = fopen(base_path() . '/data/schema-php.sql', 'r');
$fileSize = filesize(base_path() . '/data/schema-php.sql');

Artisan::call('db:schema', ['--force' => true, '--method' => 'php']);

$this->assertTrue(file_exists(base_path() . '/database/schema.sql'));

$schemaDbFile = fopen(base_path() . '/database/schema.sql', 'r');
$schemaFileSize = filesize(base_path() . '/database/schema.sql');

while (($line = fgets($dbFile)) !== false) {
$schemaLine = fgets($schemaDbFile);
if (substr($schemaLine, 0, 2) !== '--' && substr($schemaLine, 0, 2) !== '/*') {
$this->assertEquals($line, $schemaLine);
}
}

fclose($dbFile);
fclose($schemaDbFile);
unlink(base_path() . '/database/schema.sql');
}
}

0 comments on commit ba330f9

Please sign in to comment.