-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[12.x] Add database dump with data + load #57682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
barryvdh
wants to merge
7
commits into
laravel:12.x
Choose a base branch
from
barryvdh:feat-database-dump
base: 12.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+313
−13
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
93b0f6d
Add database dump with data
barryvdh d83baac
Tweak withData state
barryvdh da04de0
Add sqlite
barryvdh 8be622d
Add load command
barryvdh 6e23e07
TWeak load command
barryvdh f2f92be
CS fix
barryvdh b388c05
Add postgres
barryvdh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,82 @@ | ||
| <?php | ||
|
|
||
| namespace Illuminate\Database\Console; | ||
|
|
||
| use Illuminate\Console\Command; | ||
| use Illuminate\Contracts\Events\Dispatcher; | ||
| use Illuminate\Database\Connection; | ||
| use Illuminate\Database\ConnectionResolverInterface; | ||
| use Illuminate\Database\Events\DatabaseDumped; | ||
| use Illuminate\Database\Events\MigrationsPruned; | ||
| use Illuminate\Database\Events\SchemaDumped; | ||
| use Illuminate\Filesystem\Filesystem; | ||
| use Illuminate\Support\Facades\Config; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
|
|
||
| #[AsCommand(name: 'db:dump')] | ||
| class DbDumpCommand extends Command | ||
| { | ||
| /** | ||
| * The console command name. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $signature = 'db:dump | ||
| {--database= : The database connection to use} | ||
| {--path= : The path where the schema dump file should be stored}'; | ||
|
|
||
| /** | ||
| * The console command description. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $description = 'Dump the given database schema and data'; | ||
|
|
||
| /** | ||
| * Execute the console command. | ||
| * | ||
| * @param \Illuminate\Database\ConnectionResolverInterface $connections | ||
| * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher | ||
| * @return void | ||
| */ | ||
| public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher) | ||
| { | ||
| $connection = $connections->connection($database = $this->input->getOption('database')); | ||
|
|
||
| $this->schemaState($connection)->dump( | ||
| $connection, $path = $this->path($connection), true | ||
| ); | ||
|
|
||
| $dispatcher->dispatch(new DatabaseDumped($connection, $path)); | ||
|
|
||
| $info = 'Database dumped'; | ||
|
|
||
| $this->components->info($info.' successfully to ' . $path); | ||
| } | ||
|
|
||
| /** | ||
| * Create a schema state instance for the given connection. | ||
| * | ||
| * @param \Illuminate\Database\Connection $connection | ||
| * @return mixed | ||
| */ | ||
| protected function schemaState(Connection $connection) | ||
| { | ||
| return $connection->getSchemaState() | ||
| ->handleOutputUsing(function ($type, $buffer) { | ||
| $this->output->write($buffer); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Get the path that the dump should be written to. | ||
| * | ||
| * @param \Illuminate\Database\Connection $connection | ||
| */ | ||
| protected function path(Connection $connection) | ||
| { | ||
| return tap($this->option('path') ?: storage_path($connection->getName() . '-' . date("Ymdhis").'.sql'), function ($path) { | ||
| (new Filesystem)->ensureDirectoryExists(dirname($path)); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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,40 @@ | ||
| <?php | ||
|
|
||
| namespace Illuminate\Database\Events; | ||
|
|
||
| class DatabaseDumped | ||
| { | ||
| /** | ||
| * The database connection instance. | ||
| * | ||
| * @var \Illuminate\Database\Connection | ||
| */ | ||
| public $connection; | ||
|
|
||
| /** | ||
| * The database connection name. | ||
| * | ||
| * @var string | ||
| */ | ||
| public $connectionName; | ||
|
|
||
| /** | ||
| * The path to the schema dump. | ||
| * | ||
| * @var string | ||
| */ | ||
| public $path; | ||
|
|
||
| /** | ||
| * Create a new event instance. | ||
| * | ||
| * @param \Illuminate\Database\Connection $connection | ||
| * @param string $path | ||
| */ | ||
| public function __construct($connection, $path) | ||
| { | ||
| $this->connection = $connection; | ||
| $this->connectionName = $connection->getName(); | ||
| $this->path = $path; | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would rename this to
$hasDataThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to a setting on the Schema, like the migration table.