-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,42 @@ | ||
# laravel-db-sync | ||
Sync a remote DB to a local DB | ||
# DB Sync | ||
|
||
## Introduction | ||
Sync remote database to a local database | ||
|
||
## Install | ||
|
||
Install the package. | ||
|
||
```bash | ||
composer require dcblogdev/db-sync | ||
``` | ||
|
||
## Config | ||
|
||
You can publish the config file with: | ||
|
||
``` | ||
php artisan vendor:publish --provider="Dcblogdev\DbSync\DbSyncServiceProvider" --tag="config" | ||
``` | ||
|
||
## .env | ||
|
||
Set the remove database credentials in your .env file | ||
|
||
``` | ||
REMOTE_DATABASE_HOST=theonecrm.co.uk | ||
REMOTE_DATABASE_USERNAME= | ||
REMOTE_DATABASE_NAME= | ||
REMOTE_DATABASE_PASSWORD= | ||
REMOTE_DATABASE_IGNORE_TABLES='' | ||
``` | ||
|
||
Set a comma seperate list of tables NOT to export in `REMOTE_DATABASE_IGNORE_TABLES` | ||
|
||
## Usage | ||
|
||
To export a remote database to OVERRIDE your local database by running: | ||
|
||
```bash | ||
php artisan db:production-sync | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "dcblogdev/db-sync", | ||
"description": "Sync database down.", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "David Carr", | ||
"email": "dave@dcblog.dev" | ||
} | ||
], | ||
"require-dev": { | ||
"orchestra/testbench": "^5.0|^6.23|^7.0", | ||
"pestphp/pest": "^1.21", | ||
"pestphp/pest-plugin-laravel": "^1.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Dcblogdev\\DbSync\\": "src/", | ||
"Dcblogdev\\DbSync\\Tests\\": "tests" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"classmap": [ | ||
"tests/TestCase.php" | ||
] | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Dcblogdev\\DbSync\\DbSyncServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
return [ | ||
'host' => env('REMOTE_DATABASE_HOST', ''), | ||
'username' => env('REMOTE_DATABASE_USERNAME', ''), | ||
'database' => env('REMOTE_DATABASE_NAME', ''), | ||
'password' => env('REMOTE_DATABASE_PASSWORD', ''), | ||
'ignore' => env('REMOTE_DATABASE_IGNORE_TABLES', '') | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" | ||
> | ||
<coverage> | ||
<include> | ||
<directory suffix=".php">src/</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Unit"> | ||
<directory suffix="Test.php">./tests/Unit</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="DB_CONNECTION" value="testing"/> | ||
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/> | ||
</php> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Dcblogdev\DbSync\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class DbSyncCommand extends Command | ||
{ | ||
protected $signature = 'db:production-sync'; | ||
protected $description = 'Sync production database with local'; | ||
|
||
public function handle() | ||
{ | ||
if (app()->environment(['local', 'staging'])) { | ||
$host = config('dbsync.host'); | ||
$username = config('dbsync.username'); | ||
$database = config('dbsync.database'); | ||
$password = config('dbsync.password'); | ||
$ignore = config('dbsync.ignore'); | ||
$ignoreTables = explode(',', $ignore); | ||
|
||
if (empty($host) || empty($username) || empty($database)) { | ||
$this->error("DB credentials not set, have you published the config and set ENV variables?"); | ||
return true; | ||
} | ||
|
||
$ignoreString = null; | ||
foreach ($ignoreTables as $name) { | ||
$ignoreString .= " --ignore-table=$database.$name"; | ||
} | ||
|
||
// execute command | ||
exec("mysqldump -h $host -u $username -p$password $database --column-statistics=0 $ignoreString > file.sql", | ||
$output); | ||
$this->comment(implode(PHP_EOL, $output)); | ||
|
||
DB::unprepared(file_get_contents(base_path('file.sql'))); | ||
|
||
//delete files | ||
unlink('file.sql'); | ||
|
||
$this->comment("DB Synced"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Dcblogdev\DbSync; | ||
|
||
use Illuminate\Contracts\Support\DeferrableProvider; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class DbSyncServiceProvider extends ServiceProvider | ||
{ | ||
public function boot() | ||
{ | ||
if ($this->app->runningInConsole()) { | ||
|
||
$this->publishConfig(); | ||
$this->publishCommands(); | ||
} | ||
} | ||
|
||
public function provides() | ||
{ | ||
return [Console\DbSyncCommand::class]; | ||
} | ||
|
||
protected function publishConfig() | ||
{ | ||
$this->publishes([ | ||
__DIR__.'/../config/dbsync.php' => config_path('dbsync.php'), | ||
], 'config'); | ||
} | ||
|
||
protected function publishCommands() | ||
{ | ||
$this->commands([ | ||
Console\DbSyncCommand::class, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
use Dcblogdev\DbSync\Tests\TestCase; | ||
|
||
uses(TestCase::class)->in(__DIR__); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Dcblogdev\DbSync\Tests; | ||
|
||
use Orchestra\Testbench\TestCase as Orchestra; | ||
use TopPackages\DbSync\DbSyncServiceProvider; | ||
|
||
class TestCase extends Orchestra | ||
{ | ||
protected function getPackageProviders($app) | ||
{ | ||
return [ | ||
DbSyncServiceProvider::class, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Artisan; | ||
use Illuminate\Support\Facades\File; | ||
|
||
//TODO pass test | ||
test('demo', function () { | ||
$this->artisan('db:production-sync') | ||
->expectsOutput('DB credentials not set, have you published the config and set ENV variables?'); | ||
}); |