Passed
Push — develop ( 6a19a1...981d31 )
by Mario
02:48
created

Version0_13_0::up()   B

Complexity

Conditions 7
Paths 25

Size

Total Lines 234
Code Lines 160

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 160
nc 25
nop 1
dl 0
loc 234
rs 7.0666
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AppBundle\Migrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Schema\Schema;
7
use Ds\Component\Container\Attribute;
8
use Ramsey\Uuid\Uuid;
9
use stdClass;
10
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
11
use Symfony\Component\Yaml\Yaml;
12
13
/**
14
 * Class Version0_13_0
15
 */
16
class Version0_13_0 extends AbstractMigration implements ContainerAwareInterface
17
{
18
    use Attribute\Container;
19
20
    /**
21
     * Up
22
     *
23
     * @param \Doctrine\DBAL\Schema\Schema $schema
24
     */
25
    public function up(Schema $schema)
26
    {
27
        $platform = $this->connection->getDatabasePlatform()->getName();
28
        $cipherService = $this->container->get('ds_encryption.service.cipher');
29
30
        switch ($platform) {
31
            case 'postgresql':
32
                // Schema
33
                $this->addSql('CREATE SEQUENCE ds_config_id_seq INCREMENT BY 1 MINVALUE 1 START 9');
34
                $this->addSql('CREATE SEQUENCE ds_parameter_id_seq INCREMENT BY 1 MINVALUE 1 START 4');
35
                $this->addSql('CREATE SEQUENCE ds_metadata_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
36
                $this->addSql('CREATE SEQUENCE ds_metadata_trans_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
37
                $this->addSql('CREATE SEQUENCE ds_access_id_seq INCREMENT BY 1 MINVALUE 1 START 3');
38
                $this->addSql('CREATE SEQUENCE ds_access_permission_id_seq INCREMENT BY 1 MINVALUE 1 START 7');
39
                $this->addSql('CREATE SEQUENCE ds_tenant_id_seq INCREMENT BY 1 MINVALUE 1 START 2');
40
                $this->addSql('CREATE SEQUENCE app_case_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
41
                $this->addSql('CREATE SEQUENCE app_case_status_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
42
                $this->addSql('CREATE SEQUENCE app_case_status_trans_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
43
                $this->addSql('CREATE SEQUENCE app_case_trans_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
44
                $this->addSql('CREATE TABLE ds_config (id INT NOT NULL, uuid UUID NOT NULL, "owner" VARCHAR(255) DEFAULT NULL, owner_uuid UUID DEFAULT NULL, "key" VARCHAR(255) NOT NULL, value TEXT DEFAULT NULL, version INT DEFAULT 1 NOT NULL, tenant UUID NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
45
                $this->addSql('CREATE UNIQUE INDEX UNIQ_758C45F4D17F50A6 ON ds_config (uuid)');
46
                $this->addSql('CREATE UNIQUE INDEX UNIQ_758C45F48A90ABA94E59C462 ON ds_config (key, tenant)');
47
                $this->addSql('CREATE TABLE ds_parameter (id INT NOT NULL, "key" VARCHAR(255) NOT NULL, value TEXT DEFAULT NULL, PRIMARY KEY(id))');
48
                $this->addSql('CREATE UNIQUE INDEX UNIQ_B3C0FD91F48571EB ON ds_parameter ("key")');
49
                $this->addSql('CREATE TABLE ds_metadata (id INT NOT NULL, uuid UUID NOT NULL, "owner" VARCHAR(255) DEFAULT NULL, owner_uuid UUID DEFAULT NULL, slug VARCHAR(255) NOT NULL, type VARCHAR(255) DEFAULT NULL, data JSON NOT NULL, version INT DEFAULT 1 NOT NULL, tenant UUID NOT NULL, deleted_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
50
                $this->addSql('CREATE UNIQUE INDEX UNIQ_11290F17D17F50A6 ON ds_metadata (uuid)');
51
                $this->addSql('CREATE UNIQUE INDEX UNIQ_11290F17989D9B624E59C462 ON ds_metadata (slug, tenant)');
52
                $this->addSql('CREATE TABLE ds_metadata_trans (id INT NOT NULL, translatable_id INT DEFAULT NULL, title VARCHAR(255) NOT NULL, locale VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
53
                $this->addSql('CREATE INDEX IDX_A6447E202C2AC5D3 ON ds_metadata_trans (translatable_id)');
54
                $this->addSql('CREATE UNIQUE INDEX ds_metadata_trans_unique_translation ON ds_metadata_trans (translatable_id, locale)');
55
                $this->addSql('CREATE TABLE ds_access (id INT NOT NULL, uuid UUID NOT NULL, "owner" VARCHAR(255) DEFAULT NULL, owner_uuid UUID DEFAULT NULL, assignee VARCHAR(255) DEFAULT NULL, assignee_uuid UUID DEFAULT NULL, version INT DEFAULT 1 NOT NULL, tenant UUID NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
56
                $this->addSql('CREATE UNIQUE INDEX UNIQ_A76F41DCD17F50A6 ON ds_access (uuid)');
57
                $this->addSql('CREATE TABLE ds_access_permission (id INT NOT NULL, access_id INT DEFAULT NULL, scope VARCHAR(255) DEFAULT NULL, entity VARCHAR(255) DEFAULT NULL, entity_uuid UUID DEFAULT NULL, "key" VARCHAR(255) NOT NULL, attributes JSON NOT NULL, tenant UUID NOT NULL, PRIMARY KEY(id))');
58
                $this->addSql('CREATE INDEX IDX_D46DD4D04FEA67CF ON ds_access_permission (access_id)');
59
                $this->addSql('CREATE TABLE ds_tenant (id INT NOT NULL, uuid UUID NOT NULL, data JSON NOT NULL, version INT DEFAULT 1 NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
60
                $this->addSql('CREATE UNIQUE INDEX UNIQ_EF5FAEEAD17F50A6 ON ds_tenant (uuid)');
61
                $this->addSql('CREATE TABLE app_case (id INT NOT NULL, uuid UUID NOT NULL, custom_id VARCHAR(255) NOT NULL, "owner" VARCHAR(255) DEFAULT NULL, owner_uuid UUID DEFAULT NULL, identity VARCHAR(255) DEFAULT NULL, identity_uuid UUID DEFAULT NULL, state VARCHAR(255) DEFAULT NULL, priority INT DEFAULT 0 NOT NULL, version INT DEFAULT 1 NOT NULL, tenant UUID NOT NULL, deleted_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
62
                $this->addSql('CREATE UNIQUE INDEX UNIQ_7D26BCA4D17F50A6 ON app_case (uuid)');
63
                $this->addSql('CREATE UNIQUE INDEX UNIQ_7D26BCA4614A603A4E59C462 ON app_case (custom_id, tenant)');
64
                $this->addSql('CREATE TABLE app_case_status (id INT NOT NULL, case_id INT DEFAULT NULL, uuid UUID NOT NULL, "owner" VARCHAR(255) DEFAULT NULL, owner_uuid UUID DEFAULT NULL, identity VARCHAR(255) DEFAULT NULL, identity_uuid UUID DEFAULT NULL, version INT DEFAULT 1 NOT NULL, tenant UUID NOT NULL, deleted_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
65
                $this->addSql('CREATE UNIQUE INDEX UNIQ_8B0B0409D17F50A6 ON app_case_status (uuid)');
66
                $this->addSql('CREATE INDEX IDX_8B0B0409CF10D4F5 ON app_case_status (case_id)');
67
                $this->addSql('CREATE TABLE app_case_status_trans (id INT NOT NULL, translatable_id INT DEFAULT NULL, title VARCHAR(255) DEFAULT NULL, description TEXT DEFAULT NULL, data JSON NOT NULL, locale VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
68
                $this->addSql('CREATE INDEX IDX_12F776492C2AC5D3 ON app_case_status_trans (translatable_id)');
69
                $this->addSql('CREATE UNIQUE INDEX app_case_status_trans_unique_translation ON app_case_status_trans (translatable_id, locale)');
70
                $this->addSql('CREATE TABLE app_case_trans (id INT NOT NULL, translatable_id INT DEFAULT NULL, title VARCHAR(255) DEFAULT NULL, data JSON NOT NULL, locale VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
71
                $this->addSql('CREATE INDEX IDX_74D34F2D2C2AC5D3 ON app_case_trans (translatable_id)');
72
                $this->addSql('CREATE UNIQUE INDEX app_case_trans_unique_translation ON app_case_trans (translatable_id, locale)');
73
                $this->addSql('ALTER TABLE ds_metadata_trans ADD CONSTRAINT FK_A6447E202C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES ds_metadata (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
74
                $this->addSql('ALTER TABLE ds_access_permission ADD CONSTRAINT FK_D46DD4D04FEA67CF FOREIGN KEY (access_id) REFERENCES ds_access (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
75
                $this->addSql('ALTER TABLE app_case_status ADD CONSTRAINT FK_8B0B0409CF10D4F5 FOREIGN KEY (case_id) REFERENCES app_case (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
76
                $this->addSql('ALTER TABLE app_case_status_trans ADD CONSTRAINT FK_12F776492C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES app_case_status (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
77
                $this->addSql('ALTER TABLE app_case_trans ADD CONSTRAINT FK_74D34F2D2C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES app_case (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
78
                $this->addSql('CREATE TABLE ds_session (id VARCHAR(128) NOT NULL PRIMARY KEY, data BYTEA NOT NULL, time INTEGER NOT NULL, lifetime INTEGER NOT NULL)');
79
80
                // Data
81
                $yml = file_get_contents('/srv/api-platform/src/AppBundle/Resources/migrations/1_0_0.yml');
82
                $data = Yaml::parse($yml);
83
                $i = 0;
84
                $parameters = [
85
                    [
86
                        'key' => 'ds_system.user.username',
87
                        'value' => serialize($data['system']['username'])
88
                    ],
89
                    [
90
                        'key' => 'ds_system.user.password',
91
                        'value' => $cipherService->encrypt($data['system']['password'])
92
                    ],
93
                    [
94
                        'key' => 'ds_tenant.tenant.default',
95
                        'value' => serialize($data['tenant']['uuid'])
96
                    ]
97
                ];
98
99
                foreach ($parameters as $parameter) {
100
                    $this->addSql(sprintf(
101
                        'INSERT INTO ds_parameter (id, key, value) VALUES (%d, %s, %s);',
102
                        ++$i,
103
                        $this->connection->quote($parameter['key']),
104
                        $this->connection->quote($parameter['value'])
105
                    ));
106
                }
107
108
                $i = 0;
109
                $tenants = [
110
                    [
111
                        'uuid' => $data['tenant']['uuid'],
112
                        'data' => '"'.$cipherService->encrypt(new stdClass).'"'
113
                    ]
114
                ];
115
116
                foreach ($tenants as $tenant) {
117
                    $this->addSql(sprintf(
118
                        'INSERT INTO ds_tenant (id, uuid, data, created_at, updated_at) VALUES (%d, %s, %s, %s, %s);',
119
                        ++$i,
120
                        $this->connection->quote($tenant['uuid']),
121
                        $this->connection->quote($tenant['data']),
122
                        'now()',
123
                        'now()'
124
                    ));
125
                }
126
127
                $i = 0;
128
                $configs = [
129
                    [
130
                        'key' => 'ds_api.user.username',
131
                        'value' => serialize($data['user']['system']['username'])
132
                    ],
133
                    [
134
                        'key' => 'ds_api.user.password',
135
                        'value' => $cipherService->encrypt($data['user']['system']['password'])
136
                    ],
137
                    [
138
                        'key' => 'ds_api.user.uuid',
139
                        'value' => serialize($data['user']['system']['uuid'])
140
                    ],
141
                    [
142
                        'key' => 'ds_api.user.roles',
143
                        'value' => serialize([])
144
                    ],
145
                    [
146
                        'key' => 'ds_api.user.identity.roles',
147
                        'value' => serialize([])
148
                    ],
149
                    [
150
                        'key' => 'ds_api.user.identity.type',
151
                        'value' => serialize('System')
152
                    ],
153
                    [
154
                        'key' => 'ds_api.user.identity.uuid',
155
                        'value' => serialize($data['identity']['system']['uuid'])
156
                    ],
157
                    [
158
                        'key' => 'ds_api.user.tenant',
159
                        'value' => serialize($data['tenant']['uuid'])
160
                    ]
161
                ];
162
163
                foreach ($configs as $config) {
164
                    $this->addSql(sprintf(
165
                        'INSERT INTO ds_config (id, uuid, owner, owner_uuid, key, value, version, tenant, created_at, updated_at) VALUES (%d, %s, %s, %s, %s, %s, %d, %s, %s, %s);',
166
                        ++$i,
167
                        $this->connection->quote(Uuid::uuid4()->toString()),
168
                        $this->connection->quote('BusinessUnit'),
169
                        $this->connection->quote($data['business_unit']['administration']['uuid']),
170
                        $this->connection->quote($config['key']),
171
                        $this->connection->quote($config['value']),
172
                        1,
173
                        $this->connection->quote($data['tenant']['uuid']),
174
                        'now()',
175
                        'now()'
176
                    ));
177
                }
178
179
                $i = 0;
180
                $j = 0;
181
                $accesses = [
182
                    [
183
                        'owner' => 'System',
184
                        'owner_uuid' => $data['identity']['system']['uuid'],
185
                        'assignee' => 'System',
186
                        'assignee_uuid' => $data['identity']['system']['uuid'],
187
                        'permissions' => [
188
                            [
189
                                'key' => 'entity',
190
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE"]'
191
                            ],
192
                            [
193
                                'key' => 'property',
194
                                'attributes' => '["BROWSE","READ","EDIT"]'
195
                            ],
196
                            [
197
                                'key' => 'generic',
198
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE","EXECUTE"]'
199
                            ]
200
                        ]
201
                    ],
202
                    [
203
                        'owner' => 'BusinessUnit',
204
                        'owner_uuid' => $data['business_unit']['administration']['uuid'],
205
                        'assignee' => 'Staff',
206
                        'assignee_uuid' => $data['identity']['admin']['uuid'],
207
                        'permissions' => [
208
                            [
209
                                'key' => 'entity',
210
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE"]'
211
                            ],
212
                            [
213
                                'key' => 'property',
214
                                'attributes' => '["BROWSE","READ","EDIT"]'
215
                            ],
216
                            [
217
                                'key' => 'generic',
218
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE","EXECUTE"]'
219
                            ]
220
                        ]
221
                    ]
222
                ];
223
224
                foreach ($accesses as $access) {
225
                    $this->addSql(sprintf(
226
                        'INSERT INTO ds_access (id, uuid, owner, owner_uuid, assignee, assignee_uuid, version, tenant, created_at, updated_at) VALUES (%d, %s, %s, %s, %s, %s, %d, %s, %s, %s);',
227
                        ++$i,
228
                        $this->connection->quote(Uuid::uuid4()->toString()),
229
                        $this->connection->quote($access['owner']),
230
                        $this->connection->quote($access['owner_uuid']),
231
                        $this->connection->quote($access['assignee']),
232
                        $this->connection->quote($access['assignee_uuid']),
233
                        1,
234
                        $this->connection->quote($data['tenant']['uuid']),
235
                        'now()',
236
                        'now()'
237
                    ));
238
239
                    foreach ($access['permissions'] as $permission) {
240
                        $this->addSql(sprintf(
241
                            'INSERT INTO ds_access_permission (id, access_id, scope, entity, entity_uuid, key, attributes, tenant) VALUES (%d, %d, %s, %s, %s, %s, %s, %s);',
242
                            ++$j,
243
                            $i,
244
                            $this->connection->quote('generic'),
245
                            'NULL',
246
                            'NULL',
247
                            $this->connection->quote($permission['key']),
248
                            $this->connection->quote($permission['attributes']),
249
                            $this->connection->quote($data['tenant']['uuid'])
250
                        ));
251
                    }
252
                }
253
254
                break;
255
256
            default:
257
                $this->abortIf(true,'Migration cannot be executed on "'.$platform.'".');
258
                break;
259
        }
260
    }
261
262
    /**
263
     * Down
264
     *
265
     * @param \Doctrine\DBAL\Schema\Schema $schema
266
     */
267
    public function down(Schema $schema)
268
    {
269
        $platform = $this->connection->getDatabasePlatform()->getName();
270
271
        switch ($platform) {
272
            case 'postgresql':
273
                // Schema
274
                $this->addSql('ALTER TABLE ds_metadata_trans DROP CONSTRAINT FK_A6447E202C2AC5D3');
275
                $this->addSql('ALTER TABLE ds_access_permission DROP CONSTRAINT FK_D46DD4D04FEA67CF');
276
                $this->addSql('ALTER TABLE app_case_status DROP CONSTRAINT FK_8B0B0409CF10D4F5');
277
                $this->addSql('ALTER TABLE app_case_trans DROP CONSTRAINT FK_74D34F2D2C2AC5D3');
278
                $this->addSql('ALTER TABLE app_case_status_trans DROP CONSTRAINT FK_12F776492C2AC5D3');
279
                $this->addSql('DROP SEQUENCE ds_config_id_seq CASCADE');
280
                $this->addSql('DROP SEQUENCE ds_parameter_id_seq CASCADE');
281
                $this->addSql('DROP SEQUENCE ds_metadata_id_seq CASCADE');
282
                $this->addSql('DROP SEQUENCE ds_metadata_trans_id_seq CASCADE');
283
                $this->addSql('DROP SEQUENCE ds_access_id_seq CASCADE');
284
                $this->addSql('DROP SEQUENCE ds_access_permission_id_seq CASCADE');
285
                $this->addSql('DROP SEQUENCE ds_tenant_id_seq CASCADE');
286
                $this->addSql('DROP SEQUENCE app_case_id_seq CASCADE');
287
                $this->addSql('DROP SEQUENCE app_case_status_id_seq CASCADE');
288
                $this->addSql('DROP SEQUENCE app_case_status_trans_id_seq CASCADE');
289
                $this->addSql('DROP SEQUENCE app_case_trans_id_seq CASCADE');
290
                $this->addSql('DROP TABLE ds_config');
291
                $this->addSql('DROP TABLE ds_parameter');
292
                $this->addSql('DROP TABLE ds_metadata');
293
                $this->addSql('DROP TABLE ds_metadata_trans');
294
                $this->addSql('DROP TABLE ds_access');
295
                $this->addSql('DROP TABLE ds_access_permission');
296
                $this->addSql('DROP TABLE ds_tenant');
297
                $this->addSql('DROP TABLE app_case');
298
                $this->addSql('DROP TABLE app_case_status');
299
                $this->addSql('DROP TABLE app_case_status_trans');
300
                $this->addSql('DROP TABLE app_case_trans');
301
                $this->addSql('DROP TABLE ds_session');
302
                break;
303
304
            default:
305
                $this->abortIf(true,'Migration cannot be executed on "'.$platform.'".');
306
                break;
307
        }
308
    }
309
}
310