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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
<?php
namespace Illuminate\Tests\Integration\Database\Postgres;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* @requires extension pdo_pgsql
* @requires OS Linux|Darwin
*/
class PostgresSchemaBuilderTest extends PostgresTestCase
{
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);
$app['config']->set('database.connections.pgsql.search_path', 'public,private');
}
protected function defineDatabaseMigrations()
{
parent::defineDatabaseMigrations();
DB::statement('create schema if not exists private');
}
protected function destroyDatabaseMigrations()
{
DB::statement('drop table if exists public.table');
DB::statement('drop table if exists private.table');
DB::statement('drop view if exists public.foo');
DB::statement('drop view if exists private.foo');
DB::statement('drop schema private');
parent::destroyDatabaseMigrations();
}
public function testDropAllTablesOnAllSchemas()
{
Schema::create('public.table', function (Blueprint $table) {
$table->increments('id');
});
Schema::create('private.table', function (Blueprint $table) {
$table->increments('id');
});
Schema::dropAllTables();
$this->artisan('migrate:install');
$this->assertFalse(Schema::hasTable('public.table'));
$this->assertFalse(Schema::hasTable('private.table'));
}
public function testDropAllTablesUsesDontDropConfigOnAllSchemas()
{
$this->app['config']->set('database.connections.pgsql.dont_drop', ['spatial_ref_sys', 'table']);
DB::purge('pgsql');
Schema::create('public.table', function (Blueprint $table) {
$table->increments('id');
});
Schema::create('private.table', function (Blueprint $table) {
$table->increments('id');
});
Schema::dropAllTables();
$this->artisan('migrate:install');
$this->assertTrue(Schema::hasTable('public.table'));
$this->assertTrue(Schema::hasTable('private.table'));
}
public function testDropAllTablesUsesDontDropConfigOnOneSchema()
{
$this->app['config']->set('database.connections.pgsql.dont_drop', ['spatial_ref_sys', 'private.table']);
DB::purge('pgsql');
Schema::create('public.table', function (Blueprint $table) {
$table->increments('id');
});
Schema::create('private.table', function (Blueprint $table) {
$table->increments('id');
});
Schema::dropAllTables();
$this->artisan('migrate:install');
$this->assertFalse(Schema::hasTable('public.table'));
$this->assertTrue(Schema::hasTable('private.table'));
}
public function testDropAllViewsOnAllSchemas()
{
DB::statement('create view public.foo (id) as select 1');
DB::statement('create view private.foo (id) as select 1');
Schema::dropAllViews();
$this->assertFalse($this->hasView('public', 'foo'));
$this->assertFalse($this->hasView('private', 'foo'));
}
public function testAddTableCommentOnNewTable()
{
Schema::create('public.posts', function (Blueprint $table) {
$table->comment('This is a comment');
});
$this->assertEquals('This is a comment', DB::selectOne("select obj_description('public.posts'::regclass, 'pg_class')")->obj_description);
}
public function testAddTableCommentOnExistingTable()
{
Schema::create('public.posts', function (Blueprint $table) {
$table->id();
$table->comment('This is a comment');
});
Schema::table('public.posts', function (Blueprint $table) {
$table->comment('This is a new comment');
});
$this->assertEquals('This is a new comment', DB::selectOne("select obj_description('public.posts'::regclass, 'pg_class')")->obj_description);
}
public function testGetTables()
{
Schema::create('public.table', function (Blueprint $table) {
$table->string('name');
});
Schema::create('private.table', function (Blueprint $table) {
$table->integer('votes');
});
$tables = Schema::getTables();
$this->assertNotEmpty(array_filter($tables, function ($table) {
return $table['name'] === 'table' && $table['schema'] === 'public';
}));
$this->assertNotEmpty(array_filter($tables, function ($table) {
return $table['name'] === 'table' && $table['schema'] === 'private';
}));
}
public function testGetViews()
{
DB::statement('create view public.foo (id) as select 1');
DB::statement('create view private.foo (id) as select 1');
$views = Schema::getViews();
$this->assertNotEmpty(array_filter($views, function ($view) {
return $view['name'] === 'foo' && $view['schema'] === 'public';
}));
$this->assertNotEmpty(array_filter($views, function ($view) {
return $view['name'] === 'foo' && $view['schema'] === 'private';
}));
}
public function testDropPartitionedTables()
{
DB::statement('create table groups (id bigserial, tenant_id bigint, name varchar, primary key (id, tenant_id)) partition by hash (tenant_id)');
DB::statement('create table groups_1 partition of groups for values with (modulus 2, remainder 0)');
DB::statement('create table groups_2 partition of groups for values with (modulus 2, remainder 1)');
$tables = array_column(Schema::getTables(), 'name');
$this->assertContains('groups', $tables);
$this->assertContains('groups_1', $tables);
$this->assertContains('groups_2', $tables);
Schema::dropAllTables();
$this->artisan('migrate:install');
$tables = array_column(Schema::getTables(), 'name');
$this->assertNotContains('groups', $tables);
$this->assertNotContains('groups_1', $tables);
$this->assertNotContains('groups_2', $tables);
}
protected function hasView($schema, $table)
{
return DB::table('information_schema.views')
->where('table_catalog', $this->app['config']->get('database.connections.pgsql.database'))
->where('table_schema', $schema)
->where('table_name', $table)
->exists();
}
}
|