Conditions | 1 |
Paths | 1 |
Total Lines | 72 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
19 | public function up(Schema $schema): void |
||
20 | { |
||
21 | $this->addSql('ALTER TABLE c_blog_attachment ADD COLUMN IF NOT EXISTS post_id INT DEFAULT NULL'); |
||
22 | $this->addSql('ALTER TABLE c_blog_attachment ADD COLUMN IF NOT EXISTS comment_id INT DEFAULT NULL'); |
||
23 | |||
24 | $this->addSql('CREATE INDEX IF NOT EXISTS IDX_E769AADC4B89032C ON c_blog_attachment (post_id)'); |
||
25 | $this->addSql('CREATE INDEX IF NOT EXISTS IDX_E769AADCF8697D13 ON c_blog_attachment (comment_id)'); |
||
26 | |||
27 | $this->addSql('ALTER TABLE c_blog_attachment DROP FOREIGN KEY IF EXISTS FK_E769AADC4B89032C'); |
||
28 | $this->addSql('ALTER TABLE c_blog_attachment DROP FOREIGN KEY IF EXISTS FK_E769AADCF8697D13'); |
||
29 | |||
30 | $this->addSql('ALTER TABLE c_blog_attachment ADD CONSTRAINT FK_E769AADC4B89032C FOREIGN KEY (post_id) REFERENCES c_blog_post (iid) ON DELETE CASCADE'); |
||
31 | $this->addSql('ALTER TABLE c_blog_attachment ADD CONSTRAINT FK_E769AADCF8697D13 FOREIGN KEY (comment_id) REFERENCES c_blog_comment (iid) ON DELETE CASCADE'); |
||
32 | |||
33 | |||
34 | // Ensure columns exist |
||
35 | $this->addSql('ALTER TABLE c_blog_comment ADD COLUMN IF NOT EXISTS post_id INT DEFAULT NULL'); |
||
36 | $this->addSql('ALTER TABLE c_blog_comment ADD COLUMN IF NOT EXISTS parent_comment_id INT DEFAULT NULL'); |
||
37 | |||
38 | // Make them NULLABLE (some previous attempts may have left them NOT NULL) |
||
39 | $this->addSql('ALTER TABLE c_blog_comment MODIFY post_id INT NULL'); |
||
40 | $this->addSql('ALTER TABLE c_blog_comment MODIFY parent_comment_id INT NULL'); |
||
41 | $this->addSql('ALTER TABLE c_blog_comment MODIFY comment_id INT NOT NULL DEFAULT 0'); |
||
42 | |||
43 | // DATA CLEANUP |
||
44 | $this->addSql('UPDATE c_blog_comment SET post_id = NULL WHERE post_id = 0'); |
||
45 | $this->addSql('UPDATE c_blog_comment SET parent_comment_id = NULL WHERE parent_comment_id = 0'); |
||
46 | |||
47 | $this->addSql(' |
||
48 | UPDATE c_blog_comment c |
||
49 | LEFT JOIN c_blog_post p ON c.post_id = p.iid |
||
50 | SET c.post_id = NULL |
||
51 | WHERE c.post_id IS NOT NULL AND p.iid IS NULL |
||
52 | '); |
||
53 | |||
54 | $this->addSql(' |
||
55 | UPDATE c_blog_comment c |
||
56 | LEFT JOIN c_blog_comment pc ON c.parent_comment_id = pc.iid |
||
57 | SET c.parent_comment_id = NULL |
||
58 | WHERE c.parent_comment_id IS NOT NULL AND pc.iid IS NULL |
||
59 | '); |
||
60 | |||
61 | // Indexes |
||
62 | $this->addSql('CREATE INDEX IF NOT EXISTS IDX_CAA18F14B89032C ON c_blog_comment (post_id)'); |
||
63 | $this->addSql('CREATE INDEX IF NOT EXISTS IDX_CAA18F1BF2AF943 ON c_blog_comment (parent_comment_id)'); |
||
64 | |||
65 | // FKs |
||
66 | $this->addSql('ALTER TABLE c_blog_comment DROP FOREIGN KEY IF EXISTS FK_CAA18F14B89032C'); |
||
67 | $this->addSql('ALTER TABLE c_blog_comment DROP FOREIGN KEY IF EXISTS FK_CAA18F1BF2AF943'); |
||
68 | |||
69 | $this->addSql('ALTER TABLE c_blog_comment ADD CONSTRAINT FK_CAA18F14B89032C FOREIGN KEY (post_id) REFERENCES c_blog_post (iid) ON DELETE CASCADE'); |
||
70 | $this->addSql('ALTER TABLE c_blog_comment ADD CONSTRAINT FK_CAA18F1BF2AF943 FOREIGN KEY (parent_comment_id) REFERENCES c_blog_comment (iid) ON DELETE CASCADE'); |
||
71 | |||
72 | // Ensure column exists |
||
73 | $this->addSql('ALTER TABLE c_blog_rating ADD COLUMN IF NOT EXISTS post_id INT DEFAULT NULL'); |
||
74 | |||
75 | // Make it NULLABLE in case it exists as NOT NULL |
||
76 | $this->addSql('ALTER TABLE c_blog_rating MODIFY post_id INT NULL'); |
||
77 | |||
78 | // Cleanup |
||
79 | $this->addSql('UPDATE c_blog_rating SET post_id = NULL WHERE post_id = 0'); |
||
80 | $this->addSql(' |
||
81 | UPDATE c_blog_rating r |
||
82 | LEFT JOIN c_blog_post p ON r.post_id = p.iid |
||
83 | SET r.post_id = NULL |
||
84 | WHERE r.post_id IS NOT NULL AND p.iid IS NULL |
||
85 | '); |
||
86 | |||
87 | // Index + FK |
||
88 | $this->addSql('CREATE INDEX IF NOT EXISTS IDX_D4E307604B89032C ON c_blog_rating (post_id)'); |
||
89 | $this->addSql('ALTER TABLE c_blog_rating DROP FOREIGN KEY IF EXISTS FK_D4E307604B89032C'); |
||
90 | $this->addSql('ALTER TABLE c_blog_rating ADD CONSTRAINT FK_D4E307604B89032C FOREIGN KEY (post_id) REFERENCES c_blog_post (iid) ON DELETE CASCADE'); |
||
91 | } |
||
120 |