m141001_145619_init::createUserTables()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 96
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 8.3859
c 0
b 0
f 0
cc 1
eloc 67
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class m141001_145619_init extends CDbMigration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
	/**
7
	 * For data insertions, this is the current time.
8
	 * @var integer
9
	 */
10
	private $_moment = 0;
11
12
	public function safeUp()
13
	{
14
		$this->_moment = time();
15
16
		// Try to get the table names, if we get something back, do not run this migration
17
		try
18
		{
19
			$test = Yii::app()->db->schema->getTables();
20
			if (count($test) <= 1)
21
				throw new Exception('CiiMS doesn\'t exist. Applying base migration');
22
			return true;
23
		}
24
		catch (Exception $e)
25
		{
26
			// Yii will throw an exception if Yii::app()->db->schema is undefined (which is should be if we're doing this for the first time)
27
			// This SHOULD throw an error if we're running this for the first time - it's the only way to check if we have a valid db or not.
28
		}
29
30
		// Otherwise, run the install migration
31
		$this->createUserTables();
32
33
		$this->createCategories();
34
35
		$this->createContent();
36
37
		$this->createConfiguration();
38
	}
39
40
	public function safeDown()
41
	{
42
		echo "m141001_145619_init does not support migration down.\n";
43
		return false;
44
	}
45
46
	/**
47
	 * Creates the tables, indexes, and relations for users
48
	 */
49
	private function createUserTables()
50
	{
51
		$this->createTable('users', array(
52
			'id' 		=> 'pk',
53
			'email' 	=> 'string NOT NULL',
54
			'password' 	=> 'string NOT NULL',
55
			'username'  => 'string NOT NULL',
56
			'user_role' => 'integer DEFAULT 1',
57
			'status' 	=> 'integer DEFAULT 1',
58
			'created' 	=> 'integer',
59
			'updated' 	=> 'integer'
60
		));
61
62
		$this->createTable('user_roles', array(
63
			'id' 		=> 'pk',
64
			'name' 		=> 'string NOT NULL',
65
			'created' 	=> 'integer',
66
			'updated' 	=> 'integer'
67
		));
68
69
		$this->createTable('user_metadata', array(
70
			'user_id' 	  => 'integer',
71
			'key'		  => 'string NOT NULL',
72
			'value' 	  => 'text NOT NULL',
73
			'entity_type' => 'integer',
74
			'created' 	  => 'integer',
75
			'updated' 	  => 'integer'
76
		));
77
78
		// Create the necessary indexes on the columns
79
		$this->createIndex('user_email', 'users', 'email', true);
80
		$this->createIndex('user_username', 'users', 'username', true);
81
		$this->createIndex('user_metadata', 'user_metadata', 'user_id, key', false);
82
83
		$this->addPrimaryKey('user_metadata_composite', 'user_metadata', 'user_id,key');
84
85
		// Setup the foreign key constraints
86
		$this->addForeignKey('user_metadata_relation_fk', 'user_metadata', 'user_id', 'users', 'id', 'CASCADE', 'NO ACTION');
87
88
		// Insert data into the tables
89
		$this->insert('user_roles', array(
90
			'id'      => 1,
91
			'name' 	  => 'User',
92
			'created' => $this->_moment,
93
			'updated' => $this->_moment
94
		));
95
96
		$this->insert('user_roles', array(
97
			'id'      => 2,
98
			'name'    => 'Pending',
99
			'created' => $this->_moment,
100
			'updated' => $this->_moment
101
		));
102
103
		$this->insert('user_roles', array(
104
			'id'      => 3,
105
			'name'    => 'Suspended',
106
			'created' => $this->_moment,
107
			'updated' => $this->_moment
108
		));
109
110
		$this->insert('user_roles', array(
111
			'id'      => 5,
112
			'name'    => 'Collaborator',
113
			'created' => $this->_moment,
114
			'updated' => $this->_moment
115
		));
116
117
		$this->insert('user_roles', array(
118
			'id'      => 6,
119
			'name'    => 'Author',
120
			'created' => $this->_moment,
121
			'updated' => $this->_moment
122
		));
123
124
		$this->insert('user_roles', array(
125
			'id'      => 7,
126
			'name'    => 'User',
127
			'created' => $this->_moment,
128
			'updated' => $this->_moment
129
		));
130
131
		$this->insert('user_roles', array(
132
			'id'      => 8,
133
			'name'    => 'Publisher',
134
			'created' => $this->_moment,
135
			'updated' => $this->_moment
136
		));
137
138
		$this->insert('user_roles', array(
139
			'id'      => 9,
140
			'name'    => 'Administrator',
141
			'created' => $this->_moment,
142
			'updated' => $this->_moment
143
		));
144
	}
145
146
	/**
147
	 * Create the categories and relations
148
	 */
149
	private function createCategories()
150
	{
151
		// Categories
152
		$this->createTable('categories', array(
153
			'id' 		=> 'pk',
154
			'parent_id' => 'integer DEFAULT 1',
155
			'name' 		=> 'string NOT NULL',
156
			'slug' 	    => 'string NOT NULL',
157
			'created' 	=> 'integer',
158
			'updated' 	=> 'integer'
159
		));
160
161
		$this->createTable('categories_metadata', array(
162
			'category_id' => 'integer',
163
			'key'		  => 'string NOT NULL',
164
			'value' 	  => 'text NOT NULL',
165
			'created' 	  => 'integer',
166
			'updated' 	  => 'integer'
167
		));
168
169
		// Insert the first record into the categories table
170
		$this->insert('categories', array(
171
			'id'      => 1,
172
			'name'    => 'Uncategorized',
173
			'slug'    => 'uncategorized',
174
			'created' => $this->_moment,
175
			'updated' => $this->_moment
176
		));
177
178
		$this->addPrimaryKey('categories_metadata_composite', 'categories_metadata', 'category_id,key');
179
		$this->addForeignKey('categories_parents_fk', 'categories', 'parent_id', 'categories', 'id', 'CASCADE', 'NO ACTION');
180
		$this->addForeignKey('categories_metadata_fk', 'categories_metadata', 'category_id', 'categories', 'id', 'CASCADE', 'NO ACTION');
181
	}
182
183
	/**
184
	 * Creates the content, comemnts, and necessary relations
185
	 */
186
	private function createContent()
187
	{
188
		$this->createTable('content', array(
189
			'id'		  => 'integer NOT NULL',
190
			'vid' 		  => 'integer NOT NULL DEFAULT 1',
191
			'title'		  => 'string  NOT NULL',
192
			'content'     => 'text NOT NULL',
193
			'excerpt'     => 'text NOT NULL',
194
			'slug'        => 'string NOT NULL',
195
			'category_id' => 'integer DEFAULT 1',
196
			'author_id'   => 'integer DEFAULT 1',
197
			'type_id'     => 'integer DEFAULT 2',
198
			'commentable' => 'integer DEFAULT 1',
199
			'password' 	  => 'string DEFAULT NULL',
200
			'status'      => 'integer DEFAULT 0',
201
			'like_count'  => 'integer DEFAULT 0',
202
			'published'	  => 'integer',
203
			'created'     => 'integer',
204
			'updated' 	  => 'integer'
205
		));
206
207
		$this->createTable('content_types', array(
208
			'id'          => 'pk',
209
			'name'		  => 'string NOT NULL',
210
			'created'     => 'integer',
211
			'updated' 	  => 'integer'
212
		));
213
214
		$this->createTable('content_metadata', array(
215
			'content_id'  => 'integer',
216
			'key'		  => 'string NOT NULL',
217
			'value' 	  => 'text NOT NULL',
218
			'created' 	  => 'integer',
219
			'updated' 	  => 'integer'
220
		));
221
222
		$this->createTable('comments', array(
223
			'id'		  => 'pk',
224
			'content_id'  => 'integer',
225
			'author_id'   => 'integer',
226
			'comment'     => 'integer',
227
			'created'     => 'integer',
228
			'updated' 	  => 'integer'
229
		));
230
231
		$this->insert('content_types', array(
232
			'id'          => 1,
233
			'name'        => 'Static Page',
234
			'created'     => $this->_moment,
235
			'updated'     => $this->_moment
236
		));
237
238
		$this->insert('content_types', array(
239
			'id'          => 2,
240
			'name'    	  => 'Blog Post',
241
			'created'     => $this->_moment,
242
			'updated'     => $this->_moment
243
		));
244
245
		$this->addPrimaryKey('content_composite', 'content', 'id, vid');
246
247
		$this->execute('ALTER TABLE `content` CHANGE id id INT( 11 ) NOT NULL AUTO_INCREMENT');
248
249
		$this->addPrimaryKey('content_metadata_composite', 'content_metadata', 'content_id,key');
250
251
		$this->createIndex('content_author', 'content', 'author_id', false);
252
		$this->createIndex('content_category', 'content', 'category_id', false);
253
		$this->createIndex('content_type', 'content', 'type_id', false);
254
		$this->createIndex('comment_content', 'comments', 'content_id', false);
255
		$this->createIndex('comment_author', 'comments', 'author_id', false);
256
257
		$this->addForeignKey('content_category_fk', 'content', 'category_id', 'categories', 'id', 'CASCADE', 'NO ACTION');
258
		$this->addForeignKey('content_author_fk', 'content', 'author_id', 'users', 'id', 'CASCADE', 'NO ACTION');
259
		$this->addForeignKey('content_type_fk', 'content', 'type_id', 'content_types', 'id', 'CASCADE', 'NO ACTION');
260
261
		$this->addForeignKey('comments_content_id_fk', 'comments', 'content_id', 'content', 'id', 'CASCADE', 'NO ACTION');
262
		$this->addForeignKey('comments_author_fk', 'comments', 'author_id', 'users', 'id', 'CASCADE', 'NO ACTION');
263
	}
264
265
	/**
266
	 * Creates the configuration and events tables
267
	 */
268
	private function createConfiguration()
269
	{
270
		$this->createTable('configuration', array(
271
			'key'		  => 'string',
272
			'value' 	  => 'text NOT NULL',
273
			'created' 	  => 'integer',
274
			'updated' 	  => 'integer'
275
		));
276
277
		$this->createTable('events', array(
278
			'id'		  => 'pk',
279
			'event'       => 'string NOT NULL',
280
			'event_data'  => 'text DEFAULT NULL',
281
			'uri'         => 'string DEFAULT NULL',
282
			'content_id'  => 'integer DEFAULT NULL',
283
			'created' 	  => 'integer'
284
		));
285
286
		$this->addPrimaryKey('configuration_pk', 'configuration', 'key');
287
	}
288
}