Skip to content

Commit 7c69896

Browse files
author
Nicolas Boisvert
committed
Updated ReadMe
1 parent 87c2e12 commit 7c69896

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed

README.md

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,224 @@
11
# Laravel DB Import
2+
###### By Nicolas Boisvert :: [email protected]
3+
24
### Laravel database import package for easy manipulations
5+
6+
## Purposes
7+
8+
This package will help you import your production database. It can be use to migrate current production server to another server or the checkout the production database to test with the same datas, for example.
9+
10+
## Installation
11+
12+
Install it via composer
13+
```
14+
composer require nicklayb/laravel-db-import
15+
```
16+
17+
Register the service provider in `app.php`
18+
```
19+
Nicklayb\LaravelDbImport\ServiceProvider::class,
20+
```
21+
22+
And then you have to publish it.
23+
```
24+
php artisan vendor:publish
25+
```
26+
27+
## Configuration
28+
29+
### Creating import
30+
First of all you need to create your import class. You create a class in any namespace you want that extends the `Nicklayb\LaravelDbImport\Import` class. Here is a an example file, I created it in my commands namespace but feel free to put where you want to. See API part for more parameters
31+
```php
32+
<?php
33+
34+
namespace Foo\Console\Commands;
35+
36+
use Nicklayb\LaravelDbImport\Import;
37+
38+
class ProdImport extends Import
39+
{
40+
protected $sourceConnection = 'source';
41+
protected $destinationConnection = 'mysql';
42+
}
43+
```
44+
You must precise at least these two parameters. The must refer to registered databases in your `database.php` config file. Put your production database connection name in the `$sourceConnection` and the destination, guess what, in the `$destinationConnection` property. **I highly suggest connecting your source database with a read-only user to prevent mistake. Because if you put the source in the destination, you're gonnna have a bad time**.
45+
46+
#### Manipulations
47+
48+
If you want a table to be manipulated on import like, for instance, setting the timestamp to now, you can create a manipulation. You just define a method that begins with `manipulate` and with your table name in camel case format. The method receives an instance of the table row, you must return it but manipulated
49+
50+
```php
51+
<?php
52+
53+
namespace Foo\Console\Commands;
54+
55+
use Nicklayb\LaravelDbImport\Import;
56+
57+
class ProdImport extends Import
58+
{
59+
protected $sourceConnection = 'source';
60+
protected $destinationConnection = 'mysql';
61+
62+
public function manipulateUsers($user)
63+
{
64+
$user->created_at = Carbon::now();
65+
return $user;
66+
}
67+
}
68+
```
69+
70+
### Registering import
71+
72+
Since you published the vendor's file, you'll notice that you have a brand new `dbimport.php` in your config file. In this file you will register all of you import classes you want to use.
73+
```php
74+
<?php
75+
76+
return [
77+
/**
78+
* Register your databases imports class here by specifying a key
79+
* and the class to manage this import
80+
*
81+
* 'production' => Namespace\ProductionImport::class
82+
*/
83+
'imports' => [
84+
'prod' => Foo\Console\Commands\ProdImport::class
85+
]
86+
];
87+
```
88+
89+
### Executing import
90+
91+
**Before trying it, make sure you registered the good source and the good destination**
92+
93+
You will be able to call it with artisan using the following command
94+
```
95+
php artisan db:import prod
96+
```
97+
98+
The parameter `prod` of the command should match the key you registered in `dbimport.php`.
99+
100+
## API
101+
102+
Here's a list of properties and methods you can override matching your needs
103+
104+
```php
105+
<?php
106+
107+
class MyImport extends Import
108+
{
109+
/**
110+
* The key of the source connection created in the database config file
111+
*
112+
* @var string
113+
*/
114+
protected $sourceConnection = 'source';
115+
116+
/**
117+
* The key of the destination connection created in the database config file
118+
*
119+
* @var string
120+
*/
121+
protected $destinationConnection = 'destination';
122+
123+
/**
124+
* Password reset option, yout must specify the table of the users as
125+
* key and specify the new password as the value. Default column
126+
* is 'password' but override it by adding :column
127+
* 'users:column_password' => 'superpassword'
128+
*
129+
* @var array
130+
*/
131+
protected $resetPassword = [];
132+
133+
/**
134+
* Specify tables you don't want to import during the upload by specifying
135+
* the table name
136+
*
137+
* @var array
138+
*/
139+
protected $ignoreTables = [];
140+
141+
/**
142+
* Set the tables to import after all the others, this is useful when you
143+
* are dealing with foreign key constraints
144+
*
145+
* @var array
146+
*/
147+
protected $lastTables = [];
148+
149+
/**
150+
* Set this property to true to execute a php artisan migrate:refresh
151+
* before importing your database
152+
*
153+
* @var bool
154+
*/
155+
protected $refresh = false;
156+
157+
/**
158+
* Specify table by table the select statement of which column to load like
159+
* ['users' => ['firstname', 'lastname']]
160+
*
161+
* @var array
162+
*/
163+
protected $selects = [];
164+
165+
/**
166+
* Show table command, it may change depending on your database server
167+
*
168+
* @var string
169+
*/
170+
protected $showTablesCommand = 'SHOW TABLES';
171+
172+
/**
173+
* Key for default password when using reset passwords
174+
*
175+
* @var string
176+
*/
177+
protected $defaultPasswordColumn = 'password';
178+
179+
/**
180+
* Method that hashes password
181+
*
182+
* @param string $password
183+
* @return string
184+
*/
185+
public function hashPassword($password)
186+
{
187+
return bcrypt($password);
188+
}
189+
190+
/**
191+
* Fill the array with Closures to execute before starting the import
192+
*
193+
* @return array
194+
*/
195+
public function preImport()
196+
{
197+
return [];
198+
}
199+
200+
/**
201+
* Fill the array with Closures to execute after the import is done
202+
*
203+
* @return array
204+
*/
205+
public function postImport()
206+
{
207+
return [];
208+
}
209+
}
210+
```
211+
212+
## Conclusion
213+
214+
Thank you for using, testing and improving it and feel free to contact me for any question.
215+
216+
Ending joke :
217+
>I don't see women as objects, I consider each to be in a class of her own
218+
219+
## License
220+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
221+
222+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
223+
224+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)