Skip to content

Commit

Permalink
Merge pull request #15 from envor/main
Browse files Browse the repository at this point in the history
add copy function
  • Loading branch information
inmanturbo authored Jul 30, 2024
2 parents 8e4c2f2 + 7c3eaf1 commit c742e03
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"

Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions src/MariaDb/MariaDbCopyTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Envor\SchemaMacros\MariaDb;

use Stringable;

/**
* determine if the database exists
*
* @param string|Stringable $database
*
* @mixin \Illuminate\Database\Schema\MariaDbBuilder
*
* @return bool
*/
class MariaDbCopyTable
{
public function __invoke(): callable
{
return function (string $from, ?string $to = null): mixed {
$from = (string) $from;
$to = $to ? (string) $to : $from.'_copy';

$copy = function ($from, $to) use (&$copy) {
/** @var \Illuminate\Database\Schema\MariaDbBuilder $this */
if ($this->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);
};
}
}
39 changes: 39 additions & 0 deletions src/MySql/MySqlCopyTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Envor\SchemaMacros\MySql;

use Stringable;

/**
* determine if the database exists
*
* @param string|Stringable $database
*
* @mixin \Illuminate\Database\Schema\MySqlBuilder
*
* @return bool
*/
class MySqlCopyTable
{
public function __invoke(): callable
{
return function (string $from, ?string $to = null): mixed {
$from = (string) $from;
$to = $to ? (string) $to : $from.'_copy';

$copy = function ($from, $to) use (&$copy) {
/** @var \Illuminate\Database\Schema\MySqlBuilder $this */
if ($this->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);
};
}
}
39 changes: 39 additions & 0 deletions src/SQLite/SQLiteCopyTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Envor\SchemaMacros\SQLite;

use Stringable;

/**
* determine if the database exists
*
* @param string|Stringable $database
*
* @mixin \Illuminate\Database\Schema\SQLiteBuilder
*
* @return bool
*/
class SQLiteCopyTable
{
public function __invoke(): callable
{
return function (string $from, ?string $to = null): mixed {
$from = (string) $from;
$to = $to ? (string) $to : $from.'_copy';

$copy = function ($from, $to) use (&$copy) {
/** @var \Illuminate\Database\Schema\SQLiteBuilder $this */
if ($this->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);
};
}
}
5 changes: 5 additions & 0 deletions src/SchemaMacrosServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
}
}
17 changes: 17 additions & 0 deletions tests/MariadbMacrosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
17 changes: 17 additions & 0 deletions tests/SqliteMacrosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit c742e03

Please sign in to comment.