Passed
Push — develop ( 5fbb42...480eed )
by Mario
02:42
created

Version0_13_0::up()   B

Complexity

Conditions 7
Paths 25

Size

Total Lines 228
Code Lines 154

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 154
nc 25
nop 1
dl 0
loc 228
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 8');
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_record_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
41
                $this->addSql('CREATE SEQUENCE app_record_association_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
42
                $this->addSql('CREATE SEQUENCE app_record_trans_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
43
                $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))');
44
                $this->addSql('CREATE UNIQUE INDEX UNIQ_758C45F4D17F50A6 ON ds_config (uuid)');
45
                $this->addSql('CREATE UNIQUE INDEX UNIQ_758C45F48A90ABA94E59C462 ON ds_config (key, tenant)');
46
                $this->addSql('CREATE TABLE ds_parameter (id INT NOT NULL, "key" VARCHAR(255) NOT NULL, value TEXT DEFAULT NULL, PRIMARY KEY(id))');
47
                $this->addSql('CREATE UNIQUE INDEX UNIQ_B3C0FD91F48571EB ON ds_parameter ("key")');
48
                $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))');
49
                $this->addSql('CREATE UNIQUE INDEX UNIQ_11290F17D17F50A6 ON ds_metadata (uuid)');
50
                $this->addSql('CREATE UNIQUE INDEX UNIQ_11290F17989D9B624E59C462 ON ds_metadata (slug, tenant)');
51
                $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))');
52
                $this->addSql('CREATE INDEX IDX_A6447E202C2AC5D3 ON ds_metadata_trans (translatable_id)');
53
                $this->addSql('CREATE UNIQUE INDEX ds_metadata_trans_unique_translation ON ds_metadata_trans (translatable_id, locale)');
54
                $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))');
55
                $this->addSql('CREATE UNIQUE INDEX UNIQ_A76F41DCD17F50A6 ON ds_access (uuid)');
56
                $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))');
57
                $this->addSql('CREATE INDEX IDX_D46DD4D04FEA67CF ON ds_access_permission (access_id)');
58
                $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))');
59
                $this->addSql('CREATE UNIQUE INDEX UNIQ_EF5FAEEAD17F50A6 ON ds_tenant (uuid)');
60
                $this->addSql('CREATE TABLE app_record (id INT NOT 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, 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))');
61
                $this->addSql('CREATE UNIQUE INDEX UNIQ_7E37F211D17F50A6 ON app_record (uuid)');
62
                $this->addSql('CREATE TABLE app_record_association (id INT NOT NULL, record_id INT DEFAULT NULL, uuid UUID NOT NULL, entity VARCHAR(255) NOT NULL, entity_uuid UUID NOT NULL, "owner" VARCHAR(255) NOT NULL, owner_uuid UUID 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))');
63
                $this->addSql('CREATE UNIQUE INDEX UNIQ_57646D0AD17F50A6 ON app_record_association (uuid)');
64
                $this->addSql('CREATE INDEX IDX_57646D0A4DFD750C ON app_record_association (record_id)');
65
                $this->addSql('CREATE TABLE app_record_trans (id INT NOT NULL, translatable_id INT DEFAULT NULL, title VARCHAR(255) DEFAULT NULL, locale VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
66
                $this->addSql('CREATE INDEX IDX_33F4A5602C2AC5D3 ON app_record_trans (translatable_id)');
67
                $this->addSql('CREATE UNIQUE INDEX app_record_trans_unique_translation ON app_record_trans (translatable_id, locale)');
68
                $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');
69
                $this->addSql('ALTER TABLE ds_access_permission ADD CONSTRAINT FK_D46DD4D04FEA67CF FOREIGN KEY (access_id) REFERENCES ds_access (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
70
                $this->addSql('ALTER TABLE app_record_association ADD CONSTRAINT FK_57646D0A4DFD750C FOREIGN KEY (record_id) REFERENCES app_record (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
71
                $this->addSql('ALTER TABLE app_record_trans ADD CONSTRAINT FK_33F4A5602C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES app_record (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
72
                $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)');
73
74
                // Data
75
                $yml = file_get_contents('/srv/api-platform/src/AppBundle/Resources/migrations/1_0_0.yml');
76
                $data = Yaml::parse($yml);
77
                $i = 0;
78
                $parameters = [
79
                    [
80
                        'key' => 'ds_system.user.username',
81
                        'value' => serialize($data['system']['username'])
82
                    ],
83
                    [
84
                        'key' => 'ds_system.user.password',
85
                        'value' => $cipherService->encrypt($data['system']['password'])
86
                    ],
87
                    [
88
                        'key' => 'ds_tenant.tenant.default',
89
                        'value' => serialize($data['tenant']['uuid'])
90
                    ]
91
                ];
92
93
                foreach ($parameters as $parameter) {
94
                    $this->addSql(sprintf(
95
                        'INSERT INTO ds_parameter (id, key, value) VALUES (%d, %s, %s);',
96
                        ++$i,
97
                        $this->connection->quote($parameter['key']),
98
                        $this->connection->quote($parameter['value'])
99
                    ));
100
                }
101
102
                $i = 0;
103
                $tenants = [
104
                    [
105
                        'uuid' => $data['tenant']['uuid'],
106
                        'data' => '"'.$cipherService->encrypt(new stdClass).'"'
107
                    ]
108
                ];
109
110
                foreach ($tenants as $tenant) {
111
                    $this->addSql(sprintf(
112
                        'INSERT INTO ds_tenant (id, uuid, data, created_at, updated_at) VALUES (%d, %s, %s, %s, %s);',
113
                        ++$i,
114
                        $this->connection->quote($tenant['uuid']),
115
                        $this->connection->quote($tenant['data']),
116
                        'now()',
117
                        'now()'
118
                    ));
119
                }
120
121
                $i = 0;
122
                $configs = [
123
                    [
124
                        'key' => 'ds_api.user.username',
125
                        'value' => serialize($data['user']['system']['username'])
126
                    ],
127
                    [
128
                        'key' => 'ds_api.user.password',
129
                        'value' => $cipherService->encrypt($data['user']['system']['password'])
130
                    ],
131
                    [
132
                        'key' => 'ds_api.user.uuid',
133
                        'value' => serialize($data['user']['system']['uuid'])
134
                    ],
135
                    [
136
                        'key' => 'ds_api.user.roles',
137
                        'value' => serialize([])
138
                    ],
139
                    [
140
                        'key' => 'ds_api.user.identity.roles',
141
                        'value' => serialize([])
142
                    ],
143
                    [
144
                        'key' => 'ds_api.user.identity.type',
145
                        'value' => serialize('System')
146
                    ],
147
                    [
148
                        'key' => 'ds_api.user.identity.uuid',
149
                        'value' => serialize($data['identity']['system']['uuid'])
150
                    ],
151
                    [
152
                        'key' => 'ds_api.user.tenant',
153
                        'value' => serialize($data['tenant']['uuid'])
154
                    ]
155
                ];
156
157
                foreach ($configs as $config) {
158
                    $this->addSql(sprintf(
159
                        '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);',
160
                        ++$i,
161
                        $this->connection->quote(Uuid::uuid4()->toString()),
162
                        $this->connection->quote('BusinessUnit'),
163
                        $this->connection->quote($data['business_unit']['administration']['uuid']),
164
                        $this->connection->quote($config['key']),
165
                        $this->connection->quote($config['value']),
166
                        1,
167
                        $this->connection->quote($data['tenant']['uuid']),
168
                        'now()',
169
                        'now()'
170
                    ));
171
                }
172
173
                $i = 0;
174
                $j = 0;
175
                $accesses = [
176
                    [
177
                        'owner' => 'System',
178
                        'owner_uuid' => $data['identity']['system']['uuid'],
179
                        'assignee' => 'System',
180
                        'assignee_uuid' => $data['identity']['system']['uuid'],
181
                        'permissions' => [
182
                            [
183
                                'key' => 'entity',
184
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE"]'
185
                            ],
186
                            [
187
                                'key' => 'property',
188
                                'attributes' => '["BROWSE","READ","EDIT"]'
189
                            ],
190
                            [
191
                                'key' => 'generic',
192
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE","EXECUTE"]'
193
                            ]
194
                        ]
195
                    ],
196
                    [
197
                        'owner' => 'BusinessUnit',
198
                        'owner_uuid' => $data['business_unit']['administration']['uuid'],
199
                        'assignee' => 'Staff',
200
                        'assignee_uuid' => $data['identity']['admin']['uuid'],
201
                        'permissions' => [
202
                            [
203
                                'key' => 'entity',
204
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE"]'
205
                            ],
206
                            [
207
                                'key' => 'property',
208
                                'attributes' => '["BROWSE","READ","EDIT"]'
209
                            ],
210
                            [
211
                                'key' => 'generic',
212
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE","EXECUTE"]'
213
                            ]
214
                        ]
215
                    ]
216
                ];
217
218
                foreach ($accesses as $access) {
219
                    $this->addSql(sprintf(
220
                        '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);',
221
                        ++$i,
222
                        $this->connection->quote(Uuid::uuid4()->toString()),
223
                        $this->connection->quote($access['owner']),
224
                        $this->connection->quote($access['owner_uuid']),
225
                        $this->connection->quote($access['assignee']),
226
                        $this->connection->quote($access['assignee_uuid']),
227
                        1,
228
                        $this->connection->quote($data['tenant']['uuid']),
229
                        'now()',
230
                        'now()'
231
                    ));
232
233
                    foreach ($access['permissions'] as $permission) {
234
                        $this->addSql(sprintf(
235
                            'INSERT INTO ds_access_permission (id, access_id, scope, entity, entity_uuid, key, attributes, tenant) VALUES (%d, %d, %s, %s, %s, %s, %s, %s);',
236
                            ++$j,
237
                            $i,
238
                            $this->connection->quote('generic'),
239
                            'NULL',
240
                            'NULL',
241
                            $this->connection->quote($permission['key']),
242
                            $this->connection->quote($permission['attributes']),
243
                            $this->connection->quote($data['tenant']['uuid'])
244
                        ));
245
                    }
246
                }
247
248
                break;
249
250
            default:
251
                $this->abortIf(true,'Migration cannot be executed on "'.$platform.'".');
252
                break;
253
        }
254
    }
255
256
    /**
257
     * Down
258
     *
259
     * @param \Doctrine\DBAL\Schema\Schema $schema
260
     */
261
    public function down(Schema $schema)
262
    {
263
        $platform = $this->connection->getDatabasePlatform()->getName();
264
265
        switch ($platform) {
266
            case 'postgresql':
267
                // Schema
268
                $this->addSql('ALTER TABLE ds_metadata_trans DROP CONSTRAINT FK_A6447E202C2AC5D3');
269
                $this->addSql('ALTER TABLE ds_access_permission DROP CONSTRAINT FK_D46DD4D04FEA67CF');
270
                $this->addSql('ALTER TABLE app_record_association DROP CONSTRAINT FK_57646D0A4DFD750C');
271
                $this->addSql('ALTER TABLE app_record_trans DROP CONSTRAINT FK_33F4A5602C2AC5D3');
272
                $this->addSql('DROP SEQUENCE ds_config_id_seq CASCADE');
273
                $this->addSql('DROP SEQUENCE ds_parameter_id_seq CASCADE');
274
                $this->addSql('DROP SEQUENCE ds_metadata_id_seq CASCADE');
275
                $this->addSql('DROP SEQUENCE ds_metadata_trans_id_seq CASCADE');
276
                $this->addSql('DROP SEQUENCE ds_access_id_seq CASCADE');
277
                $this->addSql('DROP SEQUENCE ds_access_permission_id_seq CASCADE');
278
                $this->addSql('DROP SEQUENCE ds_tenant_id_seq CASCADE');
279
                $this->addSql('DROP SEQUENCE app_record_id_seq CASCADE');
280
                $this->addSql('DROP SEQUENCE app_record_association_id_seq CASCADE');
281
                $this->addSql('DROP SEQUENCE app_record_trans_id_seq CASCADE');
282
                $this->addSql('DROP TABLE ds_config');
283
                $this->addSql('DROP TABLE ds_parameter');
284
                $this->addSql('DROP TABLE ds_metadata');
285
                $this->addSql('DROP TABLE ds_metadata_trans');
286
                $this->addSql('DROP TABLE ds_access');
287
                $this->addSql('DROP TABLE ds_access_permission');
288
                $this->addSql('DROP TABLE ds_tenant');
289
                $this->addSql('DROP TABLE app_record');
290
                $this->addSql('DROP TABLE app_record_association');
291
                $this->addSql('DROP TABLE app_record_trans');
292
                $this->addSql('DROP TABLE ds_session');
293
                break;
294
295
            default:
296
                $this->abortIf(true,'Migration cannot be executed on "'.$platform.'".');
297
                break;
298
        }
299
    }
300
}
301