Skip to content

Commit 320e71b

Browse files
committed
first commit
1 parent d5a44d8 commit 320e71b

File tree

10 files changed

+231
-2
lines changed

10 files changed

+231
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
vendor

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,42 @@
1-
# laravel-db-sync
2-
Sync a remote DB to a local DB
1+
# DB Sync
2+
3+
## Introduction
4+
Sync remote database to a local database
5+
6+
## Install
7+
8+
Install the package.
9+
10+
```bash
11+
composer require dcblogdev/db-sync
12+
```
13+
14+
## Config
15+
16+
You can publish the config file with:
17+
18+
```
19+
php artisan vendor:publish --provider="Dcblogdev\DbSync\DbSyncServiceProvider" --tag="config"
20+
```
21+
22+
## .env
23+
24+
Set the remove database credentials in your .env file
25+
26+
```
27+
REMOTE_DATABASE_HOST=theonecrm.co.uk
28+
REMOTE_DATABASE_USERNAME=
29+
REMOTE_DATABASE_NAME=
30+
REMOTE_DATABASE_PASSWORD=
31+
REMOTE_DATABASE_IGNORE_TABLES=''
32+
```
33+
34+
Set a comma seperate list of tables NOT to export in `REMOTE_DATABASE_IGNORE_TABLES`
35+
36+
## Usage
37+
38+
To export a remote database to OVERRIDE your local database by running:
39+
40+
```bash
41+
php artisan db:production-sync
42+
```

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "dcblogdev/db-sync",
3+
"description": "Sync database down.",
4+
"type": "library",
5+
"authors": [
6+
{
7+
"name": "David Carr",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require-dev": {
12+
"orchestra/testbench": "^5.0|^6.23|^7.0",
13+
"pestphp/pest": "^1.21",
14+
"pestphp/pest-plugin-laravel": "^1.1"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Dcblogdev\\DbSync\\": "src/",
19+
"Dcblogdev\\DbSync\\Tests\\": "tests"
20+
}
21+
},
22+
"autoload-dev": {
23+
"classmap": [
24+
"tests/TestCase.php"
25+
]
26+
},
27+
"extra": {
28+
"laravel": {
29+
"providers": [
30+
"Dcblogdev\\DbSync\\DbSyncServiceProvider"
31+
]
32+
}
33+
}
34+
}

config/dbsync.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
'host' => env('REMOTE_DATABASE_HOST', ''),
5+
'username' => env('REMOTE_DATABASE_USERNAME', ''),
6+
'database' => env('REMOTE_DATABASE_NAME', ''),
7+
'password' => env('REMOTE_DATABASE_PASSWORD', ''),
8+
'ignore' => env('REMOTE_DATABASE_IGNORE_TABLES', '')
9+
];

phpunit.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
bootstrap="vendor/autoload.php"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
colors="true"
8+
verbose="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
processIsolation="false"
13+
stopOnFailure="false"
14+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
15+
>
16+
<coverage>
17+
<include>
18+
<directory suffix=".php">src/</directory>
19+
</include>
20+
</coverage>
21+
<testsuites>
22+
<testsuite name="Unit">
23+
<directory suffix="Test.php">./tests/Unit</directory>
24+
</testsuite>
25+
</testsuites>
26+
<php>
27+
<env name="DB_CONNECTION" value="testing"/>
28+
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
29+
</php>
30+
</phpunit>

src/Console/DbSyncCommand.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Dcblogdev\DbSync\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\DB;
7+
8+
class DbSyncCommand extends Command
9+
{
10+
protected $signature = 'db:production-sync';
11+
protected $description = 'Sync production database with local';
12+
13+
public function handle()
14+
{
15+
if (app()->environment(['local', 'staging'])) {
16+
$host = config('dbsync.host');
17+
$username = config('dbsync.username');
18+
$database = config('dbsync.database');
19+
$password = config('dbsync.password');
20+
$ignore = config('dbsync.ignore');
21+
$ignoreTables = explode(',', $ignore);
22+
23+
if (empty($host) || empty($username) || empty($database)) {
24+
$this->error("DB credentials not set, have you published the config and set ENV variables?");
25+
return true;
26+
}
27+
28+
$ignoreString = null;
29+
foreach ($ignoreTables as $name) {
30+
$ignoreString .= " --ignore-table=$database.$name";
31+
}
32+
33+
// execute command
34+
exec("mysqldump -h $host -u $username -p$password $database --column-statistics=0 $ignoreString > file.sql",
35+
$output);
36+
$this->comment(implode(PHP_EOL, $output));
37+
38+
DB::unprepared(file_get_contents(base_path('file.sql')));
39+
40+
//delete files
41+
unlink('file.sql');
42+
43+
$this->comment("DB Synced");
44+
}
45+
}
46+
}

src/DbSyncServiceProvider.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Dcblogdev\DbSync;
4+
5+
use Illuminate\Contracts\Support\DeferrableProvider;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class DbSyncServiceProvider extends ServiceProvider
9+
{
10+
public function boot()
11+
{
12+
if ($this->app->runningInConsole()) {
13+
14+
$this->publishConfig();
15+
$this->publishCommands();
16+
}
17+
}
18+
19+
public function provides()
20+
{
21+
return [Console\DbSyncCommand::class];
22+
}
23+
24+
protected function publishConfig()
25+
{
26+
$this->publishes([
27+
__DIR__.'/../config/dbsync.php' => config_path('dbsync.php'),
28+
], 'config');
29+
}
30+
31+
protected function publishCommands()
32+
{
33+
$this->commands([
34+
Console\DbSyncCommand::class,
35+
]);
36+
}
37+
}

tests/Pest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Dcblogdev\DbSync\Tests\TestCase;
4+
5+
uses(TestCase::class)->in(__DIR__);

tests/TestCase.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Dcblogdev\DbSync\Tests;
4+
5+
use Orchestra\Testbench\TestCase as Orchestra;
6+
use TopPackages\DbSync\DbSyncServiceProvider;
7+
8+
class TestCase extends Orchestra
9+
{
10+
protected function getPackageProviders($app)
11+
{
12+
return [
13+
DbSyncServiceProvider::class,
14+
];
15+
}
16+
}

tests/Unit/DbSyncTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Artisan;
4+
use Illuminate\Support\Facades\File;
5+
6+
//TODO pass test
7+
test('demo', function () {
8+
$this->artisan('db:production-sync')
9+
->expectsOutput('DB credentials not set, have you published the config and set ENV variables?');
10+
});

0 commit comments

Comments
 (0)