|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional\SchemaTool; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
|
6
|
|
|
use Doctrine\Tests\Models; |
|
7
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
|
8
|
|
|
|
|
9
|
|
|
class PostgreSqlSchemaToolTest extends OrmFunctionalTestCase |
|
10
|
|
|
{ |
|
11
|
|
|
protected function setUp() |
|
12
|
|
|
{ |
|
13
|
|
|
parent::setUp(); |
|
14
|
|
|
|
|
15
|
|
|
if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'postgresql') { |
|
16
|
|
|
$this->markTestSkipped('The ' . __CLASS__ .' requires the use of postgresql.'); |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function testPostgresMetadataSequenceIncrementedBy10() |
|
21
|
|
|
{ |
|
22
|
|
|
$address = $this->_em->getClassMetadata(Models\CMS\CmsAddress::class); |
|
23
|
|
|
|
|
24
|
|
|
$this->assertEquals(1, $address->sequenceGeneratorDefinition['allocationSize']); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testGetCreateSchemaSql() |
|
28
|
|
|
{ |
|
29
|
|
|
$classes = [ |
|
30
|
|
|
$this->_em->getClassMetadata(Models\CMS\CmsAddress::class), |
|
31
|
|
|
$this->_em->getClassMetadata(Models\CMS\CmsUser::class), |
|
32
|
|
|
$this->_em->getClassMetadata(Models\CMS\CmsPhonenumber::class), |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
$tool = new SchemaTool($this->_em); |
|
36
|
|
|
$sql = $tool->getCreateSchemaSql($classes); |
|
37
|
|
|
$sqlCount = count($sql); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertEquals("CREATE TABLE cms_addresses (id INT NOT NULL, user_id INT DEFAULT NULL, country VARCHAR(50) NOT NULL, zip VARCHAR(50) NOT NULL, city VARCHAR(50) NOT NULL, PRIMARY KEY(id))", array_shift($sql)); |
|
40
|
|
|
$this->assertEquals("CREATE UNIQUE INDEX UNIQ_ACAC157BA76ED395 ON cms_addresses (user_id)", array_shift($sql)); |
|
41
|
|
|
$this->assertEquals("CREATE TABLE cms_users (id INT NOT NULL, email_id INT DEFAULT NULL, status VARCHAR(50) DEFAULT NULL, username VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id))", array_shift($sql)); |
|
42
|
|
|
$this->assertEquals("CREATE UNIQUE INDEX UNIQ_3AF03EC5F85E0677 ON cms_users (username)", array_shift($sql)); |
|
43
|
|
|
$this->assertEquals("CREATE UNIQUE INDEX UNIQ_3AF03EC5A832C1C9 ON cms_users (email_id)", array_shift($sql)); |
|
44
|
|
|
$this->assertEquals("CREATE TABLE cms_users_groups (user_id INT NOT NULL, group_id INT NOT NULL, PRIMARY KEY(user_id, group_id))", array_shift($sql)); |
|
45
|
|
|
$this->assertEquals("CREATE INDEX IDX_7EA9409AA76ED395 ON cms_users_groups (user_id)", array_shift($sql)); |
|
46
|
|
|
$this->assertEquals("CREATE INDEX IDX_7EA9409AFE54D947 ON cms_users_groups (group_id)", array_shift($sql)); |
|
47
|
|
|
$this->assertEquals("CREATE TABLE cms_users_tags (user_id INT NOT NULL, tag_id INT NOT NULL, PRIMARY KEY(user_id, tag_id))", array_shift($sql)); |
|
48
|
|
|
$this->assertEquals("CREATE INDEX IDX_93F5A1ADA76ED395 ON cms_users_tags (user_id)", array_shift($sql)); |
|
49
|
|
|
$this->assertEquals("CREATE INDEX IDX_93F5A1ADBAD26311 ON cms_users_tags (tag_id)", array_shift($sql)); |
|
50
|
|
|
$this->assertEquals("CREATE TABLE cms_phonenumbers (phonenumber VARCHAR(50) NOT NULL, user_id INT DEFAULT NULL, PRIMARY KEY(phonenumber))", array_shift($sql)); |
|
51
|
|
|
$this->assertEquals("CREATE INDEX IDX_F21F790FA76ED395 ON cms_phonenumbers (user_id)", array_shift($sql)); |
|
52
|
|
|
$this->assertEquals("CREATE SEQUENCE cms_addresses_id_seq INCREMENT BY 1 MINVALUE 1 START 1", array_shift($sql)); |
|
53
|
|
|
$this->assertEquals("CREATE SEQUENCE cms_users_id_seq INCREMENT BY 1 MINVALUE 1 START 1", array_shift($sql)); |
|
54
|
|
|
$this->assertEquals("ALTER TABLE cms_addresses ADD CONSTRAINT FK_ACAC157BA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); |
|
55
|
|
|
$this->assertEquals("ALTER TABLE cms_users ADD CONSTRAINT FK_3AF03EC5A832C1C9 FOREIGN KEY (email_id) REFERENCES cms_emails (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); |
|
56
|
|
|
$this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); |
|
57
|
|
|
$this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AFE54D947 FOREIGN KEY (group_id) REFERENCES cms_groups (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); |
|
58
|
|
|
$this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); |
|
59
|
|
|
$this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADBAD26311 FOREIGN KEY (tag_id) REFERENCES cms_tags (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); |
|
60
|
|
|
$this->assertEquals("ALTER TABLE cms_phonenumbers ADD CONSTRAINT FK_F21F790FA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertEquals([], $sql, "SQL Array should be empty now."); |
|
63
|
|
|
$this->assertEquals(22, $sqlCount, "Total of 22 queries should be executed"); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testGetCreateSchemaSql2() |
|
67
|
|
|
{ |
|
68
|
|
|
$classes = [ |
|
69
|
|
|
$this->_em->getClassMetadata(Models\Generic\DecimalModel::class) |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
$tool = new SchemaTool($this->_em); |
|
73
|
|
|
$sql = $tool->getCreateSchemaSql($classes); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertEquals(2, count($sql)); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertEquals('CREATE TABLE decimal_model (id INT NOT NULL, "decimal" NUMERIC(5, 2) NOT NULL, "high_scale" NUMERIC(14, 4) NOT NULL, PRIMARY KEY(id))', $sql[0]); |
|
78
|
|
|
$this->assertEquals("CREATE SEQUENCE decimal_model_id_seq INCREMENT BY 1 MINVALUE 1 START 1", $sql[1]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testGetCreateSchemaSql3() |
|
82
|
|
|
{ |
|
83
|
|
|
$classes = [ |
|
84
|
|
|
$this->_em->getClassMetadata(Models\Generic\BooleanModel::class) |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
$tool = new SchemaTool($this->_em); |
|
88
|
|
|
$sql = $tool->getCreateSchemaSql($classes); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals(2, count($sql)); |
|
91
|
|
|
$this->assertEquals("CREATE TABLE boolean_model (id INT NOT NULL, booleanField BOOLEAN NOT NULL, PRIMARY KEY(id))", $sql[0]); |
|
92
|
|
|
$this->assertEquals("CREATE SEQUENCE boolean_model_id_seq INCREMENT BY 1 MINVALUE 1 START 1", $sql[1]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testGetDropSchemaSql() |
|
96
|
|
|
{ |
|
97
|
|
|
$classes = [ |
|
98
|
|
|
$this->_em->getClassMetadata(Models\CMS\CmsAddress::class), |
|
99
|
|
|
$this->_em->getClassMetadata(Models\CMS\CmsUser::class), |
|
100
|
|
|
$this->_em->getClassMetadata(Models\CMS\CmsPhonenumber::class), |
|
101
|
|
|
]; |
|
102
|
|
|
|
|
103
|
|
|
$tool = new SchemaTool($this->_em); |
|
104
|
|
|
$sql = $tool->getDropSchemaSQL($classes); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertEquals(17, count($sql)); |
|
107
|
|
|
|
|
108
|
|
|
$dropSequenceSQLs = 0; |
|
109
|
|
|
|
|
110
|
|
|
foreach ($sql AS $stmt) { |
|
111
|
|
|
if (strpos($stmt, "DROP SEQUENCE") === 0) { |
|
112
|
|
|
$dropSequenceSQLs++; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
$this->assertEquals(4, $dropSequenceSQLs, "Expect 4 sequences to be dropped."); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @group DDC-1657 |
|
120
|
|
|
*/ |
|
121
|
|
|
public function testUpdateSchemaWithPostgreSQLSchema() |
|
122
|
|
|
{ |
|
123
|
|
|
$classes = [ |
|
124
|
|
|
$this->_em->getClassMetadata(DDC1657Screen::class), |
|
125
|
|
|
$this->_em->getClassMetadata(DDC1657Avatar::class), |
|
126
|
|
|
]; |
|
127
|
|
|
|
|
128
|
|
|
$tool = new SchemaTool($this->_em); |
|
129
|
|
|
$tool->createSchema($classes); |
|
130
|
|
|
|
|
131
|
|
|
$sql = $tool->getUpdateSchemaSql($classes); |
|
132
|
|
|
$sql = array_filter($sql, function($sql) { return (strpos($sql, "DROP SEQUENCE stonewood.") === 0); }); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertCount(0, $sql, implode("\n", $sql)); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function testSchemaFromMetadataNamespaces() : void |
|
138
|
|
|
{ |
|
139
|
|
|
$schemaTool = new SchemaTool($this->_em); |
|
140
|
|
|
$platform = $this->_em->getConnection()->getDatabasePlatform(); |
|
141
|
|
|
$classes = [ |
|
142
|
|
|
$this->_em->getClassMetadata(Models\CMS\CmsUser::class), |
|
143
|
|
|
]; |
|
144
|
|
|
|
|
145
|
|
|
$schemaWithoutNamespaces = $schemaTool->getSchemaFromMetadata($classes); |
|
146
|
|
|
self::assertNotContains('CREATE SCHEMA public', $schemaWithoutNamespaces->toSql($platform)); |
|
147
|
|
|
|
|
148
|
|
|
$schemaWithNamespaces = $schemaTool->getSchemaFromMetadata($classes, true); |
|
149
|
|
|
self::assertContains('CREATE SCHEMA public', $schemaWithNamespaces->toSql($platform)); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @Entity |
|
155
|
|
|
* @Table(name="stonewood.screen") |
|
156
|
|
|
*/ |
|
157
|
|
|
class DDC1657Screen |
|
158
|
|
|
{ |
|
159
|
|
|
/** |
|
160
|
|
|
* Identifier |
|
161
|
|
|
* @var int |
|
162
|
|
|
* |
|
163
|
|
|
* @Id |
|
164
|
|
|
* @GeneratedValue(strategy="IDENTITY") |
|
165
|
|
|
* @Column(name="pk", type="integer", nullable=false) |
|
166
|
|
|
*/ |
|
167
|
|
|
private $pk; |
|
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Title |
|
171
|
|
|
* @var string |
|
172
|
|
|
* |
|
173
|
|
|
* @Column(name="title", type="string", length=255, nullable=false) |
|
174
|
|
|
*/ |
|
175
|
|
|
private $title; |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Path |
|
179
|
|
|
* @var string |
|
180
|
|
|
* |
|
181
|
|
|
* @Column(name="path", type="string", length=255, nullable=false) |
|
182
|
|
|
*/ |
|
183
|
|
|
private $path; |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Register date |
|
187
|
|
|
* @var Date |
|
|
|
|
|
|
188
|
|
|
* |
|
189
|
|
|
* @Column(name="ddate", type="date", nullable=false) |
|
190
|
|
|
*/ |
|
191
|
|
|
private $ddate; |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Avatar |
|
195
|
|
|
* @var Stonewood\Model\Entity\Avatar |
|
|
|
|
|
|
196
|
|
|
* |
|
197
|
|
|
* @ManyToOne(targetEntity="DDC1657Avatar") |
|
198
|
|
|
* @JoinColumn(name="pk_avatar", referencedColumnName="pk", nullable=true, onDelete="CASCADE") |
|
199
|
|
|
*/ |
|
200
|
|
|
private $avatar; |
|
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @Entity |
|
205
|
|
|
* @Table(name="stonewood.avatar") |
|
206
|
|
|
*/ |
|
207
|
|
|
class DDC1657Avatar |
|
208
|
|
|
{ |
|
209
|
|
|
/** |
|
210
|
|
|
* Identifier |
|
211
|
|
|
* @var int |
|
212
|
|
|
* |
|
213
|
|
|
* @Id |
|
214
|
|
|
* @GeneratedValue(strategy="IDENTITY") |
|
215
|
|
|
* @Column(name="pk", type="integer", nullable=false) |
|
216
|
|
|
*/ |
|
217
|
|
|
private $pk; |
|
218
|
|
|
} |
|
219
|
|
|
|