Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dcblogdev committed Jun 23, 2022
1 parent d5a44d8 commit 320e71b
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
vendor
44 changes: 42 additions & 2 deletions README.md
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
```
34 changes: 34 additions & 0 deletions composer.json
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"
]
}
}
}
9 changes: 9 additions & 0 deletions config/dbsync.php
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', '')
];
30 changes: 30 additions & 0 deletions phpunit.xml
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>
46 changes: 46 additions & 0 deletions src/Console/DbSyncCommand.php
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");
}
}
}
37 changes: 37 additions & 0 deletions src/DbSyncServiceProvider.php
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,
]);
}
}
5 changes: 5 additions & 0 deletions tests/Pest.php
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__);
16 changes: 16 additions & 0 deletions tests/TestCase.php
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,
];
}
}
10 changes: 10 additions & 0 deletions tests/Unit/DbSyncTest.php
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?');
});

0 comments on commit 320e71b

Please sign in to comment.