chore: initial commit
This commit is contained in:
10
api/migrations/101_version_101.php
Normal file
10
api/migrations/101_version_101.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_101 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
}
|
||||
}
|
||||
10
api/migrations/102_version_102.php
Normal file
10
api/migrations/102_version_102.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_102 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
}
|
||||
}
|
||||
10
api/migrations/103_version_103.php
Normal file
10
api/migrations/103_version_103.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_103 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
}
|
||||
}
|
||||
11
api/migrations/200_version_200.php
Normal file
11
api/migrations/200_version_200.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_200 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
27
api/migrations/201_version_201.php
Normal file
27
api/migrations/201_version_201.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_201 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
$this->ci->dbforge->add_column('user_api', array('permission_enable' => array('type' => 'TINYINT', 'default' => 0)));
|
||||
|
||||
$this->ci->dbforge->add_field([
|
||||
'api_id' => [
|
||||
'type' => 'INT',
|
||||
'constraint' => 11,
|
||||
],
|
||||
'feature' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 50,
|
||||
],
|
||||
'capability' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 50,
|
||||
],
|
||||
]);
|
||||
$this->ci->dbforge->create_table('user_api_permissions', true);
|
||||
}
|
||||
}
|
||||
10
api/migrations/202_version_202.php
Normal file
10
api/migrations/202_version_202.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_202 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
}
|
||||
}
|
||||
12
api/migrations/203_version_203.php
Normal file
12
api/migrations/203_version_203.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_203 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
12
api/migrations/204_version_204.php
Normal file
12
api/migrations/204_version_204.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_204 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
12
api/migrations/205_version_205.php
Normal file
12
api/migrations/205_version_205.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_205 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
12
api/migrations/206_version_206.php
Normal file
12
api/migrations/206_version_206.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_206 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
12
api/migrations/207_version_207.php
Normal file
12
api/migrations/207_version_207.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_207 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
12
api/migrations/208_version_208.php
Normal file
12
api/migrations/208_version_208.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_208 extends App_module_migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
141
api/migrations/209_version_209.php
Normal file
141
api/migrations/209_version_209.php
Normal file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_209 extends App_module_migration
|
||||
{
|
||||
/** @var CI_DB_query_builder */
|
||||
protected $db;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Properly initialize the database once the migration is constructed
|
||||
$CI = &get_instance();
|
||||
$CI->load->database();
|
||||
$this->db = $CI->db;
|
||||
}
|
||||
public function up()
|
||||
{
|
||||
// Create API quotas table with foreign key relationship
|
||||
if (!$this->db->table_exists(db_prefix() . 'user_api_quotas')) {
|
||||
$this->db->query("
|
||||
CREATE TABLE `" . db_prefix() . "user_api_quotas` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`user_api_id` int(11) NOT NULL,
|
||||
`api_key` varchar(255) NOT NULL,
|
||||
`request_limit` int(11) NOT NULL DEFAULT 1000,
|
||||
`time_window` int(11) NOT NULL DEFAULT 3600,
|
||||
`burst_limit` int(11) NOT NULL DEFAULT 0,
|
||||
`active` tinyint(1) NOT NULL DEFAULT 1,
|
||||
`created_at` datetime NOT NULL,
|
||||
`updated_at` datetime NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `user_api_id` (`user_api_id`),
|
||||
KEY `api_key` (`api_key`),
|
||||
KEY `active` (`active`),
|
||||
CONSTRAINT `fk_user_api_quotas_user_api`
|
||||
FOREIGN KEY (`user_api_id`)
|
||||
REFERENCES `" . db_prefix() . "user_api` (`id`)
|
||||
ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
");
|
||||
} else {
|
||||
// Check if user_api_id column exists, if not add it
|
||||
if (!$this->db->field_exists('user_api_id', db_prefix() . 'user_api_quotas')) {
|
||||
$this->db->query("ALTER TABLE `" . db_prefix() . "user_api_quotas`
|
||||
ADD `user_api_id` int(11) NOT NULL AFTER `id`");
|
||||
|
||||
// Update existing records to link with user_api table
|
||||
$this->db->query("
|
||||
UPDATE `" . db_prefix() . "user_api_quotas` uaq
|
||||
INNER JOIN `" . db_prefix() . "user_api` ua ON uaq.api_key = ua.token
|
||||
SET uaq.user_api_id = ua.id
|
||||
");
|
||||
|
||||
// Add foreign key constraint
|
||||
$this->db->query("
|
||||
ALTER TABLE `" . db_prefix() . "user_api_quotas`
|
||||
ADD CONSTRAINT `fk_user_api_quotas_user_api`
|
||||
FOREIGN KEY (`user_api_id`)
|
||||
REFERENCES `" . db_prefix() . "user_api` (`id`)
|
||||
ON DELETE CASCADE ON UPDATE CASCADE
|
||||
");
|
||||
}
|
||||
}
|
||||
|
||||
// Create API usage logs table with foreign key relationship
|
||||
if (!$this->db->table_exists(db_prefix() . 'api_usage_logs')) {
|
||||
$this->db->query("
|
||||
CREATE TABLE `" . db_prefix() . "api_usage_logs` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`user_api_id` int(11) NOT NULL,
|
||||
`api_key` varchar(255) NOT NULL,
|
||||
`endpoint` varchar(255) NOT NULL,
|
||||
`response_code` int(11) NOT NULL,
|
||||
`response_time` decimal(10,4) NOT NULL DEFAULT 0.0000,
|
||||
`timestamp` int(11) NOT NULL,
|
||||
`ip_address` varchar(45) NOT NULL,
|
||||
`user_agent` text,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `user_api_id` (`user_api_id`),
|
||||
KEY `api_key` (`api_key`),
|
||||
KEY `endpoint` (`endpoint`),
|
||||
KEY `timestamp` (`timestamp`),
|
||||
KEY `response_code` (`response_code`),
|
||||
CONSTRAINT `fk_api_usage_logs_user_api`
|
||||
FOREIGN KEY (`user_api_id`)
|
||||
REFERENCES `" . db_prefix() . "user_api` (`id`)
|
||||
ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
");
|
||||
} else {
|
||||
// Check if user_api_id column exists, if not add it
|
||||
if (!$this->db->field_exists('user_api_id', db_prefix() . 'api_usage_logs')) {
|
||||
$this->db->query("ALTER TABLE `" . db_prefix() . "api_usage_logs`
|
||||
ADD `user_api_id` int(11) NOT NULL AFTER `id`");
|
||||
|
||||
// Update existing records to link with user_api table
|
||||
$this->db->query("
|
||||
UPDATE `" . db_prefix() . "api_usage_logs` aul
|
||||
INNER JOIN `" . db_prefix() . "user_api` ua ON aul.api_key = ua.token
|
||||
SET aul.user_api_id = ua.id
|
||||
");
|
||||
|
||||
// Add foreign key constraint
|
||||
$this->db->query("
|
||||
ALTER TABLE `" . db_prefix() . "api_usage_logs`
|
||||
ADD CONSTRAINT `fk_api_usage_logs_user_api`
|
||||
FOREIGN KEY (`user_api_id`)
|
||||
REFERENCES `" . db_prefix() . "user_api` (`id`)
|
||||
ON DELETE CASCADE ON UPDATE CASCADE
|
||||
");
|
||||
}
|
||||
}
|
||||
|
||||
// Insert default quota settings for existing API users if they don't exist
|
||||
$this->db->query("
|
||||
INSERT IGNORE INTO `" . db_prefix() . "user_api_quotas`
|
||||
(`user_api_id`, `api_key`, `request_limit`, `time_window`, `burst_limit`, `active`, `created_at`, `updated_at`)
|
||||
SELECT
|
||||
id as user_api_id,
|
||||
token as api_key,
|
||||
1000 as request_limit,
|
||||
3600 as time_window,
|
||||
100 as burst_limit,
|
||||
1 as active,
|
||||
NOW() as created_at,
|
||||
NOW() as updated_at
|
||||
FROM `" . db_prefix() . "user_api`
|
||||
WHERE id NOT IN (SELECT user_api_id FROM `" . db_prefix() . "user_api_quotas`)
|
||||
");
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
// Drop tables
|
||||
$this->db->query("DROP TABLE IF EXISTS `" . db_prefix() . "api_usage_logs`");
|
||||
$this->db->query("DROP TABLE IF EXISTS `" . db_prefix() . "user_api_quotas`");
|
||||
}
|
||||
}
|
||||
115
api/migrations/210_version_210.php
Normal file
115
api/migrations/210_version_210.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Migration_Version_210 extends App_module_migration
|
||||
{
|
||||
/** @var CI_DB_query_builder */
|
||||
protected $db;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Properly initialize the database once the migration is constructed
|
||||
$CI = &get_instance();
|
||||
$CI->load->database();
|
||||
$this->db = $CI->db;
|
||||
}
|
||||
public function up()
|
||||
{
|
||||
// Add quota fields to user_api table
|
||||
if (!$this->db->field_exists('request_limit', db_prefix() . 'user_api')) {
|
||||
$this->db->query("
|
||||
ALTER TABLE `" . db_prefix() . "user_api`
|
||||
ADD `request_limit` int(11) NOT NULL DEFAULT 1000 AFTER `permission_enable`,
|
||||
ADD `time_window` int(11) NOT NULL DEFAULT 3600 AFTER `request_limit`,
|
||||
ADD `burst_limit` int(11) NOT NULL DEFAULT 0 AFTER `time_window`,
|
||||
ADD `quota_active` tinyint(1) NOT NULL DEFAULT 1 AFTER `burst_limit`,
|
||||
ADD `quota_created_at` datetime NULL AFTER `quota_active`,
|
||||
ADD `quota_updated_at` datetime NULL AFTER `quota_created_at`
|
||||
");
|
||||
}
|
||||
|
||||
// Migrate existing quota data to user_api table
|
||||
if ($this->db->table_exists(db_prefix() . 'user_api_quotas')) {
|
||||
$this->db->query("
|
||||
UPDATE `" . db_prefix() . "user_api` ua
|
||||
INNER JOIN `" . db_prefix() . "user_api_quotas` uaq ON ua.token = uaq.api_key
|
||||
SET
|
||||
ua.request_limit = uaq.request_limit,
|
||||
ua.time_window = uaq.time_window,
|
||||
ua.burst_limit = uaq.burst_limit,
|
||||
ua.quota_active = uaq.active,
|
||||
ua.quota_created_at = uaq.created_at,
|
||||
ua.quota_updated_at = uaq.updated_at
|
||||
");
|
||||
}
|
||||
|
||||
// Set default values for users without quota settings
|
||||
$this->db->query("
|
||||
UPDATE `" . db_prefix() . "user_api`
|
||||
SET
|
||||
request_limit = 1000,
|
||||
time_window = 3600,
|
||||
burst_limit = 100,
|
||||
quota_active = 1,
|
||||
quota_created_at = NOW(),
|
||||
quota_updated_at = NOW()
|
||||
WHERE request_limit IS NULL OR request_limit = 0
|
||||
");
|
||||
|
||||
// Drop the separate user_api_quotas table (no longer needed)
|
||||
$this->db->query("DROP TABLE IF EXISTS `" . db_prefix() . "user_api_quotas`");
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
// Recreate user_api_quotas table
|
||||
$this->db->query("
|
||||
CREATE TABLE `" . db_prefix() . "user_api_quotas` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`user_api_id` int(11) NOT NULL,
|
||||
`api_key` varchar(255) NOT NULL,
|
||||
`request_limit` int(11) NOT NULL DEFAULT 1000,
|
||||
`time_window` int(11) NOT NULL DEFAULT 3600,
|
||||
`burst_limit` int(11) NOT NULL DEFAULT 0,
|
||||
`active` tinyint(1) NOT NULL DEFAULT 1,
|
||||
`created_at` datetime NOT NULL,
|
||||
`updated_at` datetime NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `user_api_id` (`user_api_id`),
|
||||
KEY `api_key` (`api_key`),
|
||||
KEY `active` (`active`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
");
|
||||
|
||||
// Migrate data back to separate table
|
||||
$this->db->query("
|
||||
INSERT INTO `" . db_prefix() . "user_api_quotas`
|
||||
(`user_api_id`, `api_key`, `request_limit`, `time_window`, `burst_limit`, `active`, `created_at`, `updated_at`)
|
||||
SELECT
|
||||
id as user_api_id,
|
||||
token as api_key,
|
||||
request_limit,
|
||||
time_window,
|
||||
burst_limit,
|
||||
quota_active as active,
|
||||
quota_created_at as created_at,
|
||||
quota_updated_at as updated_at
|
||||
FROM `" . db_prefix() . "user_api`
|
||||
WHERE request_limit IS NOT NULL
|
||||
");
|
||||
|
||||
// Remove quota fields from user_api table
|
||||
$this->db->query("
|
||||
ALTER TABLE `" . db_prefix() . "user_api`
|
||||
DROP COLUMN `request_limit`,
|
||||
DROP COLUMN `time_window`,
|
||||
DROP COLUMN `burst_limit`,
|
||||
DROP COLUMN `quota_active`,
|
||||
DROP COLUMN `quota_created_at`,
|
||||
DROP COLUMN `quota_updated_at`
|
||||
");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user