Skip to content

Commit 24dafdb

Browse files
committed
Fix #133
Generate SQL Server user defined types
1 parent d85d579 commit 24dafdb

File tree

6 files changed

+63
-9
lines changed

6 files changed

+63
-9
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ The generator first generates all tables and then adds foreign keys to existing
142142
However, SQLite only supports foreign keys upon creation of the table and not when tables are altered.
143143
*_add_foreign_keys_* migrations will still be generated, however will get omitted if migrate to SQLite type database.
144144

145-
## PostgreSQL Custom Column Type
145+
## User Defined Custom Column Type
146146

147-
The generator will register custom data type from `pg_type`, then generate migration as
147+
The generator will register custom data type from the schema, then generate migration as
148148

149149
```php
150150
public function up()
@@ -158,6 +158,11 @@ public function up()
158158

159159
Note that the new `column` is always added at the end of the created `table` which means the ordering of the column generated in migration will differ from what we have from the schema.
160160

161+
Supported DB:
162+
163+
- [x] PostgreSQL
164+
- [x] SQL Server
165+
161166
## Thank You
162167

163168
Thanks to Bernhard Breytenbach for his great work. This package is cloned from https://github.com/Xethron/migrations-generator.

src/DBAL/Models/DBALCustomColumn.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ public function __construct(string $table, DoctrineDBALColumn $column)
3131
{
3232
$this->name = $column->getName();
3333
$this->tableName = $table;
34-
$this->sqls = DB::getDoctrineConnection()->getDatabasePlatform()->getAlterTableSQL(new TableDiff($this->tableName, [$column]));
34+
35+
// COLLATE clause cannot be used on user-defined data types.
36+
// Unset collation here.
37+
$platformOptions = $column->getPlatformOptions();
38+
unset($platformOptions['collation']);
39+
$column->setPlatformOptions($platformOptions);
40+
41+
$this->sqls = DB::getDoctrineConnection()->getDatabasePlatform()->getAlterTableSQL(new TableDiff($this->tableName, [$column]));
3542
}
3643

3744
/**

src/DBAL/RegisterColumnType.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
use KitLoong\MigrationsGenerator\Enum\Driver;
1313
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType;
1414
use KitLoong\MigrationsGenerator\Repositories\PgSQLRepository;
15+
use KitLoong\MigrationsGenerator\Repositories\SQLSrvRepository;
1516

1617
class RegisterColumnType
1718
{
1819
private $pgSQLRepository;
20+
private $sqlSrvRepository;
1921

20-
public function __construct(PgSQLRepository $pgSQLRepository)
22+
public function __construct(PgSQLRepository $pgSQLRepository, SQLSrvRepository $sqlSrvRepository)
2123
{
22-
$this->pgSQLRepository = $pgSQLRepository;
24+
$this->pgSQLRepository = $pgSQLRepository;
25+
$this->sqlSrvRepository = $sqlSrvRepository;
2326
}
2427

2528
/**
@@ -128,11 +131,16 @@ public function getName()
128131
*/
129132
private function getCustomTypes(): Collection
130133
{
131-
if (DB::getDriverName() === Driver::PGSQL()->getValue()) {
132-
return $this->pgSQLRepository->getCustomDataTypes();
133-
}
134+
switch (DB::getDriverName()) {
135+
case Driver::PGSQL():
136+
return $this->pgSQLRepository->getCustomDataTypes();
137+
138+
case Driver::SQLSRV():
139+
return $this->sqlSrvRepository->getCustomDataTypes();
134140

135-
return new Collection();
141+
default:
142+
return new Collection();
143+
}
136144
}
137145

138146
/**

src/Repositories/SQLSrvRepository.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,23 @@ public function getEnumPresetValues(string $table, string $column): Collection
194194

195195
return new Collection(array_reverse(explode('\' OR ' . $separator, $value)));
196196
}
197+
198+
/**
199+
* Get a list of custom data types.
200+
*
201+
* @return \Illuminate\Support\Collection<string>
202+
*/
203+
public function getCustomDataTypes(): Collection
204+
{
205+
$rows = DB::select("SELECT * FROM sys.types WHERE is_user_defined = 1");
206+
$types = new Collection();
207+
208+
if (count($rows) > 0) {
209+
foreach ($rows as $row) {
210+
$types->push($row->name);
211+
}
212+
}
213+
214+
return $types;
215+
}
197216
}

tests/Feature/SQLSrv/CommandTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function testRun()
3434
{
3535
$migrateTemplates = function () {
3636
$this->migrateGeneral('sqlsrv');
37+
38+
DB::statement(
39+
"ALTER TABLE all_columns_sqlsrv ADD accountnumber accountnumber NOT NULL"
40+
);
3741
};
3842

3943
$generateMigrations = function () {

tests/Feature/SQLSrv/SQLSrvTestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ protected function getEnvironmentSetUp($app)
3131
]);
3232
}
3333

34+
protected function setUp(): void
35+
{
36+
parent::setUp();
37+
38+
// Drop first.
39+
DB::statement("DROP TYPE IF EXISTS accountnumber");
40+
41+
// Create for custom column type test.
42+
DB::statement("CREATE TYPE accountnumber FROM [nvarchar](15) NULL");
43+
}
44+
3445
protected function dumpSchemaAs(string $destination): void
3546
{
3647
$tables = DB::getDoctrineSchemaManager()->listTableNames();

0 commit comments

Comments
 (0)