Skip to content

Commit 5da59b1

Browse files
Merge pull request michaeldyrynda#17 from maxcelos/master
Adding SQLite support
2 parents 8c7a89e + 7689999 commit 5da59b1

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/vendor
22
composer.lock
3+
.idea/
4+
.phpunit.result.cache

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ As of 3.0, this package _no longer overrides_ Laravel's default `uuid` method, b
1616

1717
> **Note**: This package purposely does not use [package discovery](https://laravel.com/docs/5.8/packages#package-discovery), as it makes changes to the MySQL schema file, which is something you should explicitly enable.
1818
19-
MySQL is the only supported connection type, only because I've no experience with other drivers. I welcome any pull requests to implement this functionality for other database drivers.
19+
MySQL and SQLite are the only supported connection types, only because I've no experience with other drivers. I welcome any pull requests to implement this functionality for other database drivers.
2020

2121
Note that `doctrine/dbal` does not appear to support changing existing `uuid` fields, and doing so would cause your existing values to be truncated in any event.
2222

src/Connection/SQLiteConnection.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Dyrynda\Database\Connection;
4+
5+
use Dyrynda\Database\Schema\Grammars\SQLiteGrammar;
6+
use Illuminate\Database\SQLiteConnection as BaseSQLiteConnection;
7+
8+
class SQLiteConnection extends BaseSQLiteConnection
9+
{
10+
/**
11+
* Get the default schema grammar instance.
12+
*
13+
* @return \Illuminate\Database\Grammar
14+
*/
15+
protected function getDefaultSchemaGrammar()
16+
{
17+
return $this->withTablePrefix(new SQLiteGrammar);
18+
}
19+
}

src/LaravelEfficientUuidServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\ServiceProvider;
77
use Illuminate\Database\Schema\Blueprint;
88
use Dyrynda\Database\Connection\MySqlConnection;
9+
use Dyrynda\Database\Connection\SQLiteConnection;
910

1011
class LaravelEfficientUuidServiceProvider extends ServiceProvider
1112
{
@@ -30,6 +31,10 @@ public function register()
3031
return new MySqlConnection($connection, $database, $prefix, $config);
3132
});
3233

34+
Connection::resolverFor('sqlite', function ($connection, $database, $prefix, $config) {
35+
return new SQLiteConnection($connection, $database, $prefix, $config);
36+
});
37+
3338
Blueprint::macro('efficientUuid', function ($column) {
3439
return $this->addColumn('efficientUuid', $column);
3540
});

src/Schema/Grammars/SQLiteGrammar.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Dyrynda\Database\Schema\Grammars;
4+
5+
use Illuminate\Support\Fluent;
6+
use Illuminate\Database\Schema\Grammars\SQLiteGrammar as IlluminateSQLiteGrammar;
7+
8+
class SQLiteGrammar extends IlluminateSQLiteGrammar
9+
{
10+
/**
11+
* Create the column definition for a UUID type.
12+
*
13+
* @param \Illuminate\Support\Fluent $column
14+
*
15+
* @return string
16+
*/
17+
protected function typeEfficientUuid(Fluent $column)
18+
{
19+
return 'blob(256)';
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Mockery as m;
6+
use Illuminate\Database\Connection;
7+
use Illuminate\Database\Schema\Blueprint;
8+
use Dyrynda\Database\Schema\Grammars\SQLiteGrammar;
9+
10+
class DatabaseSQLiteSchemaGrammarTest extends TestCase
11+
{
12+
public function tearDown(): void
13+
{
14+
m::close();
15+
}
16+
17+
public function testAddingUuid()
18+
{
19+
$blueprint = new Blueprint('users', function ($table) {
20+
$table->uuid('foo');
21+
$table->efficientUuid('bar');
22+
});
23+
24+
$connection = m::mock(Connection::class);
25+
26+
$this->assertEquals(
27+
['alter table "users" add column "foo" varchar not null', 'alter table "users" add column "bar" blob(256) not null'],
28+
$blueprint->toSql($connection, new SQLiteGrammar)
29+
);
30+
}
31+
}

0 commit comments

Comments
 (0)