Update_706
extends DatabaseUpdateBase
in package
Add a unique index on the email column of the users table.
This enforces email uniqueness at the database level to prevent race conditions that the model-level validation cannot catch.
- Empty emails are converted to NULL (multiple NULLs are allowed in unique indexes on MySQL and PostgreSQL).
- For MSSQL, a filtered index is used since it does not allow multiple NULLs in a standard unique index.
- Existing duplicate non-empty emails are deduplicated by appending the user's uid before the index is created.
Table of Contents
Properties
- $db : CDbConnection
- $newVersion : int
- $options : string
Methods
- __construct() : mixed
- getVersion() : int
- Get db version number based on class name, e.g. 123 for Update_123
- safeUp() : void
- Runs up() wrapped in a transaction.
- up() : mixed
- This is the function that must be implemented by the child classes.
- buildUniqueEmail() : string
- Build a valid, unique email by inserting a tag into the local part.
- convertEmptyEmailsToNull() : mixed
- Convert empty email strings to NULL.
- createUniqueEmailIndex() : mixed
- Create the unique index on email (database-specific for MSSQL).
- deduplicateEmails() : mixed
- Deduplicate existing non-empty duplicate emails by inserting a unique sub-address tag into the local part so the result is still a valid RFC 5322 address (e.g. user+migration5@example.com).
- dropOldEmailIndex() : mixed
- Drop the existing non-unique index on email.
- emailExists() : bool
- Check whether an email already exists in the users table.
- updateVersion() : mixed
- Sets DBVersion in settings global to $this->newVersion Last thing that happens before transaction commit in every update.
Properties
$db
protected
CDbConnection
$db
$newVersion
protected
int
$newVersion
$options
protected
string
$options
Methods
__construct()
public
__construct(CDbConnection $connection, string $options) : mixed
Parameters
- $connection : CDbConnection
- $options : string
-
Specific database options like ENGINE=INNODB etc
getVersion()
Get db version number based on class name, e.g. 123 for Update_123
public
getVersion() : int
Return values
intsafeUp()
Runs up() wrapped in a transaction.
public
safeUp() : void
Will rollback transaction and re-throw exception at failure.
Tags
up()
This is the function that must be implemented by the child classes.
public
up() : mixed
buildUniqueEmail()
Build a valid, unique email by inserting a tag into the local part.
private
buildUniqueEmail(string $email, int $uid) : string
Parameters
- $email : string
-
Original duplicate email address.
- $uid : int
-
User id used as the primary differentiator.
Return values
string —A unique email address.
convertEmptyEmailsToNull()
Convert empty email strings to NULL.
private
convertEmptyEmailsToNull() : mixed
createUniqueEmailIndex()
Create the unique index on email (database-specific for MSSQL).
private
createUniqueEmailIndex() : mixed
deduplicateEmails()
Deduplicate existing non-empty duplicate emails by inserting a unique sub-address tag into the local part so the result is still a valid RFC 5322 address (e.g. user+migration5@example.com).
private
deduplicateEmails() : mixed
dropOldEmailIndex()
Drop the existing non-unique index on email.
private
dropOldEmailIndex() : mixed
emailExists()
Check whether an email already exists in the users table.
private
emailExists(string $email) : bool
Parameters
- $email : string
Return values
boolupdateVersion()
Sets DBVersion in settings global to $this->newVersion Last thing that happens before transaction commit in every update.
private
updateVersion() : mixed