silverstripe /
silverstripe-postgresql
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\PostgreSQL\Tests; |
||
| 4 | |||
| 5 | use SilverStripe\Core\ClassInfo; |
||
| 6 | use SilverStripe\Dev\SapphireTest; |
||
| 7 | use SilverStripe\ORM\Connect\Database; |
||
| 8 | use SilverStripe\ORM\Connect\DatabaseException; |
||
| 9 | use SilverStripe\ORM\DB; |
||
| 10 | use SilverStripe\PostgreSQL\PostgreSQLConnector; |
||
| 11 | use SilverStripe\PostgreSQL\PostgreSQLSchemaManager; |
||
| 12 | |||
| 13 | class PostgreSQLSchemaManagerTest extends SapphireTest |
||
| 14 | { |
||
| 15 | |||
| 16 | protected $usesTransactions = false; |
||
| 17 | |||
| 18 | public function testAlterTable() |
||
| 19 | { |
||
| 20 | try { |
||
| 21 | /** @var PostgreSQLSchemaManager $dbSchema */ |
||
| 22 | $dbSchema = DB::get_schema(); |
||
| 23 | $dbSchema->quiet(); |
||
| 24 | |||
| 25 | $this->createSS3Table(); |
||
| 26 | |||
| 27 | try { |
||
| 28 | DB::query('INSERT INTO "ClassNamesUpgrade" ("ClassName") VALUES (\'App\MySite\FooBar\')'); |
||
| 29 | $this->assertFalse(true, 'SS3 Constaint should have blocked the previous insert.'); |
||
| 30 | } catch (DatabaseException $ex) { |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
| 31 | } |
||
| 32 | |||
| 33 | $dbSchema->schemaUpdate(function () use ($dbSchema) { |
||
| 34 | $dbSchema->requireTable( |
||
| 35 | 'ClassNamesUpgrade', |
||
| 36 | [ |
||
| 37 | 'ID' => 'PrimaryKey', |
||
| 38 | 'ClassName' => 'Enum(array("App\\\\MySite\\\\FooBar"))', |
||
| 39 | ] |
||
| 40 | ); |
||
| 41 | }); |
||
| 42 | |||
| 43 | DB::query('INSERT INTO "ClassNamesUpgrade" ("ClassName") VALUES (\'App\MySite\FooBar\')'); |
||
| 44 | $count = DB::query('SELECT count(*) FROM "ClassNamesUpgrade" WHERE "ClassName" = \'App\MySite\FooBar\'') |
||
| 45 | ->value(); |
||
| 46 | |||
| 47 | $this->assertEquals(1, $count); |
||
| 48 | } finally { |
||
| 49 | DB::query('DROP TABLE IF EXISTS "ClassNamesUpgrade"'); |
||
| 50 | DB::query('DROP SEQUENCE IF EXISTS "ClassNamesUpgrade_ID_seq"'); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | private function createSS3Table() |
||
| 55 | { |
||
| 56 | DB::query(<<<SQL |
||
| 57 | CREATE SEQUENCE "ClassNamesUpgrade_ID_seq" start 1 increment 1; |
||
| 58 | CREATE TABLE "ClassNamesUpgrade" |
||
| 59 | ( |
||
| 60 | "ID" bigint NOT NULL DEFAULT nextval('"ClassNamesUpgrade_ID_seq"'::regclass), |
||
| 61 | "ClassName" character varying(255) DEFAULT 'ClassNamesUpgrade'::character varying, |
||
| 62 | CONSTRAINT "ClassNamesUpgrade_pkey" PRIMARY KEY ("ID"), |
||
| 63 | CONSTRAINT "ClassNamesUpgrade_ClassName_check" CHECK ("ClassName"::text = ANY (ARRAY['FooBar'::character varying::text])) |
||
| 64 | ) |
||
| 65 | WITH ( |
||
| 66 | OIDS=FALSE |
||
| 67 | ); |
||
| 68 | SQL |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function testRenameTable() |
||
| 73 | { |
||
| 74 | try { |
||
| 75 | /** @var PostgreSQLSchemaManager $dbSchema */ |
||
| 76 | $dbSchema = DB::get_schema(); |
||
| 77 | $dbSchema->quiet(); |
||
| 78 | |||
| 79 | $this->createSS3VersionedTable(); |
||
| 80 | |||
| 81 | $this->assertConstraintCount(1, 'ClassNamesUpgrade_versioned_ClassName_check'); |
||
| 82 | |||
| 83 | $dbSchema->schemaUpdate(function () use ($dbSchema) { |
||
| 84 | $dbSchema->renameTable( |
||
| 85 | 'ClassNamesUpgrade_versioned', |
||
| 86 | 'ClassNamesUpgrade_Versioned' |
||
| 87 | ); |
||
| 88 | }); |
||
| 89 | |||
| 90 | $this->assertTableCount(0, 'ClassNamesUpgrade_versioned'); |
||
| 91 | $this->assertTableCount(1, 'ClassNamesUpgrade_Versioned'); |
||
| 92 | $this->assertConstraintCount(0, 'ClassNamesUpgrade_versioned_ClassName_check'); |
||
| 93 | $this->assertConstraintCount(1, 'ClassNamesUpgrade_Versioned_ClassName_check'); |
||
| 94 | } finally { |
||
| 95 | DB::query('DROP TABLE IF EXISTS "ClassNamesUpgrade_Versioned"'); |
||
| 96 | DB::query('DROP TABLE IF EXISTS "ClassNamesUpgrade_versioned"'); |
||
| 97 | DB::query('DROP SEQUENCE IF EXISTS "ClassNamesUpgrade_versioned_ID_seq"'); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | private function assertConstraintCount($expected, $constraintName) |
||
| 102 | { |
||
| 103 | $count = DB::prepared_query( |
||
| 104 | 'SELECT count(*) FROM pg_catalog.pg_constraint WHERE conname like ?', |
||
| 105 | [$constraintName] |
||
| 106 | )->value(); |
||
| 107 | |||
| 108 | $this->assertEquals($expected, $count); |
||
| 109 | } |
||
| 110 | |||
| 111 | private function assertTableCount($expected, $tableName) |
||
| 112 | { |
||
| 113 | $count = DB::prepared_query( |
||
| 114 | 'SELECT count(*) FROM pg_catalog.pg_tables WHERE "tablename" like ?', |
||
| 115 | [$tableName] |
||
| 116 | )->value(); |
||
| 117 | |||
| 118 | $this->assertEquals($expected, $count); |
||
| 119 | } |
||
| 120 | |||
| 121 | private function createSS3VersionedTable() |
||
| 122 | { |
||
| 123 | DB::query(<<<SQL |
||
| 124 | CREATE SEQUENCE "ClassNamesUpgrade_versioned_ID_seq" start 1 increment 1; |
||
| 125 | CREATE TABLE "ClassNamesUpgrade_versioned" |
||
| 126 | ( |
||
| 127 | "ID" bigint NOT NULL DEFAULT nextval('"ClassNamesUpgrade_versioned_ID_seq"'::regclass), |
||
| 128 | "ClassName" character varying(255) DEFAULT 'ClassNamesUpgrade'::character varying, |
||
| 129 | CONSTRAINT "ClassNamesUpgrade_pkey" PRIMARY KEY ("ID"), |
||
| 130 | CONSTRAINT "ClassNamesUpgrade_versioned_ClassName_check" CHECK ("ClassName"::text = ANY (ARRAY['FooBar'::character varying::text])) |
||
| 131 | ) |
||
| 132 | WITH ( |
||
| 133 | OIDS=FALSE |
||
| 134 | ); |
||
| 135 | SQL |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | } |
||
| 139 |