{"id":26964429,"url":"https://github.com/nicklayb/laravel-db-import","last_synced_at":"2025-04-03T06:32:21.210Z","repository":{"id":57026718,"uuid":"83696350","full_name":"nicklayb/laravel-db-import","owner":"nicklayb","description":"Import command for managing database import in Laravel","archived":false,"fork":false,"pushed_at":"2019-10-24T15:23:13.000Z","size":45,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-08T07:37:00.263Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nicklayb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-02T15:57:46.000Z","updated_at":"2020-08-17T19:26:26.000Z","dependencies_parsed_at":"2022-08-23T16:20:37.455Z","dependency_job_id":null,"html_url":"https://github.com/nicklayb/laravel-db-import","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklayb%2Flaravel-db-import","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklayb%2Flaravel-db-import/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklayb%2Flaravel-db-import/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklayb%2Flaravel-db-import/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nicklayb","download_url":"https://codeload.github.com/nicklayb/laravel-db-import/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246948013,"owners_count":20859362,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-03T06:31:26.751Z","updated_at":"2025-04-03T06:32:21.196Z","avatar_url":"https://github.com/nicklayb.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel DB Import\n###### By Nicolas Boisvert :: nicklay@me.com\n\n[![Build Status](https://travis-ci.org/nicklayb/laravel-db-import.svg?branch=master)](https://travis-ci.org/nicklayb/laravel-db-import)\n\n### Laravel database import package for easy manipulations\n\n## Purposes\n\nThis 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.\n\n## Installation\n\nInstall it via composer\n```\ncomposer require nicklayb/laravel-db-import\n```\n\nRegister the service provider in `app.php`\n```\nNicklayb\\LaravelDbImport\\ServiceProvider::class,\n```\n\nAnd then you have to publish it.\n```\nphp artisan vendor:publish\n```\n\n## Configuration\n\n### Creating import\nFirst 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\n```php\n\u003c?php\n\nnamespace Foo\\Console\\Commands;\n\nuse Nicklayb\\LaravelDbImport\\Import;\n\nclass ProdImport extends Import\n{\n    protected $sourceConnection = 'source';\n    protected $destinationConnection = 'mysql';\n}\n```\nYou 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**.\n\n#### Manipulations\n\nIf 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\n\n```php\n\u003c?php\n\nnamespace Foo\\Console\\Commands;\n\nuse Nicklayb\\LaravelDbImport\\Import;\n\nclass ProdImport extends Import\n{\n    protected $sourceConnection = 'source';\n    protected $destinationConnection = 'mysql';\n\n    public function manipulateUsers($user)\n    {\n        $user-\u003ecreated_at = Carbon::now();\n        return $user;\n    }\n}\n```\n\n#### Table filter\n\nSometimes you may need to filter tables to get only certain items like, for instance, only the last 6 months of work. This may be achieved by adding table filters.\n\nLet say I have a table called `orders` where I only need the last 6 months items.\n\n```\nnamespace Foo\\Console\\Commands;\n\nuse Nicklayb\\LaravelDbImport\\Import;\n\nclass ProdImport extends Import\n{\n    protected $sourceConnection = 'source';\n    protected $destinationConnection = 'mysql';\n\n    public function filterUsers($query)\n    {\n        return $query-\u003ewhere('created_at', '\u003e', Carbon::now()-\u003esubMonths(6));\n    }\n}\n```\n\nYou will receive the base query in parameter and you should return the modified query.\n\n### Registering import\n\nSince 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.\n```php\n\u003c?php\n\nreturn [\n    /**\n     * Register your databases imports class here by specifying a key\n     * and the class to manage this import\n     *\n     *  'production' =\u003e Namespace\\ProductionImport::class\n     */\n    'imports' =\u003e [\n        'prod' =\u003e Foo\\Console\\Commands\\ProdImport::class\n    ]\n];\n```\n\n### Executing import\n\n**Before trying it, make sure you registered the good source and the good destination**\n\nYou will be able to call it with artisan using the following command\n```\nphp artisan db:import prod\n```\n\nThe parameter `prod` of the command should match the key you registered in `dbimport.php`.\n\n## API\n\nHere's a list of properties and methods you can override matching your needs\n\n```php\n\u003c?php\n\nclass MyImport extends Import\n{\n    /**\n     * The key of the source connection created in the database config file\n     *\n     * @var string\n     */\n    protected $sourceConnection = 'source';\n\n    /**\n     * The key of the destination connection created in the database config file\n     *\n     * @var string\n     */\n    protected $destinationConnection = 'destination';\n\n    /**\n     * Password reset option, yout must specify the table of the users as\n     * key and specify the new password as the value. Default column\n     * is 'password' but override it by adding :column\n     * 'users:column_password' =\u003e 'superpassword'\n     *\n     * @var array\n     */\n    protected $resetPassword = [];\n\n    /**\n     * Specify tables you don't want to import during the upload by specifying\n     * the table name\n     *\n     * @var array\n     */\n    protected $ignoreTables = [];\n\n    /**\n     * Set the tables to import after all the others, this is useful when you\n     * are dealing with foreign key constraints\n     *\n     * @var array\n     */\n    protected $lastTables = [];\n\n    /**\n     * Set this property to true to execute a php artisan migrate:refresh\n     * before importing your database\n     *\n     * @var bool\n     */\n    protected $refresh = false;\n\n    /**\n     * Specify table by table the select statement of which column to load like\n     * ['users' =\u003e ['firstname', 'lastname']]\n     *\n     * @var array\n     */\n    protected $selects = [];\n\n    /**\n     * Show table command, it may change depending on your database server\n     *\n     * @var string\n     */\n    protected $showTablesCommand = 'SHOW TABLES';\n\n    /**\n     * Key for default password when using reset passwords\n     *\n     * @var string\n     */\n    protected $defaultPasswordColumn = 'password';\n\n    /**\n     * Method that hashes password\n     *\n     * @param string $password\n     * @return string\n     */\n    public function hashPassword($password)\n    {\n        return bcrypt($password);\n    }\n\n    /**\n     * Fill the array with Closures to execute before starting the import\n     *\n     * @return array\n     */\n    public function preImport()\n    {\n        return [];\n    }\n\n    /**\n     * Fill the array with Closures to execute after the import is done\n     *\n     * @return array\n     */\n    public function postImport()\n    {\n        return [];\n    }\n}\n```\n\n## Conclusion\n\nThank you for using, testing and improving it and feel free to contact me for any question.\n\nEnding joke :\n\u003eI don't see women as objects, I consider each to be in a class of her own\n\n## License\nPermission 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:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicklayb%2Flaravel-db-import","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnicklayb%2Flaravel-db-import","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicklayb%2Flaravel-db-import/lists"}