|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SingleStore\Laravel\Tests\Hybrid; |
| 4 | + |
| 5 | +use Illuminate\Foundation\Application; |
| 6 | +use Illuminate\Support\Facades\Schema; |
| 7 | +use SingleStore\Laravel\Schema\Blueprint; |
| 8 | +use SingleStore\Laravel\Schema\Builder; |
| 9 | +use SingleStore\Laravel\Tests\BaseTest; |
| 10 | + |
| 11 | +class ChangeColumnTest extends BaseTest |
| 12 | +{ |
| 13 | + use HybridTestHelpers; |
| 14 | + |
| 15 | + /** @test */ |
| 16 | + public function change_column_on_rowstore_table() |
| 17 | + { |
| 18 | + if (version_compare(Application::VERSION, '10.0', '<')) { |
| 19 | + $this->markTestSkipped('requires higher laravel version'); |
| 20 | + } |
| 21 | + |
| 22 | + if ($this->runHybridIntegrations()) { |
| 23 | + $cached = $this->mockDatabaseConnection; |
| 24 | + |
| 25 | + $this->mockDatabaseConnection = false; |
| 26 | + |
| 27 | + if (method_exists(Builder::class, 'useNativeSchemaOperationsIfPossible')) { |
| 28 | + Schema::useNativeSchemaOperationsIfPossible(); |
| 29 | + } |
| 30 | + |
| 31 | + $this->createTable(function (Blueprint $table) { |
| 32 | + $table->rowstore(); |
| 33 | + $table->id(); |
| 34 | + $table->string('data'); |
| 35 | + }); |
| 36 | + |
| 37 | + Schema::table('test', function (Blueprint $table) { |
| 38 | + $table->text('data')->nullable()->change(); |
| 39 | + }); |
| 40 | + |
| 41 | + $this->assertEquals(['id', 'data'], Schema::getColumnListing('test')); |
| 42 | + |
| 43 | + if (version_compare(Application::VERSION, '10.30', '>=')) { |
| 44 | + $this->assertEquals('text', Schema::getColumnType('test', 'data')); |
| 45 | + } |
| 46 | + |
| 47 | + $this->mockDatabaseConnection = $cached; |
| 48 | + } |
| 49 | + |
| 50 | + $blueprint = new Blueprint('test'); |
| 51 | + $blueprint->text('data')->nullable()->change(); |
| 52 | + |
| 53 | + $connection = $this->getConnection(); |
| 54 | + $connection->shouldReceive('getDatabaseName')->andReturn('database'); |
| 55 | + $connection->shouldReceive('scalar') |
| 56 | + ->with("select exists (select 1 from information_schema.tables where table_schema = 'database' and table_name = 'test' and storage_type = 'COLUMNSTORE') as is_columnstore") |
| 57 | + ->andReturn(0); |
| 58 | + $connection->shouldReceive('usingNativeSchemaOperations')->andReturn(true); |
| 59 | + $statements = $blueprint->toSql($connection, $this->getGrammar()); |
| 60 | + |
| 61 | + $this->assertCount(1, $statements); |
| 62 | + $this->assertEquals('alter table `test` modify `data` text null', $statements[0]); |
| 63 | + } |
| 64 | + |
| 65 | + /** @test */ |
| 66 | + public function change_column_of_columnstore_table() |
| 67 | + { |
| 68 | + if (version_compare(Application::VERSION, '11.15', '<')) { |
| 69 | + $this->markTestSkipped('requires higher laravel version'); |
| 70 | + } |
| 71 | + |
| 72 | + if ($this->runHybridIntegrations()) { |
| 73 | + $cached = $this->mockDatabaseConnection; |
| 74 | + |
| 75 | + $this->mockDatabaseConnection = false; |
| 76 | + |
| 77 | + $this->createTable(function (Blueprint $table) { |
| 78 | + $table->id(); |
| 79 | + $table->string('data'); |
| 80 | + }); |
| 81 | + |
| 82 | + Schema::table('test', function (Blueprint $table) { |
| 83 | + $table->text('data')->nullable()->change(); |
| 84 | + }); |
| 85 | + |
| 86 | + $this->assertEquals(['id', 'data'], Schema::getColumnListing('test')); |
| 87 | + $this->assertEquals('text', Schema::getColumnType('test', 'data')); |
| 88 | + |
| 89 | + $this->mockDatabaseConnection = $cached; |
| 90 | + } |
| 91 | + |
| 92 | + $blueprint = new Blueprint('test'); |
| 93 | + $blueprint->text('data')->nullable()->change(); |
| 94 | + |
| 95 | + $connection = $this->getConnection(); |
| 96 | + $connection->shouldReceive('getDatabaseName')->andReturn('database'); |
| 97 | + $connection->shouldReceive('scalar') |
| 98 | + ->with("select exists (select 1 from information_schema.tables where table_schema = 'database' and table_name = 'test' and storage_type = 'COLUMNSTORE') as is_columnstore") |
| 99 | + ->andReturn(1); |
| 100 | + |
| 101 | + $statements = $blueprint->toSql($connection, $this->getGrammar()); |
| 102 | + |
| 103 | + $this->assertCount(4, $statements); |
| 104 | + $this->assertEquals([ |
| 105 | + 'alter table `test` add `__temp__data` text null after `data`', |
| 106 | + 'update `test` set `__temp__data` = `data`', |
| 107 | + 'alter table `test` drop `data`', |
| 108 | + 'alter table `test` change `__temp__data` `data`', |
| 109 | + ], $statements); |
| 110 | + } |
| 111 | +} |
0 commit comments