summaryrefslogtreecommitdiffstats
path: root/application/migrations/002_add_users.php
blob: d744022632870a9b809ac2ab72c336fd20b1dbf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Add_users extends CI_Migration {

	public function up()
	{
		if ($this->db->dbdriver == 'postgre')
		{
			$this->db->query('
				CREATE TABLE IF NOT EXISTS "users" (
					"id" serial PRIMARY KEY,
					"username" character varying(32) NOT NULL,
					"password" character varying(60) NOT NULL,
					"email" character varying(255) NOT NULL
				)
			');

			$this->db->query('
				CREATE TABLE IF NOT EXISTS "ci_sessions" (
					"session_id" character varying(40) NOT NULL DEFAULT 0,
					"ip_address" character varying(16) NOT NULL DEFAULT 0,
					"user_agent" character varying(120) NOT NULL,
					"last_activity" integer NOT NULL DEFAULT 0,
					"user_data" text NOT NULL,
					PRIMARY KEY ("session_id")
				);
				CREATE INDEX "ci_sessions_last_activity_idx" ON "ci_sessions" ("last_activity");
			');

			$this->db->query('
				ALTER TABLE "files" ADD "user" integer NOT NULL DEFAULT 0;
				CREATE INDEX "user_idx" ON "files" ("user");
			');
		}
		else
		{
			$this->db->query("
				CREATE TABLE IF NOT EXISTS `users` (
					`id` int(8) UNSIGNED NOT NULL AUTO_INCREMENT,
					`username` varchar(32) COLLATE ascii_general_ci NOT NULL,
					`password` varchar(60) COLLATE ascii_general_ci NOT NULL,
					`email` varchar(255) COLLATE ascii_general_ci NOT NULL,
					PRIMARY KEY (`id`)
				) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			");

			$this->db->query("
				CREATE TABLE IF NOT EXISTS `ci_sessions` (
					`session_id` varchar(40) NOT NULL DEFAULT '0',
					`ip_address` varchar(16) NOT NULL DEFAULT '0',
					`user_agent` varchar(120) NOT NULL,
					`last_activity` int(10) unsigned NOT NULL DEFAULT '0',
					`user_data` text NOT NULL,
					PRIMARY KEY (`session_id`),
					KEY `last_activity_idx` (`last_activity`)
				) ENGINE=InnoDB DEFAULT CHARSET=utf8;
			");

			$this->db->query("
				ALTER TABLE `files`
				ADD `user` INT(8) UNSIGNED NOT NULL DEFAULT '0',
				ADD INDEX (`user`)
			");
		}
	}

	public function down()
	{
		$this->dbforge->drop_table('users');
		$this->dbforge->drop_table('ci_sessions');
		if ($this->db->dbdriver == 'postgre')
		{
			$this->db->query('ALTER TABLE "files" DROP "user"');
		}
		else
		{
			$this->db->query('ALTER TABLE `files` DROP `user`');
		}
	}
}