Skip to content

Commit ded1b97

Browse files
authored
Merge pull request #64 from kitloong/feature/fix
Bug fix
2 parents 3626e55 + c59c6d3 commit ded1b97

File tree

6 files changed

+63
-14
lines changed

6 files changed

+63
-14
lines changed

src/MigrationsGenerator/DBAL/Schema.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ public function initialize(): void
100100
$this->registerDoctrineTypeMapping('cidr', DBALTypes::STRING);
101101
$this->registerDoctrineTypeMapping('oid', DBALTypes::STRING);
102102
break;
103+
case Platform::SQLSERVER:
104+
$this->registerDoctrineTypeMapping('xml', DBALTypes::TEXT);
105+
break;
103106
default:
104107
}
105108
}

src/MigrationsGenerator/Generators/Modifier/IndexModifier.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Collection;
99
use MigrationsGenerator\Generators\Blueprint\Method;
1010
use MigrationsGenerator\Generators\IndexGenerator;
11+
use MigrationsGenerator\Generators\MigrationConstants\Method\IndexType;
1112

1213
class IndexModifier
1314
{
@@ -29,22 +30,34 @@ public function __construct(IndexGenerator $indexGenerator)
2930
*/
3031
public function chainIndex(Table $table, Method $method, Collection $singleColumnIndexes, Column $column): Method
3132
{
32-
if ($singleColumnIndexes->has($column->getName())) {
33-
/** @var Index $index */
34-
$index = $singleColumnIndexes->get($column->getName());
33+
if (!$singleColumnIndexes->has($column->getName())) {
34+
return $method;
35+
}
3536

36-
// autoIncrement is handled in IntegerColumn
37-
if ($index->isPrimary() && $column->getAutoincrement()) {
38-
return $method;
39-
}
37+
/** @var Index $index */
38+
$index = $singleColumnIndexes->get($column->getName());
39+
40+
// autoIncrement is handled in \MigrationsGenerator\Generators\Columns\IntegerColumn
41+
if ($index->isPrimary() && $column->getAutoincrement()) {
42+
return $method;
43+
}
4044

41-
$indexType = $this->indexGenerator->getIndexType($index);
45+
$indexType = $this->indexGenerator->getIndexType($index);
46+
if ($indexType === IndexType::SPATIAL_INDEX) {
4247
if ($this->indexGenerator->shouldSkipName($table->getName(), $index, $indexType)) {
4348
$method->chain($indexType);
44-
} else {
45-
$method->chain($indexType, $index->getName());
49+
return $method;
4650
}
51+
52+
return $method;
4753
}
54+
55+
if ($this->indexGenerator->shouldSkipName($table->getName(), $index, $indexType)) {
56+
$method->chain($indexType);
57+
return $method;
58+
}
59+
60+
$method->chain($indexType, $index->getName());
4861
return $method;
4962
}
5063
}

src/MigrationsGenerator/Generators/TableMigration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MigrationsGenerator\DBAL\Platform;
88
use MigrationsGenerator\Generators\Blueprint\SchemaBlueprint;
99
use MigrationsGenerator\Generators\Blueprint\TableBlueprint;
10+
use MigrationsGenerator\Generators\MigrationConstants\Method\IndexType;
1011
use MigrationsGenerator\Generators\MigrationConstants\Method\SchemaBuilder;
1112
use MigrationsGenerator\Generators\MigrationConstants\Property\TableProperty;
1213
use MigrationsGenerator\MigrationsGeneratorSetting;
@@ -70,6 +71,19 @@ public function up(Table $table, array $columns, array $indexes): SchemaBlueprin
7071
}
7172
}
7273

74+
// Generate single column spatial index with custom name.
75+
if ($singleColumnIndexes->isNotEmpty()) {
76+
foreach ($singleColumnIndexes as $index) {
77+
/** @var \Doctrine\DBAL\Schema\Index $index */
78+
$indexType = $this->indexGenerator->getIndexType($index);
79+
if ($indexType === IndexType::SPATIAL_INDEX &&
80+
!$this->indexGenerator->shouldSkipName($table->getName(), $index, $indexType)) {
81+
$method = $this->indexGenerator->generate($table, $index);
82+
$blueprint->setMethod($method);
83+
}
84+
}
85+
}
86+
7387
$up->setBlueprint($blueprint);
7488

7589
return $up;

tests/Feature/MySQL57/CommandTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public function testDefaultIndexNames()
194194
'test_index_mysql57_code_index',
195195
'test_index_mysql57_column_hyphen_index',
196196
'test_index_mysql57_custom_name_index',
197+
'test_index_mysql57_custom_spatial_index_spatialindex',
197198
'test_index_mysql57_email_unique',
198199
'test_index_mysql57_enum_code_unique',
199200
'test_index_mysql57_line_string_spatialindex',
@@ -221,7 +222,10 @@ public function testDefaultFKNames()
221222
$foreignKeys = new Collection($foreignKeyArray);
222223
$foreignKeyNames = $foreignKeys->map(function (ForeignKeyConstraint $foreignKey) {
223224
return $foreignKey->getName();
224-
})->sort()->toArray();
225+
})
226+
->sort()
227+
->values()
228+
->toArray();
225229

226230
$this->assertSame(
227231
[

tests/Feature/SQLSrv/CommandTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ public function testCollation()
6363
$this->verify($migrateTemplates, $generateMigrations);
6464
}
6565

66+
public function testGenerateXml()
67+
{
68+
$this->migrateGeneral('sqlsrv');
69+
70+
// Test xml column
71+
DB::statement("alter table all_columns_sqlsrv add xml xml");
72+
73+
$this->truncateMigration();
74+
75+
$this->generateMigrations();
76+
77+
$this->assertTrue(true);
78+
}
79+
6680
/**
6781
* @param callable $migrateTemplates
6882
* @param callable $generateMigrations

tests/resources/database/migrations/general/2020_03_21_000000_expected_create_test_index_db_table.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@ class ExpectedCreateTestIndex_DB_Table extends Migration
1717
public function up()
1818
{
1919
Schema::create('test_index_[db]', function (Blueprint $table) {
20-
$table->integer('id');
20+
$table->increments('id');
2121
$table->string('code', 50)->index();
2222
$table->string('custom_name', 50)->index('custom_index');
2323
$table->string('email')->unique();
2424
$table->string('column-hyphen')->index();
2525
$table->enum('enum', ['PROGRESS', 'DONE']);
2626
$table->lineString('line_string')->spatialIndex();
27+
$table->lineString('custom_spatial_index');
2728
$table->timestamp('created_at')->nullable();
2829

29-
$table->primary('id');
3030
$table->index(['code', 'enum']);
31-
$table->index(['code', 'email'], 'custom_multi_key_index');
31+
$table->index(['code', 'email'], 'custom_multi_key_index_name');
3232
$table->unique(['enum', 'code']);
33+
$table->spatialIndex('custom_spatial_index', 'custom_spatial_index_name');
3334
});
3435
}
3536

0 commit comments

Comments
 (0)