class EnumMigrationHelper

Methods

static void
replaceStaticTableWithEnum(array $map, string $tableName, array $foreignKeys, int $validationMin, int $validationMax)

Helper to migrate certain tables with "static" values (roles, media_types, ...) to enums:

  • Removes all given foreign key constraints
  • Changes DB values to the enum values supplied by $map
  • Drops the specified table
  • Removes the _id suffix from the column name
  • Adds a validation constraint for each renamed column, checking that values are within enum [min, max] (only for new rows, existing rows are guaranteed to be valid, see below)

static 
addRangeValidationCheck(string $table, string $newColumn, int $validationMin, int $validationMax)

No description

static string
renameEnumColumn(string $table, string $column)

No description

static void
mapValues(array $map, string $table, string $column)

No description

static void
assertCompleteMap(array $map, string $tableName)

No description

static array
buildCaseMapping(array $map)

Takes a map mapping old to new values and creates a SQL expression for an atomic update.

static void
createForeignKeys(array $foreignKeys, string $tableName)

Creates foreign keys from a list and renames existing columns by adding an _id suffix. This assumes that the _id suffix was previously removed with replaceStaticTableWithEnum

Details

at line 37
static void replaceStaticTableWithEnum(array $map, string $tableName, array $foreignKeys, int $validationMin, int $validationMax)

Helper to migrate certain tables with "static" values (roles, media_types, ...) to enums:

  • Removes all given foreign key constraints
  • Changes DB values to the enum values supplied by $map
  • Drops the specified table
  • Removes the _id suffix from the column name
  • Adds a validation constraint for each renamed column, checking that values are within enum [min, max] (only for new rows, existing rows are guaranteed to be valid, see below)

To update all values atomically, an SQL query like this is built:


SET column_name = (CASE column_name
 WHEN old1 THEN new1
 WHEN old2 THEN new2
 ...
END) <-- part in parenthesis is built
WHERE column_name IN (old1, old2, ...);```

Parameters

array $map

A mapping from the old DB values to the new enum values: [$oldId => $newId]

string $tableName

Name of the table to be removed

array $foreignKeys

[[table name, column name, foreign key constraint name], ...]. If the foreign key constraint name is not supplied, the default name is constructed

int $validationMin

Used to add a DB constraint for the values. min and max should correspond to the min and max enum values.

int $validationMax

See $validationMin

Return Value

void

at line 53
static addRangeValidationCheck(string $table, string $newColumn, int $validationMin, int $validationMax)

No description

Parameters

string $table
string $newColumn
int $validationMin
int $validationMax

at line 64
static string renameEnumColumn(string $table, string $column)

No description

Parameters

string $table
string $column

Return Value

string

at line 78
static void mapValues(array $map, string $table, string $column)

No description

Parameters

array $map
string $table
string $column

Return Value

void

at line 91
static void assertCompleteMap(array $map, string $tableName)

No description

Parameters

array $map
string $tableName

Return Value

void

at line 111
static private array buildCaseMapping(array $map)

Takes a map mapping old to new values and creates a SQL expression for an atomic update.

Returns [$cases, $ids]: $cases contains the sql query as a string, $ids contains the old values of the _id column (like role_id, ...)

Parameters

array $map

Map for old to new values

Return Value

array

at line 133
static void createForeignKeys(array $foreignKeys, string $tableName)

Creates foreign keys from a list and renames existing columns by adding an _id suffix. This assumes that the _id suffix was previously removed with replaceStaticTableWithEnum

Parameters

array $foreignKeys

See replaceStaticTableWithEnum

string $tableName

Return Value

void