From 8040d499563b297a2d3a9c16ef476b4b771f940e Mon Sep 17 00:00:00 2001 From: REJack Date: Thu, 1 Nov 2018 22:51:25 +0100 Subject: [PATCH] added Database/Migrations (closes #3) --- Database/Migrations/.gitkeep | 0 ...0181026042034_create_ci_sessions_table.php | 45 ++++++++++++ .../20181026110732_create_users_table.php | 71 +++++++++++++++++++ .../20181031062503_create_user_variables.php | 52 ++++++++++++++ .../20181031063113_create_login_attempts.php | 42 +++++++++++ .../20181031064211_create_groups.php | 37 ++++++++++ .../20181031064431_create_group_to_user.php | 34 +++++++++ .../20181031064550_create_group_to_group.php | 34 +++++++++ .../20181031064714_create_perms.php | 37 ++++++++++ .../20181031065111_create_perm_to_user.php | 34 +++++++++ .../20181031065240_create_perm_to_group.php | 34 +++++++++ .../20181031072542_create_default_groups.php | 36 ++++++++++ .../20181031072914_create_default_admin.php | 41 +++++++++++ 13 files changed, 497 insertions(+) create mode 100644 Database/Migrations/.gitkeep create mode 100644 Database/Migrations/20181026042034_create_ci_sessions_table.php create mode 100644 Database/Migrations/20181026110732_create_users_table.php create mode 100644 Database/Migrations/20181031062503_create_user_variables.php create mode 100644 Database/Migrations/20181031063113_create_login_attempts.php create mode 100644 Database/Migrations/20181031064211_create_groups.php create mode 100644 Database/Migrations/20181031064431_create_group_to_user.php create mode 100644 Database/Migrations/20181031064550_create_group_to_group.php create mode 100644 Database/Migrations/20181031064714_create_perms.php create mode 100644 Database/Migrations/20181031065111_create_perm_to_user.php create mode 100644 Database/Migrations/20181031065240_create_perm_to_group.php create mode 100644 Database/Migrations/20181031072542_create_default_groups.php create mode 100644 Database/Migrations/20181031072914_create_default_admin.php diff --git a/Database/Migrations/.gitkeep b/Database/Migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Database/Migrations/20181026042034_create_ci_sessions_table.php b/Database/Migrations/20181026042034_create_ci_sessions_table.php new file mode 100644 index 0000000..891a1c6 --- /dev/null +++ b/Database/Migrations/20181026042034_create_ci_sessions_table.php @@ -0,0 +1,45 @@ +forge->addField([ + 'id' => [ + 'type' => 'VARCHAR', + 'constraint' => 128, + 'null' => false + ], + 'ip_address' => [ + 'type' => 'VARCHAR', + 'constraint' => 45, + 'null' => false + ], + 'timestamp' => [ + 'type' => 'INT', + 'constraint' => 10, + 'unsigned' => true, + 'null' => false, + 'default' => 0 + ], + 'data' => [ + 'type' => 'TEXT', + 'null' => false, + 'default' => '' + ], + ]); + $this->forge->addKey('id', true); + $this->forge->addKey('timestamp'); + $this->forge->createTable('ci_sessions', true); + } + + //-------------------------------------------------------------------- + + public function down() + { + $this->forge->dropTable('ci_sessions', true); + } +} diff --git a/Database/Migrations/20181026110732_create_users_table.php b/Database/Migrations/20181026110732_create_users_table.php new file mode 100644 index 0000000..4610603 --- /dev/null +++ b/Database/Migrations/20181026110732_create_users_table.php @@ -0,0 +1,71 @@ +forge->addField([ + 'id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + 'auto_increment' => TRUE, + ), + 'email' => array( + 'type' => 'VARCHAR', + 'constraint' => 254, + ), + 'username' => array( + 'type' => 'VARCHAR', + 'constraint' => 150, + 'null' => TRUE, + ), + 'password' => array( + 'type' => 'VARCHAR', + 'constraint' => 60, + ), + 'banned' => array( + 'type' => 'TINYINT', + 'constraint' => 1, + 'null' => TRUE, + 'default' => 0, + ), + 'created_datetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP', + 'updated_datetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP', + 'last_activity' => array( + 'type' => 'DATETIME', + 'default' => NULL, + ), + 'last_ip_address' => array( + 'type' => 'VARCHAR', + 'constraint' => 39, + 'default' => '', + ), + 'last_login' => array( + 'type' => 'DATETIME', + 'default' => NULL, + ), + 'deleted' => array( + 'type' => 'TINYINT', + 'constraint' => 1, + 'default' => 0, + ), + ]); + $this->forge->addKey('id', TRUE); + $this->forge->createTable($config->dbTableUsers, TRUE); + } + + //-------------------------------------------------------------------- + + public function down() + { + $config = new AauthConfig(); + $this->forge->dropTable($config->dbTableUsers, true); + } +} diff --git a/Database/Migrations/20181031062503_create_user_variables.php b/Database/Migrations/20181031062503_create_user_variables.php new file mode 100644 index 0000000..cef74fb --- /dev/null +++ b/Database/Migrations/20181031062503_create_user_variables.php @@ -0,0 +1,52 @@ +forge->addField([ + 'id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + 'auto_increment' => TRUE, + ), + 'user_id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + ), + 'data_key' => array( + 'type' => 'VARCHAR', + 'constraint' => 100, + ), + 'data_value' => array( + 'type' => 'TEXT', + ), + 'created_datetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP', + 'updated_datetime' => array( + 'type' => 'DATETIME', + 'default' => NULL, + ), + 'system' => array( + 'type' => 'TINYINT', + 'constraint' => 1, + 'default' => 0, + ), + ]); + $this->forge->addKey('id', TRUE); + $this->forge->createTable($config->dbTableUserVariables, TRUE); + } + + //-------------------------------------------------------------------- + + public function down() + { + $config = new AauthConfig(); + $this->forge->dropTable($config->dbTableUserVariables, true); + } +} diff --git a/Database/Migrations/20181031063113_create_login_attempts.php b/Database/Migrations/20181031063113_create_login_attempts.php new file mode 100644 index 0000000..577707f --- /dev/null +++ b/Database/Migrations/20181031063113_create_login_attempts.php @@ -0,0 +1,42 @@ +forge->addField([ + 'id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + 'auto_increment' => TRUE, + ), + 'ip_address' => array( + 'type' => 'VARCHAR', + 'constraint' => 39, + 'default' => 0, + ), + 'count' => array( + 'type' => 'TINYINT', + 'constraint' => 2, + 'default' => 0, + ), + 'created_datetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP', + 'updated_datetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP', + ]); + $this->forge->addKey('id', TRUE); + $this->forge->createTable($config->dbTableLoginAttempts, TRUE); + } + + //-------------------------------------------------------------------- + + public function down() + { + $config = new AauthConfig(); + $this->forge->dropTable($config->dbTableLoginAttempts, true); + } +} diff --git a/Database/Migrations/20181031064211_create_groups.php b/Database/Migrations/20181031064211_create_groups.php new file mode 100644 index 0000000..37edb8a --- /dev/null +++ b/Database/Migrations/20181031064211_create_groups.php @@ -0,0 +1,37 @@ +forge->addField([ + 'id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + 'auto_increment' => TRUE, + ), + 'name' => array( + 'type' => 'VARCHAR', + 'constraint' => 100, + ), + 'definition' => array( + 'type' => 'TEXT', + ), + ]); + $this->forge->addKey('id', TRUE); + $this->forge->createTable($config->dbTableGroups, TRUE); + } + + //-------------------------------------------------------------------- + + public function down() + { + $config = new AauthConfig(); + $this->forge->dropTable($config->dbTableGroups, true); + } +} diff --git a/Database/Migrations/20181031064431_create_group_to_user.php b/Database/Migrations/20181031064431_create_group_to_user.php new file mode 100644 index 0000000..24e3183 --- /dev/null +++ b/Database/Migrations/20181031064431_create_group_to_user.php @@ -0,0 +1,34 @@ +forge->addField([ + 'group_id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + ), + 'user_id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + ), + ]); + $this->forge->addKey(array('group_id','user_id'), TRUE); + $this->forge->createTable($config->dbTableGroupToUser, TRUE); + } + + //-------------------------------------------------------------------- + + public function down() + { + $config = new AauthConfig(); + $this->forge->dropTable($config->dbTableGroupToUser, true); + } +} diff --git a/Database/Migrations/20181031064550_create_group_to_group.php b/Database/Migrations/20181031064550_create_group_to_group.php new file mode 100644 index 0000000..73b2a11 --- /dev/null +++ b/Database/Migrations/20181031064550_create_group_to_group.php @@ -0,0 +1,34 @@ +forge->addField([ + 'group_id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + ), + 'subgroup_id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + ), + ]); + $this->forge->addKey(array('group_id','subgroup_id'), TRUE); + $this->forge->createTable($config->dbTableGroupToGroup, TRUE); + } + + //-------------------------------------------------------------------- + + public function down() + { + $config = new AauthConfig(); + $this->forge->dropTable($config->dbTableGroupToGroup, true); + } +} diff --git a/Database/Migrations/20181031064714_create_perms.php b/Database/Migrations/20181031064714_create_perms.php new file mode 100644 index 0000000..34b74cd --- /dev/null +++ b/Database/Migrations/20181031064714_create_perms.php @@ -0,0 +1,37 @@ +forge->addField([ + 'id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + 'auto_increment' => TRUE, + ), + 'name' => array( + 'type' => 'VARCHAR', + 'constraint' => 100, + ), + 'definition' => array( + 'type' => 'TEXT', + ), + ]); + $this->forge->addKey('id', TRUE); + $this->forge->createTable($config->dbTablePerms, TRUE); + } + + //-------------------------------------------------------------------- + + public function down() + { + $config = new AauthConfig(); + $this->forge->dropTable($config->dbTablePerms, true); + } +} diff --git a/Database/Migrations/20181031065111_create_perm_to_user.php b/Database/Migrations/20181031065111_create_perm_to_user.php new file mode 100644 index 0000000..11fc722 --- /dev/null +++ b/Database/Migrations/20181031065111_create_perm_to_user.php @@ -0,0 +1,34 @@ +forge->addField([ + 'perm_id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + ), + 'user_id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + ), + ]); + $this->forge->addKey(array('perm_id','user_id'), TRUE); + $this->forge->createTable($config->dbTablePermToUser, TRUE); + } + + //-------------------------------------------------------------------- + + public function down() + { + $config = new AauthConfig(); + $this->forge->dropTable($config->dbTablePermToUser, true); + } +} diff --git a/Database/Migrations/20181031065240_create_perm_to_group.php b/Database/Migrations/20181031065240_create_perm_to_group.php new file mode 100644 index 0000000..7dbed03 --- /dev/null +++ b/Database/Migrations/20181031065240_create_perm_to_group.php @@ -0,0 +1,34 @@ +forge->addField([ + 'perm_id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + ), + 'group_id' => array( + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => TRUE, + ), + ]); + $this->forge->addKey(array('perm_id','user_id'), TRUE); + $this->forge->createTable($config->dbTablePermToGroup, TRUE); + } + + //-------------------------------------------------------------------- + + public function down() + { + $config = new AauthConfig(); + $this->forge->dropTable($config->dbTablePermToGroup, true); + } +} diff --git a/Database/Migrations/20181031072542_create_default_groups.php b/Database/Migrations/20181031072542_create_default_groups.php new file mode 100644 index 0000000..e8ad43b --- /dev/null +++ b/Database/Migrations/20181031072542_create_default_groups.php @@ -0,0 +1,36 @@ + $config->adminGroup, + 'definition' => 'Administators', + ], + [ + 'name' => $config->defaultGroup, + 'definition' => 'Users', + ], + [ + 'name' => $config->publicGroup, + 'definition' => 'Guests', + ], + ]; + + $this->db->table($config->dbTableGroups)->insertBatch($data); + } + + //-------------------------------------------------------------------- + + public function down() + { + $this->db->table($config->dbTableGroups)->truncate(); + } +} diff --git a/Database/Migrations/20181031072914_create_default_admin.php b/Database/Migrations/20181031072914_create_default_admin.php new file mode 100644 index 0000000..26ae234 --- /dev/null +++ b/Database/Migrations/20181031072914_create_default_admin.php @@ -0,0 +1,41 @@ + 'admin', + 'email' => 'admin@example.com', + 'password' => password_hash('password123456', $config->passwordHashAlgo, $config->passwordHashOptions), + ]; + + $this->db->table($config->dbTableUsers)->insert($data); + + $data = [ + [ + 'group_id' => 1, + 'user_id' => 1, + ], + [ + 'group_id' => 2, + 'user_id' => 1, + ], + ]; + + $this->db->table($config->dbTableGroupToUser)->insertBatch($data); + } + + //-------------------------------------------------------------------- + + public function down() + { + $this->db->table($config->dbTableUsers)->truncate(); + $this->db->table($config->dbTableGroupToUser)->truncate(); + } +}