Completed
Pull Request — master (#265)
by
unknown
16:35 queued 05:23
created

InitDb::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application\Migrations;
11
12
use Phinx\Migration\AbstractMigration;
13
14
/**
15
 * InitDb migration class
16
 */
17
class InitDb extends AbstractMigration
18
{
19
    /**
20
     * Migrate Up.
21
     */
22
    public function up()
23
    {
24
        $this->execute('SET storage_engine=INNODB;');
25
        $this->execute('CREATE TABLE users (id BIGINT(20) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT, login VARCHAR(255), email VARCHAR(255), created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, updated TIMESTAMP NOT NULL, status VARCHAR(32) DEFAULT \'disabled\' NOT NULL);');
26
        $this->execute('CREATE UNIQUE INDEX users_login_uindex ON users (login);');
27
        $this->execute('CREATE UNIQUE INDEX users_email_uindex ON users (email);');
28
        $this->execute('CREATE TABLE users_actions (userId BIGINT(20) unsigned NOT NULL, code VARCHAR(32) NOT NULL, action VARCHAR(32) NOT NULL, params LONGTEXT, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, expired TIMESTAMP NOT NULL, CONSTRAINT users_actions_userId_code_pk PRIMARY KEY (userId, code), CONSTRAINT users_actions_users_id_fk FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE);');
29
        $this->execute('CREATE UNIQUE INDEX users_actions_userId_action_uindex ON users_actions (userId, action);');
30
        $this->execute('CREATE TABLE auth ( userId BIGINT(20) unsigned NOT NULL, provider VARCHAR(64) NOT NULL, foreignKey VARCHAR(255) NOT NULL, token VARCHAR(64) NOT NULL, tokenSecret VARCHAR(64) NOT NULL, tokenType VARCHAR(32) NOT NULL, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, updated TIMESTAMP NOT NULL, expired TIMESTAMP NOT NULL, CONSTRAINT auth_userId_provider_pk PRIMARY KEY (userId, provider), CONSTRAINT auth_users_id_fk FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE);');
31
        $this->execute('CREATE TABLE acl_roles ( id INT(10) unsigned NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, CONSTRAINT acl_roles_id_name_pk PRIMARY KEY (id, name));');
32
        $this->execute('CREATE UNIQUE INDEX acl_roles_name_uindex ON acl_roles (name);');
33
        $this->execute('CREATE TABLE acl_privileges ( roleId INT(10) unsigned NOT NULL, module VARCHAR(32) NOT NULL, privilege VARCHAR(32) NOT NULL, CONSTRAINT acl_privileges_acl_roles_id_fk FOREIGN KEY (roleId) REFERENCES acl_roles (id) ON DELETE CASCADE ON UPDATE CASCADE);');
34
        $this->execute('CREATE UNIQUE INDEX acl_privileges_roleId_module_privilege_uindex ON acl_privileges (roleId, module, privilege);');
35
        $this->execute('CREATE TABLE acl_users_roles ( userId BIGINT(20) unsigned NOT NULL, roleId INT(10) unsigned NOT NULL, CONSTRAINT acl_users_roles_userId_roleId_pk PRIMARY KEY (userId, roleId), CONSTRAINT acl_users_roles_users_id_fk FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT acl_users_roles_acl_roles_id_fk FOREIGN KEY (roleId) REFERENCES acl_roles (id) ON DELETE CASCADE ON UPDATE CASCADE);');
36
        $this->execute('CREATE TABLE pages ( id INT(10) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT, userId BIGINT(20) unsigned, title LONGTEXT NOT NULL, alias VARCHAR(255) NOT NULL, content LONGTEXT, keywords LONGTEXT, description LONGTEXT, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, updated TIMESTAMP NOT NULL, CONSTRAINT pages_users_id_fk FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE);');
37
        $this->execute('CREATE UNIQUE INDEX pages_alias_uindex ON pages (alias);');
38
        $this->execute('CREATE TABLE com_settings ( id INT(10) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT, alias VARCHAR(255) NOT NULL, options LONGTEXT, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, updated TIMESTAMP NOT NULL, countPerPage SMALLINT(6) DEFAULT \'10\' NOT NULL, relatedTable VARCHAR(64));');
39
        $this->execute('CREATE UNIQUE INDEX com_settings_alias_uindex ON com_settings (alias);');
40
        $this->execute('CREATE TABLE com_content ( id BIGINT(20) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT, settingsId INT(10) unsigned NOT NULL, foreignKey INT(10) unsigned NOT NULL, userId BIGINT(20) unsigned NOT NULL, parentId BIGINT(20) unsigned, content LONGTEXT, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, updated TIMESTAMP NOT NULL,  status VARCHAR(255) DEFAULT \'active\' NOT NULL, CONSTRAINT com_content_com_settings_id_fk FOREIGN KEY (settingsId) REFERENCES com_settings (id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT com_content_users_id_fk FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT com_content_com_content_id_fk FOREIGN KEY (parentId) REFERENCES com_content (id) ON DELETE CASCADE ON UPDATE CASCADE);');
41
        $this->execute('CREATE INDEX comments_target ON com_content (settingsId, foreignKey);');
42
43
        $this->execute('LOCK TABLES `users` WRITE;');
44
        $this->execute('INSERT INTO `users` (`id`, `login`, `email`, `created`, `updated`, `status`) VALUES (1,\'system\',NULL,\'2012-11-09 07:37:58\',NULL,\'disabled\'), (2,\'admin\',NULL,\'2012-11-09 07:38:41\',NULL,\'active\');');
45
        $this->execute('UNLOCK TABLES;');
46
        $this->execute('LOCK TABLES `acl_roles` WRITE;');
47
        $this->execute('INSERT INTO `acl_roles` (`id`, `name`, `created`) VALUES (1,\'system\',\'2012-11-09 07:37:31\'), (2,\'admin\',\'2012-11-09 07:37:33\'), (3,\'member\',\'2012-11-09 07:37:37\'), (4,\'guest\',\'2012-11-09 07:37:44\');');
48
        $this->execute('UNLOCK TABLES;');
49
        $this->execute('LOCK TABLES `acl_privileges` WRITE;');
50
        $this->execute('INSERT INTO `acl_privileges` (`roleId`, `module`, `privilege`) VALUES (2,\'acl\',\'Management\'), (2,\'acl\',\'View\'), (2,\'cache\',\'Management\'), (2,\'dashboard\',\'Dashboard\'), (2,\'pages\',\'Management\'), (2,\'system\',\'Info\'), (2,\'users\',\'EditEmail\'), (2,\'users\',\'EditPassword\'), (2,\'users\',\'Management\'), (2,\'users\',\'ViewProfile\'), (3,\'users\',\'EditEmail\'), (3,\'users\',\'EditPassword\'), (3,\'users\',\'ViewProfile\'), (4,\'users\',\'ViewProfile\');');
51
        $this->execute('UNLOCK TABLES;');
52
        $this->execute('LOCK TABLES `acl_users_roles` WRITE;');
53
        $this->execute('INSERT INTO `acl_users_roles` (`userId`, `roleId`) VALUES (1,1), (2,2);');
54
        $this->execute('UNLOCK TABLES;');
55
        $this->execute('LOCK TABLES `auth` WRITE;');
56
        $this->execute('INSERT INTO `auth` (`userId`, `provider`, `foreignKey`, `token`, `tokenSecret`, `tokenType`, `created`) VALUES (2,\'equals\',\'admin\',\'$2y$10$4a454775178c3f89d510fud2T.xtw01Ir.Jo.91Dr3nL2sz3OyVpK\',\'\',\'access\',\'2012-11-09 07:40:46\');');
57
        $this->execute('UNLOCK TABLES;');
58
        $this->execute('LOCK TABLES `pages` WRITE;');
59
        $this->execute('INSERT INTO `pages` (`id`, `title`, `alias`, `content`, `keywords`, `description`, `created`, `updated`, `userId`) VALUES (1,\'About Bluz Framework\',\'about\',\'<p>Bluz Lightweight PHP Framework!</p>\',\'about bluz framework\',\'About Bluz\',\'2012-04-09 18:34:03\',\'2014-05-12 11:01:03\', 1);');
60
        $this->execute('UNLOCK TABLES;');
61
    }
62
    /**
63
     * Migrate Down.
64
     */
65
    public function down()
66
    {
67
        $this->dropTable('com_content');
68
        $this->dropTable('com_settings');
69
        $this->dropTable('pages');
70
        $this->dropTable('acl_users_roles');
71
        $this->dropTable('acl_privileges');
72
        $this->dropTable('acl_roles');
73
        $this->dropTable('auth');
74
        $this->dropTable('users_actions');
75
        $this->dropTable('users');
76
    }
77
}
78