diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml index 056063f..c3ad22d 100644 --- a/.github/workflows/dependabot-auto-merge.yml +++ b/.github/workflows/dependabot-auto-merge.yml @@ -14,7 +14,7 @@ jobs: - name: Dependabot metadata id: metadata - uses: dependabot/fetch-metadata@v2.1.0 + uses: dependabot/fetch-metadata@v2.2.0 with: github-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/CHANGELOG.md b/CHANGELOG.md index ff1c5b8..a71c66e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ All notable changes to `laravel-schema-macros` will be documented in this file. +## v1.1.5 - 2024-07-03 + +### What's Changed + +* Bump dependabot/fetch-metadata from 1.6.0 to 2.0.0 by @dependabot in https://github.com/envor/laravel-schema-macros/pull/10 +* Bump aglipanci/laravel-pint-action from 2.3.1 to 2.4 by @dependabot in https://github.com/envor/laravel-schema-macros/pull/11 +* Bump dependabot/fetch-metadata from 2.0.0 to 2.1.0 by @dependabot in https://github.com/envor/laravel-schema-macros/pull/12 +* add pgsql by @inmanturbo in https://github.com/envor/laravel-schema-macros/pull/13 + +**Full Changelog**: https://github.com/envor/laravel-schema-macros/compare/v1.1.4...v1.1.5 + ## v1.1.4 - 2024-03-14 ### What's Changed diff --git a/src/MariaDb/MariaDbCopyTable.php b/src/MariaDb/MariaDbCopyTable.php new file mode 100644 index 0000000..de0da8c --- /dev/null +++ b/src/MariaDb/MariaDbCopyTable.php @@ -0,0 +1,39 @@ +hasTable($to)) { + $to = $to.'_copy'; + + return $copy($from, $to); + } + + /** @var \Illuminate\Database\Schema\MariaDbBuilder $this */ + return $this->getConnection()->statement("CREATE TABLE {$to} AS SELECT * FROM {$from}"); + }; + + return $copy($from, $to); + }; + } +} diff --git a/src/MySql/MySqlCopyTable.php b/src/MySql/MySqlCopyTable.php new file mode 100644 index 0000000..721285b --- /dev/null +++ b/src/MySql/MySqlCopyTable.php @@ -0,0 +1,39 @@ +hasTable($to)) { + $to = $to.'_copy'; + + return $copy($from, $to); + } + + /** @var \Illuminate\Database\Schema\MySqlBuilder $this */ + return $this->getConnection()->statement("CREATE TABLE {$to} AS SELECT * FROM {$from}"); + }; + + return $copy($from, $to); + }; + } +} diff --git a/src/SQLite/SQLiteCopyTable.php b/src/SQLite/SQLiteCopyTable.php new file mode 100644 index 0000000..29752c5 --- /dev/null +++ b/src/SQLite/SQLiteCopyTable.php @@ -0,0 +1,39 @@ +hasTable($to)) { + $to = $to.'_copy'; + + return $copy($from, $to); + } + + /** @var \Illuminate\Database\Schema\SQLiteBuilder $this */ + return $this->getConnection()->statement("CREATE TABLE {$to} AS SELECT * FROM {$from}"); + }; + + return $copy($from, $to); + }; + } +} diff --git a/src/SchemaMacrosServiceProvider.php b/src/SchemaMacrosServiceProvider.php index f54a9aa..55bc2ab 100644 --- a/src/SchemaMacrosServiceProvider.php +++ b/src/SchemaMacrosServiceProvider.php @@ -52,6 +52,11 @@ private function macros(): array 'mysql' => \Envor\SchemaMacros\MySql\MySqlEmptyTrash::class, 'mariadb' => \Envor\SchemaMacros\MariaDb\MariaDbEmptyTrash::class, ], + 'copyTable' => [ + 'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteCopyTable::class, + 'mysql' => \Envor\SchemaMacros\MySql\MySqlCopyTable::class, + 'mariadb' => \Envor\SchemaMacros\MariaDb\MariaDbCopyTable::class, + ], ]; } } diff --git a/tests/MariadbMacrosTest.php b/tests/MariadbMacrosTest.php index 0d9b2ce..5212c17 100644 --- a/tests/MariadbMacrosTest.php +++ b/tests/MariadbMacrosTest.php @@ -70,3 +70,20 @@ expect(DB::connection($this->connection)->getSchemaBuilder()->emptyTrash(2))->toBe(3); }); + +it('can copy table', function () { + DB::connection($this->connection)->getSchemaBuilder()->create('users', function ($table) { + $table->id(); + $table->string('name'); + $table->timestamps(); + }); + + DB::connection($this->connection)->table('users')->insert([ + ['name' => 'John Doe'], + ['name' => 'Jane Doe'], + ]); + + expect(DB::connection($this->connection)->getSchemaBuilder()->copyTable('users', 'users_copy'))->toBeTrue(); + + expect(DB::connection($this->connection)->table('users_copy')->get())->toHaveCount(2); +}); diff --git a/tests/SqliteMacrosTest.php b/tests/SqliteMacrosTest.php index 9a564b8..8d174e5 100644 --- a/tests/SqliteMacrosTest.php +++ b/tests/SqliteMacrosTest.php @@ -107,3 +107,20 @@ expect(Storage::disk('local')->files('.trash'))->toHaveCount(1); }); + +it('can copy a table', function () { + DB::connection($this->connection)->getSchemaBuilder()->create('users', function ($table) { + $table->id(); + $table->string('name'); + $table->timestamps(); + }); + + DB::connection($this->connection)->table('users')->insert([ + ['name' => 'John Doe'], + ['name' => 'Jane Doe'], + ]); + + expect(DB::connection($this->connection)->getSchemaBuilder()->copyTable('users', 'users_copy'))->toBeTrue(); + + expect(DB::connection($this->connection)->table('users_copy')->get())->toHaveCount(2); +});