Passed
Push — main ( 05b1ca...cdc05c )
by Corinna
54s queued 14s
created

Version20241113164504::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare( strict_types=1 );
4
5
namespace WMDE\Fundraising\AddressChangeContext\DataAccess\Migrations;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\Migrations\AbstractMigration;
9
10
/**
11
 * This migration is a followup for commit 82fbafd0df529ba9604b35ef805174d1a4c390fa
12
 * where we changed how we store the address change identifiers and made the columns non-nullable,
13
 * but forgot to migrate old address change entries where the `previous_identifier` defaulted to NULL.
14
 */
15
final class Version20241113164504 extends AbstractMigration {
16
	public function getDescription(): string {
17
		return 'Make address change identifiers non-nullable';
18
	}
19
20
	public function up( Schema $schema ): void {
21
		$this->addSql( "UPDATE address_change SET previous_identifier=current_identifier WHERE previous_identifier IS NULL AND current_identifier IS NOT NULL" );
22
		// We can't use $schema here to modify the columns because `addSQL` appends to the SQL generated by $schema calls.
23
		// We would fail the migration when trying to set a column to non-nullable if it already contains NULL values
24
		$this->addSql( "ALTER TABLE address_change MODIFY current_identifier VARCHAR(36) NOT NULL, MODIFY previous_identifier VARCHAR(36) NOT NULL " );
25
	}
26
27
	public function down( Schema $schema ): void {
28
		$addressChange = $schema->getTable( 'address_change' );
29
		$addressChange->modifyColumn( 'current_identifier', [ 'notnull' => false ] );
30
		$addressChange->modifyColumn( 'previous_identifier', [ 'notnull' => false ] );
31
	}
32
}
33