1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
<?php
namespace Illuminate\Tests\Integration\Database;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\Attributes\RequiresDatabase;
class TimestampTypeTest extends DatabaseTestCase
{
public function testChangeDatetimeColumnToTimestampColumn()
{
Schema::create('test', function (Blueprint $table) {
$table->addColumn('datetime', 'datetime_to_timestamp');
});
Schema::table('test', function (Blueprint $table) {
$table->timestamp('datetime_to_timestamp')->nullable()->change();
});
$this->assertTrue(Schema::hasColumn('test', 'datetime_to_timestamp'));
// Only MySQL, MariaDB, and PostgreSQL actually have a timestamp type
$this->assertSame(
match ($this->driver) {
'mysql', 'mariadb', 'pgsql' => 'timestamp',
default => 'datetime',
},
Schema::getColumnType('test', 'datetime_to_timestamp')
);
}
public function testChangeTimestampColumnToDatetimeColumn()
{
Schema::create('test', function (Blueprint $table) {
$table->addColumn('timestamp', 'timestamp_to_datetime');
});
Schema::table('test', function (Blueprint $table) {
$table->dateTime('timestamp_to_datetime')->nullable()->change();
});
$this->assertTrue(Schema::hasColumn('test', 'timestamp_to_datetime'));
// Postgres only has a timestamp type
$this->assertSame(
match ($this->driver) {
'pgsql' => 'timestamp',
default => 'datetime',
},
Schema::getColumnType('test', 'timestamp_to_datetime')
);
}
#[RequiresDatabase(['mysql', 'mariadb'])]
public function testChangeStringColumnToTimestampColumn()
{
Schema::create('test', function (Blueprint $table) {
$table->string('string_to_timestamp');
});
$blueprint = new Blueprint('test', function ($table) {
$table->timestamp('string_to_timestamp')->nullable()->change();
});
$queries = $blueprint->toSql($this->getConnection(), $this->getConnection()->getSchemaGrammar());
$expected = ['alter table `test` modify `string_to_timestamp` timestamp null'];
$this->assertEquals($expected, $queries);
}
}
|