mirror of
https://github.com/Oreolek/kangana.git
synced 2024-06-28 20:55:09 +03:00
More migrations
This commit is contained in:
parent
346608e284
commit
d5b83e8183
|
@ -10,12 +10,12 @@ class Model_Course extends ORM {
|
|||
/**
|
||||
* A pre-scheduled course - the subscriber gets a new letter once per N days in the series.
|
||||
*/
|
||||
public const TYPE_SCHEDULED = 0;
|
||||
const TYPE_SCHEDULED = 0;
|
||||
|
||||
/**
|
||||
* A irregular subscription - the letters are sent when the author sends them
|
||||
*/
|
||||
public const TYPE_IRREGULAR = 1;
|
||||
const TYPE_IRREGULAR = 1;
|
||||
|
||||
protected $_has_many = array(
|
||||
'clients' => array(
|
||||
|
|
|
@ -25,15 +25,64 @@ class Migration_Kangana_20161016123906 extends Minion_Migration_Base {
|
|||
$subscriptions = DB::select()->from('subscriptions')->as_object()->execute();
|
||||
foreach ($subscriptions as $subscription)
|
||||
{
|
||||
$course = ORM::factory('Course');
|
||||
$course->type = Course::TYPE_IRREGULAR;
|
||||
$course->title = $subscription->title;
|
||||
$course->description = $subscription->description;
|
||||
$course->price = $subscription->price;
|
||||
$instants = $subscription->instants->find_all();
|
||||
$query = DB::query(
|
||||
Database::INSERT,
|
||||
'INSERT INTO courses (type, title, description, price)
|
||||
VALUES(:type, :title, :description, :price)'
|
||||
);
|
||||
$query->parameters(array(
|
||||
':type' => Model_Course::TYPE_IRREGULAR,
|
||||
':title' => $subscription->title,
|
||||
':description' => $subscription->description,
|
||||
':price' => $subscription->price
|
||||
));
|
||||
$query->execute();
|
||||
echo 'Migrating subscription "' . $subscription->title . '"' . PHP_EOL;
|
||||
|
||||
$course_id = DB::select('id')
|
||||
->from('courses')
|
||||
->where('title', '=', $subscription->title)
|
||||
->and_where('description', '=', $subscription->description)
|
||||
->and_where('type', '=', Model_Course::TYPE_IRREGULAR)
|
||||
->execute()
|
||||
->get('id');
|
||||
|
||||
echo 'Migrating links between subscriptions and clients.';
|
||||
$clients = DB::select('client_id')
|
||||
->from('clients_subscriptions')
|
||||
->where('subscription_id', '=', $subscription->id)
|
||||
->execute()
|
||||
->as_array(NULL, 'client_id');
|
||||
|
||||
foreach ($clients as $client)
|
||||
{
|
||||
$db->query(NULL, 'INSERT INTO clients_courses (client_id, course_id)
|
||||
VALUES (' . $client . ', ' . $course_id . ')');
|
||||
}
|
||||
|
||||
echo 'Migrating links between subscriptions and groups.';
|
||||
$groups = DB::select('group_id')
|
||||
->from('subscriptions_groups')
|
||||
->where('subscription_id', '=', $subscription->id)
|
||||
->execute()
|
||||
->as_array(NULL, 'group_id');
|
||||
|
||||
foreach ($groups as $group)
|
||||
{
|
||||
$db->query(NULL, 'INSERT INTO courses_groups (group_id, course_id)
|
||||
VALUES (' . $group . ', ' . $course_id . ')');
|
||||
}
|
||||
|
||||
echo 'Migrating Instants.';
|
||||
|
||||
$instants = DB::select()
|
||||
->from('instants')
|
||||
->where('subscription_id', '=', $subscription->id)
|
||||
->as_object()
|
||||
->execute();
|
||||
foreach ($instants as $instant)
|
||||
{
|
||||
echo 'Migrating a letter:' . $instant->subject . PHP_EOL;
|
||||
echo 'Migrating letter "' . $instant->subject . '"' . PHP_EOL;
|
||||
$query = DB::query(
|
||||
Database::INSERT,
|
||||
'INSERT INTO letters (course_id, subject, text, timestamp, sent)
|
||||
|
@ -41,15 +90,21 @@ class Migration_Kangana_20161016123906 extends Minion_Migration_Base {
|
|||
);
|
||||
|
||||
$query->parameters(array(
|
||||
':course_id' => $course->id,
|
||||
':course_id' => $course_id,
|
||||
':subject' => $instant->subject,
|
||||
':text' => $instant->text,
|
||||
':timestamp' => $instant->timestamp,
|
||||
':sent' => $instant->sent,
|
||||
));
|
||||
$query->execute();
|
||||
}
|
||||
}
|
||||
$db->query(NULL, "DROP TABLE instants");
|
||||
$db->query(NULL, "DROP TABLE subscriptions");
|
||||
$db->query(NULL, "DROP TABLE subscriptions_groups");
|
||||
$db->query(NULL, "DROP TABLE clients_subscriptions");
|
||||
$db->commit();
|
||||
echo 'All done.' . PHP_EOL;
|
||||
}
|
||||
catch (Database_Exception $e)
|
||||
{
|
||||
|
@ -64,8 +119,21 @@ class Migration_Kangana_20161016123906 extends Minion_Migration_Base {
|
|||
*/
|
||||
public function down(Kohana_Database $db)
|
||||
{
|
||||
echo 'An automatic rollback for this migration back is not implemented.';
|
||||
return false;
|
||||
echo 'Opening the transaction.'.PHP_EOL;
|
||||
$db->begin();
|
||||
try
|
||||
{
|
||||
echo 'Altering tables.'.PHP_EOL;
|
||||
$db->query(NULL, 'ALTER TABLE `letters` DROP COLUMN `timestamp`');
|
||||
$db->query(NULL, "alter table `letters` DROP column `sent`");
|
||||
$db->query(NULL, "alter table `letters` DROP column `is_draft`");
|
||||
$db->query(NULL, "alter table `courses` DROP column `type`");
|
||||
$db->commit();
|
||||
echo 'All done.' . PHP_EOL;
|
||||
}
|
||||
catch (Database_Exception $e)
|
||||
{
|
||||
$db->rollback();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue