Skip to content

Commit a2a9868

Browse files
natanfelleschriskacerguis
authored andcommitted
Migrations (chriskacerguis#820)
* Translated text_rest_ip_address_time_limit * Add startup Migrations and DB Helper * Revert "Translated text_rest_ip_address_time_limit" This reverts commit fa1617d. * Add startup Migrations and DB Helper * Update users table migration
1 parent db3eef8 commit a2a9868

8 files changed

+423
-0
lines changed

application/helpers/db_helper.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* @author Natan Felles <natanfelles@gmail.com>
4+
*/
5+
defined('BASEPATH') OR exit('No direct script access allowed');
6+
7+
if ( ! function_exists('add_foreign_key'))
8+
{
9+
/**
10+
* @param string $table Table name
11+
* @param string $foreign_key Collumn name having the Foreign Key
12+
* @param string $references Table and column reference. Ex: users(id)
13+
* @param string $on_delete RESTRICT, NO ACTION, CASCADE, SET NULL, SET DEFAULT
14+
* @param string $on_update RESTRICT, NO ACTION, CASCADE, SET NULL, SET DEFAULT
15+
*
16+
* @return string SQL command
17+
*/
18+
function add_foreign_key($table, $foreign_key, $references, $on_delete = 'RESTRICT', $on_update = 'RESTRICT')
19+
{
20+
$references = explode('(', str_replace(')', '', str_replace('`', '', $references)));
21+
22+
return "ALTER TABLE `{$table}` ADD CONSTRAINT `{$table}_{$foreign_key}_fk` FOREIGN KEY (`{$foreign_key}`) REFERENCES `{$references[0]}`(`{$references[1]}`) ON DELETE {$on_delete} ON UPDATE {$on_update}";
23+
}
24+
}
25+
26+
if ( ! function_exists('drop_foreign_key'))
27+
{
28+
/**
29+
* @param string $table Table name
30+
* @param string $foreign_key Collumn name having the Foreign Key
31+
*
32+
* @return string SQL command
33+
*/
34+
function drop_foreign_key($table, $foreign_key)
35+
{
36+
return "ALTER TABLE `{$table}` DROP FOREIGN KEY `{$table}_{$foreign_key}_fk`";
37+
}
38+
}
39+
40+
if ( ! function_exists('add_trigger'))
41+
{
42+
/**
43+
* @param string $trigger_name Trigger name
44+
* @param string $table Table name
45+
* @param string $statement Command to run
46+
* @param string $time BEFORE or AFTER
47+
* @param string $event INSERT, UPDATE or DELETE
48+
* @param string $type FOR EACH ROW [FOLLOWS|PRECEDES]
49+
*
50+
* @return string SQL Command
51+
*/
52+
function add_trigger($trigger_name, $table, $statement, $time = 'BEFORE', $event = 'INSERT', $type = 'FOR EACH ROW')
53+
{
54+
return 'DELIMITER ;;' . PHP_EOL . "CREATE TRIGGER `{$trigger_name}` {$time} {$event} ON `{$table}` {$type}" . PHP_EOL . 'BEGIN' . PHP_EOL . $statement . PHP_EOL . 'END;' . PHP_EOL . 'DELIMITER ;;';
55+
}
56+
}
57+
58+
if ( ! function_exists('drop_trigger'))
59+
{
60+
/**
61+
* @param string $trigger_name Trigger name
62+
*
63+
* @return string SQL Command
64+
*/
65+
function drop_trigger($trigger_name)
66+
{
67+
return "DROP TRIGGER {$trigger_name};";
68+
}
69+
}

application/helpers/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>403 Forbidden</title>
5+
</head>
6+
<body>
7+
8+
<p>Directory access is forbidden.</p>
9+
10+
</body>
11+
</html>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* @author Natan Felles <natanfelles@gmail.com>
4+
*/
5+
defined('BASEPATH') OR exit('No direct script access allowed');
6+
7+
/**
8+
* Class Migration_create_table_users
9+
*
10+
* @property CI_DB_forge $dbforge
11+
* @property CI_DB_query_builder $db
12+
*/
13+
class Migration_create_table_users extends CI_Migration {
14+
15+
16+
protected $table = 'users';
17+
18+
19+
public function up()
20+
{
21+
$fields = array(
22+
'id' => [
23+
'type' => 'INT(11)',
24+
'auto_increment' => TRUE,
25+
'unsigned' => TRUE,
26+
],
27+
'email' => [
28+
'type' => 'VARCHAR(255)',
29+
'unique' => TRUE,
30+
],
31+
'password' => [
32+
'type' => 'VARCHAR(64)',
33+
],
34+
'firstname' => [
35+
'type' => 'VARCHAR(32)',
36+
],
37+
'lastname' => [
38+
'type' => 'VARCHAR(32)',
39+
],
40+
'created_at' => [
41+
'type' => 'DATETIME',
42+
],
43+
);
44+
$this->dbforge->add_field($fields);
45+
$this->dbforge->add_key('id', TRUE);
46+
$this->dbforge->create_table($this->table, TRUE);
47+
48+
/*for ($i = 1; $i <= 100; $i++)
49+
{
50+
$this->db->insert($this->table, [
51+
'email' => "user-{$i}@mail.com",
52+
'password' => password_hash('codeigniter', PASSWORD_DEFAULT),
53+
'firstname' => "Firstname {$i}",
54+
'lastname' => "Lastname {$i}",
55+
'created_at' => date('Y-' . rand(1, 12) . '-' . rand(1, 28) . ' H:i:s'),
56+
]);
57+
}*/
58+
}
59+
60+
61+
public function down()
62+
{
63+
if ($this->db->table_exists($this->table))
64+
{
65+
$this->dbforge->drop_table($this->table);
66+
}
67+
}
68+
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* @author Natan Felles <natanfelles@gmail.com>
4+
*/
5+
defined('BASEPATH') OR exit('No direct script access allowed');
6+
7+
/**
8+
* Class Migration_create_table_api_keys
9+
*
10+
* @property CI_DB_forge $dbforge
11+
* @property CI_DB_query_builder $db
12+
*/
13+
class Migration_create_table_api_keys extends CI_Migration {
14+
15+
16+
public function up()
17+
{
18+
$table = config_item('rest_keys_table');
19+
$fields = array(
20+
'id' => [
21+
'type' => 'INT(11)',
22+
'auto_increment' => TRUE,
23+
'unsigned' => TRUE,
24+
],
25+
'user_id' => [
26+
'type' => 'INT(11)',
27+
'unsigned' => TRUE,
28+
],
29+
config_item('rest_key_column') => [
30+
'type' => 'VARCHAR(' . config_item('rest_key_length') . ')',
31+
'unique' => TRUE,
32+
],
33+
'level' => [
34+
'type' => 'INT(2)',
35+
],
36+
'ignore_limits' => [
37+
'type' => 'TINYINT(1)',
38+
'default' => 0,
39+
],
40+
'is_private_key' => [
41+
'type' => 'TINYINT(1)',
42+
'default' => 0,
43+
],
44+
'ip_addresses' => [
45+
'type' => 'TEXT',
46+
'null' => TRUE,
47+
],
48+
'date_created' => [
49+
'type' => 'INT(11)',
50+
],
51+
);
52+
$this->dbforge->add_field($fields);
53+
$this->dbforge->add_key('id', TRUE);
54+
$this->dbforge->create_table($table);
55+
$this->db->query(add_foreign_key($table, 'user_id', 'users(id)', 'CASCADE', 'CASCADE'));
56+
}
57+
58+
59+
public function down()
60+
{
61+
$table = config_item('rest_key_column');
62+
if ($this->db->table_exists($table))
63+
{
64+
$this->db->query(drop_foreign_key($table, 'user_id'));
65+
$this->dbforge->drop_table($table);
66+
}
67+
}
68+
69+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* @author Natan Felles <natanfelles@gmail.com>
4+
*/
5+
defined('BASEPATH') OR exit('No direct script access allowed');
6+
7+
/**
8+
* Class Migration_create_table_api_logs
9+
*
10+
* @property CI_DB_forge $dbforge
11+
* @property CI_DB_query_builder $db
12+
*/
13+
class Migration_create_table_api_logs extends CI_Migration {
14+
15+
16+
public function up()
17+
{
18+
$table = config_item('rest_logs_table');
19+
$fields = array(
20+
'id' => [
21+
'type' => 'INT(11)',
22+
'auto_increment' => TRUE,
23+
'unsigned' => TRUE,
24+
],
25+
'api_key' => [
26+
'type' => 'VARCHAR(' . config_item('rest_key_length') . ')',
27+
],
28+
'uri' => [
29+
'type' => 'VARCHAR(255)',
30+
],
31+
'method' => [
32+
'type' => 'ENUM("get","post","options","put","patch","delete")',
33+
],
34+
'params' => [
35+
'type' => 'TEXT',
36+
'null' => TRUE,
37+
],
38+
'ip_address' => [
39+
'type' => 'VARCHAR(45)',
40+
],
41+
'time' => [
42+
'type' => 'INT(11)',
43+
],
44+
'rtime' => [
45+
'type' => 'FLOAT',
46+
'null' => TRUE,
47+
],
48+
'authorized' => [
49+
'type' => 'VARCHAR(1)',
50+
],
51+
'response_code' => [
52+
'type' => 'SMALLINT(3)',
53+
'null' => TRUE,
54+
'default' => 0,
55+
],
56+
);
57+
$this->dbforge->add_field($fields);
58+
$this->dbforge->add_key('id', TRUE);
59+
$this->dbforge->create_table($table);
60+
/*$this->db->query(add_foreign_key($table, 'api_key',
61+
config_item('rest_keys_table') . '(' . config_item('rest_key_column') . ')', 'CASCADE', 'CASCADE'));*/
62+
}
63+
64+
65+
public function down()
66+
{
67+
$table = config_item('rest_logs_table');
68+
if ($this->db->table_exists($table))
69+
{
70+
// $this->db->query(drop_foreign_key($table, 'api_key'));
71+
$this->dbforge->drop_table($table);
72+
}
73+
}
74+
75+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* @author Natan Felles <natanfelles@gmail.com>
4+
*/
5+
defined('BASEPATH') OR exit('No direct script access allowed');
6+
7+
/**
8+
* Class Migration_create_table_api_access
9+
*
10+
* @property CI_DB_forge $dbforge
11+
* @property CI_DB_query_builder $db
12+
*/
13+
class Migration_create_table_api_access extends CI_Migration {
14+
15+
16+
public function up()
17+
{
18+
$table = config_item('rest_access_table');
19+
$fields = array(
20+
'id' => [
21+
'type' => 'INT(11)',
22+
'auto_increment' => TRUE,
23+
'unsigned' => TRUE,
24+
],
25+
'key' => [
26+
'type' => 'VARCHAR(' . config_item('rest_key_length') . ')',
27+
],
28+
'all_access' => [
29+
'type' => 'TINYINT(1)',
30+
'default' => 0,
31+
],
32+
'controller' => [
33+
'type' => 'VARCHAR(50)',
34+
],
35+
'date_created' => [
36+
'type' => 'DATETIME',
37+
'null' => TRUE,
38+
],
39+
'date_modified' => [
40+
'type' => 'TIMESTAMP',
41+
],
42+
);
43+
$this->dbforge->add_field($fields);
44+
$this->dbforge->add_key('id', TRUE);
45+
$this->dbforge->add_key('controller');
46+
$this->dbforge->create_table($table);
47+
$this->db->query(add_foreign_key($table, 'key',
48+
config_item('rest_keys_table') . '(' . config_item('rest_key_column') . ')', 'CASCADE', 'CASCADE'));
49+
}
50+
51+
52+
public function down()
53+
{
54+
$table = config_item('rest_access_table');
55+
if ($this->db->table_exists($table))
56+
{
57+
$this->db->query(drop_foreign_key($table, 'key'));
58+
$this->dbforge->drop_table($table);
59+
}
60+
}
61+
62+
}

0 commit comments

Comments
 (0)