|
1
|
|
|
<?php defined('BASEPATH') OR exit('No direct script access allowed'); |
|
2
|
|
|
|
|
3
|
|
|
class Migration_Favourites_Rework_Revert extends CI_Migration { |
|
4
|
|
|
public function __construct() { |
|
5
|
|
|
parent::__construct(); |
|
|
|
|
|
|
6
|
|
|
$this->load->dbforge(); |
|
7
|
|
|
} |
|
8
|
|
|
|
|
9
|
|
|
public function up() { |
|
10
|
|
|
$this->dbforge->add_field(array( |
|
11
|
|
|
'id' => array( |
|
12
|
|
|
'type' => 'MEDIUMINT', |
|
13
|
|
|
'constraint' => '8', |
|
14
|
|
|
'unsigned' => TRUE, |
|
15
|
|
|
'auto_increment' => TRUE |
|
16
|
|
|
), |
|
17
|
|
|
'chapter_id' => array( |
|
18
|
|
|
'type' => 'MEDIUMINT', |
|
19
|
|
|
'constraint' => '8', |
|
20
|
|
|
'unsigned' => TRUE |
|
21
|
|
|
//FOREIGN KEY |
|
22
|
|
|
), |
|
23
|
|
|
|
|
24
|
|
|
'chapter' => array( |
|
25
|
|
|
'type' => 'VARCHAR', |
|
26
|
|
|
'constraint' => '255', |
|
27
|
|
|
'null' => FALSE |
|
28
|
|
|
), |
|
29
|
|
|
|
|
30
|
|
|
'updated_at' => array( |
|
31
|
|
|
//Despite not actually creating the field here (it's instead done below), we still need this here so a key can be created properly. |
|
32
|
|
|
// 'type' => 'TIMESTAMP', |
|
33
|
|
|
// 'null' => FALSE, |
|
34
|
|
|
// 'on_update' => FALSE |
|
35
|
|
|
) |
|
36
|
|
|
)); |
|
37
|
|
|
$this->dbforge->add_field('updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'); //CI is annoying and auto-appends ON UPDATE which we don't want. |
|
|
|
|
|
|
38
|
|
|
$this->dbforge->add_key('id', TRUE); |
|
39
|
|
|
$this->dbforge->add_key('chapter_id'); |
|
40
|
|
|
$this->dbforge->add_key('updated_at'); |
|
41
|
|
|
$this->dbforge->create_table('tracker_favourites_new'); |
|
42
|
|
|
|
|
43
|
|
|
//We need to add data from the old favourites table |
|
44
|
|
|
$query = $this->db->select(' |
|
45
|
|
|
tf.id AS id, |
|
46
|
|
|
tc.id AS chapter_id, |
|
47
|
|
|
tf.chapter AS chapter, |
|
48
|
|
|
tf.updated_at AS updated_at') |
|
49
|
|
|
->from('tracker_favourites AS tf') |
|
50
|
|
|
//NOTE: This may break, but we'll be only running this once on a live site so it can sort itself out. |
|
51
|
|
|
->join('tracker_chapters AS tc', 'tf.user_id = tc.user_id AND tf.title_id = tc.title_id', 'left') |
|
52
|
|
|
->order_by('tf.id ASC') |
|
53
|
|
|
->get(); |
|
54
|
|
|
if($data = $query->result_array()) { |
|
55
|
|
|
$this->db->insert_batch('tracker_favourites_new', $data); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
//Rename the old DB |
|
59
|
|
|
$this->dbforge->rename_table('tracker_favourites', 'tracker_favourites_old'); |
|
60
|
|
|
|
|
61
|
|
|
//Rename the new one to old name |
|
62
|
|
|
$this->dbforge->rename_table('tracker_favourites_new', 'tracker_favourites'); |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/*** Unique/Foreign Keys ***/ |
|
66
|
|
|
//For whatever reason, dbforge lacks a unique/foreign key function. |
|
67
|
|
|
$this->db->query('ALTER TABLE `tracker_favourites` ADD UNIQUE(`chapter_id`, `chapter`);'); |
|
68
|
|
|
|
|
69
|
|
|
$this->db->query(' |
|
70
|
|
|
ALTER TABLE `tracker_favourites` |
|
71
|
|
|
ADD CONSTRAINT `FK_tracker_favourites_tracker_chapters` FOREIGN KEY (`chapter_id`) REFERENCES `tracker_chapters` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION;' |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
//Remove old DB |
|
75
|
|
|
$this->dbforge->drop_table('tracker_favourites_old'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function down() { |
|
79
|
|
|
//There's no going back. |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: