1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-05-04 18:18:17 +03:00

Unique email migration

This commit is contained in:
Alexander Yakovlev 2016-11-12 16:33:30 +07:00
parent 02ca861ddc
commit 36cbc7d6f3
2 changed files with 175 additions and 0 deletions

View file

@ -2,6 +2,7 @@ language: php
php:
- "7.0"
before_script:
phpenv config-rm xdebug.ini
# install CodeSniffer for Kohana Coding Standards checks
# - git clone https://github.com/squizlabs/PHP_CodeSniffer.git php-codesniffer --depth=1
# Install Kohana Coding Standards

View file

@ -0,0 +1,174 @@
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* Clear duplicate clients and force UNIQUE on email field.
* Constraint is not removable because it's a bugfix.
**/
class Migration_Kangana_20161112132556 extends Minion_Migration_Base {
/**
* Run queries needed to apply this migration
*
* @param Kohana_Database $db Database connection
*/
public function up(Kohana_Database $db)
{
echo 'Opening the transaction.'.PHP_EOL;
$db->begin();
try
{
echo "Finding duplicate clients.".PHP_EOL;
$emails = DB::select('email')
->from('clients')
->group_by('email')
->having(DB::expr("COUNT(email)"), ">", 1)
->execute($db)
->as_array(NULL, 'email');
if ( ! empty($emails))
{
foreach ($emails as $email)
{
$clients = DB::select()
->from('clients')
->where('email', '=', $email)
->as_object()
->execute($db);
$final = [];
$final['email'] = $email;
$final['city'] = NULL;
$final['country'] = NULL;
$final['name'] = NULL;
$final['referrer'] = NULL;
$final['sex'] = NULL;
$final['token'] = NULL;
$final['courses'] = [];
$final['groups'] = [];
foreach ($clients as $client)
{
$this->set_field($final, 'city', $client->city);
$this->set_field($final, 'country', $client->country);
$this->set_field($final, 'name', $client->name);
$this->set_field($final, 'referrer', $client->referrer);
$this->set_field($final, 'sex', $client->sex);
$this->set_field($final, 'token', $client->token);
$groups = DB::select('group_id')
->from('clients_groups')
->where('client_id', '=', $client->id)
->execute($db)
->as_array(NULL, 'group_id');
if (isset($groups))
{
foreach ($groups as $group)
{
$final['groups'][] = $group;
}
}
$courses = DB::select('course_id')
->from('clients_courses')
->where('client_id', '=', $client->id)
->execute($db)
->as_array(NULL, 'course_id');
if (isset($courses))
{
foreach ($courses as $course)
{
$final['courses'][] = $course;
}
}
}
$final['courses'] = array_unique($final['courses']);
$final['groups'] = array_unique($final['groups']);
$ids = DB::select('id')
->from('clients')
->where('email', '=', $email)
->execute($db)
->as_array(NULL, 'id');
$query = DB::query(
DATABASE::DELETE,
"DELETE FROM clients_courses WHERE client_id IN (".implode(',', $ids).")"
)->execute($db);
$query = DB::query(
DATABASE::DELETE,
"DELETE FROM clients_groups WHERE client_id IN (".implode(',', $ids).")"
)->execute($db);
$query = DB::query(
DATABASE::DELETE,
"DELETE FROM tasks WHERE client_id IN (".implode(',', $ids).")"
)->execute($db);
$query = DB::query(
DATABASE::DELETE,
"DELETE FROM clients WHERE id IN (".implode(',', $ids).")"
)->execute($db);
$client = ORM::factory('Client');
$client->city = $final['city'];
$client->country = $final['country'];
$client->name = $final['name'];
$client->sex = $final['sex'];
$client->referrer = $final['referrer'];
$client->token = $final['token'];
$client->email = $final['email'];
if (empty($client->name))
{
// name is empty, we don't need a client like that
continue;
}
try
{
$client->save();
}
catch (ORM_Validation_Exception $e)
{
var_dump($e->errors());
throw $e;
}
if (!empty($final['groups']))
{
foreach ($final['groups'] as $group_id)
{
$client->add('group', $group_id);
}
}
if (!empty($final['courses']))
{
foreach ($final['courses'] as $course_id)
{
$client->add('course', $course_id);
}
}
}
}
echo 'Altering tables.'.PHP_EOL;
$db->query(NULL, "alter table `clients` add UNIQUE (`email`)");
$db->commit();
echo 'All done.'.PHP_EOL;
}
catch (Exception $e)
{
$db->rollback();
echo $e->getMessage();
return false;
}
}
protected function set_field($arr, $attribute, $value)
{
if ($value) {
$arr[$attribute] = $value;
}
return $arr;
}
/**
* Run queries needed to remove this migration
*
* @param Kohana_Database $db Database connection
*/
public function down(Kohana_Database $db)
{
echo "Constraint is not removable.";
return true;
}
}