From 6a2b654106ab8d64b8251b37bce6e3c2aca7a56b Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Tue, 16 May 2023 10:22:17 +0300 Subject: [PATCH 1/4] feat: Add InnoDB engine to migration create table methods --- .../2020-12-28-223112_create_auth_tables.php | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index 80691cd9b..4965ad7c5 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -15,13 +15,25 @@ class CreateAuthTables extends Migration */ private array $tables; + /** + * @var array|string[] + */ + private array $attributes; + + /** + * @var false + */ + private bool $ifNotExists; + public function __construct(?Forge $forge = null) { parent::__construct($forge); /** @var Auth $authConfig */ - $authConfig = config('Auth'); - $this->tables = $authConfig->tables; + $authConfig = config('Auth'); + $this->tables = $authConfig->tables; + $this->ifNotExists = false; + $this->attributes = ['ENGINE' => 'InnoDB']; } public function up(): void @@ -40,7 +52,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addUniqueKey('username'); - $this->forge->createTable($this->tables['users']); + $this->forge->createTable($this->tables['users'], $this->ifNotExists, $this->attributes); /* * Auth Identities Table @@ -64,7 +76,7 @@ public function up(): void $this->forge->addUniqueKey(['type', 'secret']); $this->forge->addKey('user_id'); $this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE'); - $this->forge->createTable($this->tables['identities']); + $this->forge->createTable($this->tables['identities'], $this->ifNotExists, $this->attributes); /** * Auth Login Attempts Table @@ -85,7 +97,7 @@ public function up(): void $this->forge->addKey(['id_type', 'identifier']); $this->forge->addKey('user_id'); // NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits - $this->forge->createTable($this->tables['logins']); + $this->forge->createTable($this->tables['logins'], $this->ifNotExists, $this->attributes); /* * Auth Token Login Attempts Table @@ -105,7 +117,7 @@ public function up(): void $this->forge->addKey(['id_type', 'identifier']); $this->forge->addKey('user_id'); // NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits - $this->forge->createTable($this->tables['token_logins']); + $this->forge->createTable($this->tables['token_logins'], $this->ifNotExists, $this->attributes); /* * Auth Remember Tokens (remember-me) Table @@ -123,7 +135,7 @@ public function up(): void $this->forge->addPrimaryKey('id'); $this->forge->addUniqueKey('selector'); $this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE'); - $this->forge->createTable($this->tables['remember_tokens']); + $this->forge->createTable($this->tables['remember_tokens'], $this->ifNotExists, $this->attributes); // Groups Users Table $this->forge->addField([ @@ -134,7 +146,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE'); - $this->forge->createTable($this->tables['groups_users']); + $this->forge->createTable($this->tables['groups_users'], $this->ifNotExists, $this->attributes); // Users Permissions Table $this->forge->addField([ @@ -145,7 +157,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE'); - $this->forge->createTable($this->tables['permissions_users']); + $this->forge->createTable($this->tables['permissions_users'], $this->ifNotExists, $this->attributes); } // -------------------------------------------------------------------- From 96baffe4b30e22ec631cc1e37a1a49a82d94c5cd Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Tue, 16 May 2023 10:29:48 +0300 Subject: [PATCH 2/4] apply rector suggestions --- .../Migrations/2020-12-28-223112_create_auth_tables.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index 4965ad7c5..b3ed443af 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -15,14 +15,7 @@ class CreateAuthTables extends Migration */ private array $tables; - /** - * @var array|string[] - */ private array $attributes; - - /** - * @var false - */ private bool $ifNotExists; public function __construct(?Forge $forge = null) From 04238b4178d3b6949c2c3b6aaf3a3f9758d8bcbc Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Tue, 16 May 2023 15:55:46 +0300 Subject: [PATCH 3/4] add check for MySQLi platform --- .../Migrations/2020-12-28-223112_create_auth_tables.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index b3ed443af..5c5626a87 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -26,7 +26,7 @@ public function __construct(?Forge $forge = null) $authConfig = config('Auth'); $this->tables = $authConfig->tables; $this->ifNotExists = false; - $this->attributes = ['ENGINE' => 'InnoDB']; + $this->attributes = ($this->db->getPlatform() === 'MySQLi') ? ['ENGINE' => 'InnoDB'] : []; } public function up(): void From 3fcfcca5fdaae14b0a0856778bfed7d52ad81f29 Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Wed, 17 May 2023 12:21:13 +0300 Subject: [PATCH 4/4] move create table to private method --- .../2020-12-28-223112_create_auth_tables.php | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index 5c5626a87..deb4570ae 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -16,17 +16,15 @@ class CreateAuthTables extends Migration private array $tables; private array $attributes; - private bool $ifNotExists; public function __construct(?Forge $forge = null) { parent::__construct($forge); /** @var Auth $authConfig */ - $authConfig = config('Auth'); - $this->tables = $authConfig->tables; - $this->ifNotExists = false; - $this->attributes = ($this->db->getPlatform() === 'MySQLi') ? ['ENGINE' => 'InnoDB'] : []; + $authConfig = config('Auth'); + $this->tables = $authConfig->tables; + $this->attributes = ($this->db->getPlatform() === 'MySQLi') ? ['ENGINE' => 'InnoDB'] : []; } public function up(): void @@ -45,7 +43,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addUniqueKey('username'); - $this->forge->createTable($this->tables['users'], $this->ifNotExists, $this->attributes); + $this->createTable($this->tables['users']); /* * Auth Identities Table @@ -69,7 +67,7 @@ public function up(): void $this->forge->addUniqueKey(['type', 'secret']); $this->forge->addKey('user_id'); $this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE'); - $this->forge->createTable($this->tables['identities'], $this->ifNotExists, $this->attributes); + $this->createTable($this->tables['identities']); /** * Auth Login Attempts Table @@ -90,7 +88,7 @@ public function up(): void $this->forge->addKey(['id_type', 'identifier']); $this->forge->addKey('user_id'); // NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits - $this->forge->createTable($this->tables['logins'], $this->ifNotExists, $this->attributes); + $this->createTable($this->tables['logins']); /* * Auth Token Login Attempts Table @@ -110,7 +108,7 @@ public function up(): void $this->forge->addKey(['id_type', 'identifier']); $this->forge->addKey('user_id'); // NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits - $this->forge->createTable($this->tables['token_logins'], $this->ifNotExists, $this->attributes); + $this->createTable($this->tables['token_logins']); /* * Auth Remember Tokens (remember-me) Table @@ -128,7 +126,7 @@ public function up(): void $this->forge->addPrimaryKey('id'); $this->forge->addUniqueKey('selector'); $this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE'); - $this->forge->createTable($this->tables['remember_tokens'], $this->ifNotExists, $this->attributes); + $this->createTable($this->tables['remember_tokens']); // Groups Users Table $this->forge->addField([ @@ -139,7 +137,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE'); - $this->forge->createTable($this->tables['groups_users'], $this->ifNotExists, $this->attributes); + $this->createTable($this->tables['groups_users']); // Users Permissions Table $this->forge->addField([ @@ -150,7 +148,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE'); - $this->forge->createTable($this->tables['permissions_users'], $this->ifNotExists, $this->attributes); + $this->createTable($this->tables['permissions_users']); } // -------------------------------------------------------------------- @@ -169,4 +167,9 @@ public function down(): void $this->db->enableForeignKeyChecks(); } + + private function createTable(string $tableName): void + { + $this->forge->createTable($tableName, false, $this->attributes); + } }