From 2ff2adb34002dd1a7c81dce7ba47326df44d84ca Mon Sep 17 00:00:00 2001 From: Yury Pikhtarev Date: Fri, 20 Jun 2025 02:21:01 +0400 Subject: [PATCH] refactor(database): standardize table engines to InnoDB for reliability - Updated UPGRADE_GUIDE.md to reflect the use of InnoDB for all tables, emphasizing data integrity and reliability. - Modified migration files to change table engines from MyISAM to InnoDB for various tracker and temporary tables. - Optimized session variable settings in cron jobs for InnoDB compatibility. - Ensured consistency across the schema by enforcing InnoDB usage in all relevant areas. --- UPGRADE_GUIDE.md | 38 ++++++------- library/includes/cron/cron_run.php | 8 ++- .../includes/cron/jobs/attach_maintenance.php | 2 +- migrations/20250619000001_initial_schema.php | 54 +++++++++---------- 4 files changed, 50 insertions(+), 52 deletions(-) diff --git a/UPGRADE_GUIDE.md b/UPGRADE_GUIDE.md index 79763c56b..7faaaf72e 100644 --- a/UPGRADE_GUIDE.md +++ b/UPGRADE_GUIDE.md @@ -30,9 +30,9 @@ TorrentPier now includes a modern database migration system using **Phinx** (fro ### Migration Architecture #### Engine Strategy -- **InnoDB**: Used for data integrity (users, posts, configuration, attachments) -- **MyISAM**: Used for performance-critical tracker tables (`bb_bt_tracker`, snapshots) -- **Expendable Tables**: Buffer and snapshot tables (`*_snap`, `buf_*`) can be dropped/recreated +- **InnoDB**: Used for all tables for maximum data integrity and reliability +- **ACID Compliance**: Full transaction support and crash recovery for all data +- **Row-Level Locking**: Better concurrency for high-traffic operations #### Directory Structure ``` @@ -101,19 +101,19 @@ class AddNewFeatureTable extends AbstractMigration 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci' ]); - + $table->addColumn('name', 'string', ['limit' => 100]) ->addColumn('created_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP']) ->addIndex('name') ->create(); } - + // Optional: explicit up/down methods for complex operations public function up() { // Complex data migration logic } - + public function down() { // Rollback logic @@ -123,22 +123,22 @@ class AddNewFeatureTable extends AbstractMigration #### Engine Guidelines ```php -// Use InnoDB for data integrity +// Use InnoDB for all tables for maximum reliability $table = $this->table('bb_user_posts', [ 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci' ]); -// Use MyISAM for high-performance tracker tables +// All tracker tables also use InnoDB for data integrity $table = $this->table('bb_bt_peer_stats', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci' ]); -// Expendable tables can be dropped without backup +// Buffer tables use InnoDB for consistency and reliability public function up() { $this->execute('DROP TABLE IF EXISTS buf_temp_data'); - // Recreate with new structure + // Recreate with new structure using InnoDB } ``` @@ -160,16 +160,16 @@ For complex data transformations, create external scripts: ```php // migrations/YYYYMMDDHHMMSS_complex_data_migration.php -class ComplexDataMigration extends AbstractMigration +class ComplexDataMigration extends AbstractMigration { public function up() { $this->output->writeln('Running complex data migration...'); - + // Call external script for complex operations $result = shell_exec('php ' . __DIR__ . '/../scripts/migrate_torrent_data.php'); $this->output->writeln($result); - + if (strpos($result, 'ERROR') !== false) { throw new Exception('Complex migration failed'); } @@ -263,7 +263,7 @@ php vendor/bin/phinx migrate The legacy `install/sql/mysql.sql` approach has been replaced by migrations: - ✅ **New installations**: Use migrations automatically -- ✅ **Development workflow**: Create migrations for all schema changes +- ✅ **Development workflow**: Create migrations for all schema changes - ✅ **Version control**: All schema changes tracked in Git - ❌ **Direct SQL imports**: No longer used for new installations @@ -303,7 +303,7 @@ php vendor/bin/phinx init # Mark the schema migration as applied without running it php vendor/bin/phinx migrate --fake --target=20250619000001 -# Mark the data seeding migration as applied without running it +# Mark the data seeding migration as applied without running it php vendor/bin/phinx migrate --fake --target=20250619000002 ``` @@ -331,8 +331,8 @@ CREATE TABLE IF NOT EXISTS bb_migrations ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- Mark initial migrations as applied -INSERT INTO bb_migrations (version, migration_name, start_time, end_time, breakpoint) -VALUES +INSERT INTO bb_migrations (version, migration_name, start_time, end_time, breakpoint) +VALUES ('20250619000001', 'InitialSchema', NOW(), NOW(), 0), ('20250619000002', 'SeedInitialData', NOW(), NOW(), 0); ``` @@ -345,7 +345,7 @@ After setup, your existing installation will work exactly like a fresh installat # Create new migrations php vendor/bin/phinx create AddNewFeature -# Run new migrations +# Run new migrations php vendor/bin/phinx migrate # Check status diff --git a/library/includes/cron/cron_run.php b/library/includes/cron/cron_run.php index debeb7859..4b30dd61c 100644 --- a/library/includes/cron/cron_run.php +++ b/library/includes/cron/cron_run.php @@ -13,11 +13,10 @@ if (!defined('BB_ROOT')) { define('IN_CRON', true); -// Set SESSION vars +// Set SESSION vars (optimized for InnoDB) DB()->query(" SET SESSION - myisam_sort_buffer_size = 16*1024*1024 - , bulk_insert_buffer_size = 8*1024*1024 + bulk_insert_buffer_size = 8*1024*1024 , join_buffer_size = 4*1024*1024 , read_buffer_size = 4*1024*1024 , read_rnd_buffer_size = 8*1024*1024 @@ -29,8 +28,7 @@ DB()->query(" // Restore vars at shutdown DB()->add_shutdown_query(" SET SESSION - myisam_sort_buffer_size = DEFAULT - , bulk_insert_buffer_size = DEFAULT + bulk_insert_buffer_size = DEFAULT , join_buffer_size = DEFAULT , read_buffer_size = DEFAULT , read_rnd_buffer_size = DEFAULT diff --git a/library/includes/cron/jobs/attach_maintenance.php b/library/includes/cron/jobs/attach_maintenance.php index 84987405e..aa5ee6101 100644 --- a/library/includes/cron/jobs/attach_maintenance.php +++ b/library/includes/cron/jobs/attach_maintenance.php @@ -28,7 +28,7 @@ DB()->query(" CREATE TEMPORARY TABLE $tmp_attach_tbl ( physical_filename VARCHAR(255) NOT NULL default '', KEY physical_filename (physical_filename(20)) - ) ENGINE = MyISAM DEFAULT CHARSET = utf8 + ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 "); DB()->add_shutdown_query("DROP TEMPORARY TABLE IF EXISTS $tmp_attach_tbl"); diff --git a/migrations/20250619000001_initial_schema.php b/migrations/20250619000001_initial_schema.php index c91424713..3f77ab41e 100644 --- a/migrations/20250619000001_initial_schema.php +++ b/migrations/20250619000001_initial_schema.php @@ -16,7 +16,7 @@ class InitialSchema extends AbstractMigration // Core forum tables - InnoDB for data integrity $this->createForumTables(); - // BitTorrent tracker tables - MyISAM for performance + // BitTorrent tracker tables - InnoDB for reliability $this->createTrackerTables(); // Configuration and system tables - InnoDB @@ -28,7 +28,7 @@ class InitialSchema extends AbstractMigration // User management - InnoDB $this->createUserTables(); - // Cache and temporary tables - MyISAM (expendable) + // Cache and temporary tables - InnoDB $this->createCacheTables(); } @@ -166,9 +166,9 @@ class InitialSchema extends AbstractMigration private function createTrackerTables() { - // bb_bt_torrents - Core torrent registry (MyISAM for performance) + // bb_bt_torrents - Core torrent registry (InnoDB for reliability) $table = $this->table('bb_bt_torrents', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => 'topic_id' @@ -200,9 +200,9 @@ class InitialSchema extends AbstractMigration ->addIndex('poster_id') ->create(); - // bb_bt_tracker - Active peer tracking (MyISAM for high-write performance) + // bb_bt_tracker - Active peer tracking (InnoDB for reliability) $table = $this->table('bb_bt_tracker', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => 'peer_hash' @@ -258,15 +258,15 @@ class InitialSchema extends AbstractMigration ->addIndex('auth_key', ['unique' => true]) ->create(); - // Snapshot tables - expendable, use MyISAM + // Snapshot tables $this->createSnapshotTables(); } private function createSnapshotTables() { - // bb_bt_tracker_snap - Tracker snapshot (expendable) + // bb_bt_tracker_snap - Tracker snapshot $table = $this->table('bb_bt_tracker_snap', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => 'topic_id' @@ -279,9 +279,9 @@ class InitialSchema extends AbstractMigration ->addColumn('completed', 'integer', ['limit' => 10, 'default' => 0]) ->create(); - // bb_bt_dlstatus_snap - Download status snapshot (expendable) + // bb_bt_dlstatus_snap - Download status snapshot $table = $this->table('bb_bt_dlstatus_snap', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false ]); @@ -291,9 +291,9 @@ class InitialSchema extends AbstractMigration ->addIndex('topic_id') ->create(); - // buf_topic_view - Topic view buffer (expendable) + // buf_topic_view - Topic view buffer $table = $this->table('buf_topic_view', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => 'topic_id' @@ -302,9 +302,9 @@ class InitialSchema extends AbstractMigration ->addColumn('topic_views', 'integer', ['limit' => 8, 'signed' => false, 'default' => 0, 'null' => false]) ->create(); - // buf_last_seeder - Last seeder buffer (expendable) + // buf_last_seeder - Last seeder buffer $table = $this->table('buf_last_seeder', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => 'topic_id' @@ -541,7 +541,7 @@ class InitialSchema extends AbstractMigration private function createCacheTables() { - // Additional tracker-related tables that are more expendable + // Additional tracker-related tables $tables = [ 'bb_bt_dlstatus', 'bb_bt_torstat', @@ -552,7 +552,7 @@ class InitialSchema extends AbstractMigration 'bb_bt_user_settings' ]; - // Create these tables with appropriate engines + // Create these tables with InnoDB engine $this->createRemainingTrackerTables(); // Create remaining system tables @@ -590,7 +590,7 @@ class InitialSchema extends AbstractMigration // bb_bt_tor_dl_stat - Torrent download statistics $table = $this->table('bb_bt_tor_dl_stat', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => ['topic_id', 'user_id'] @@ -605,7 +605,7 @@ class InitialSchema extends AbstractMigration // bb_bt_last_torstat - Last torrent statistics $table = $this->table('bb_bt_last_torstat', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => ['topic_id', 'user_id'] @@ -623,7 +623,7 @@ class InitialSchema extends AbstractMigration // bb_bt_last_userstat - Last user statistics $table = $this->table('bb_bt_last_userstat', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => 'user_id' @@ -639,7 +639,7 @@ class InitialSchema extends AbstractMigration // bb_bt_torhelp - Torrent help system $table = $this->table('bb_bt_torhelp', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => 'user_id' @@ -717,7 +717,7 @@ class InitialSchema extends AbstractMigration { // bb_posts_search - Search index for posts $table = $this->table('bb_posts_search', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => 'post_id' @@ -731,7 +731,7 @@ class InitialSchema extends AbstractMigration // bb_posts_html - Cached HTML posts $table = $this->table('bb_posts_html', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => 'post_id' @@ -743,7 +743,7 @@ class InitialSchema extends AbstractMigration // bb_search_results - Search result cache $table = $this->table('bb_search_results', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => ['session_id', 'search_type'] @@ -758,7 +758,7 @@ class InitialSchema extends AbstractMigration // bb_search_rebuild - Search rebuild status $table = $this->table('bb_search_rebuild', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => 'rebuild_session_id' @@ -835,7 +835,7 @@ class InitialSchema extends AbstractMigration { // bb_log - Action logging $table = $this->table('bb_log', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false ]); @@ -978,7 +978,7 @@ class InitialSchema extends AbstractMigration // bb_auth_access_snap $table = $this->table('bb_auth_access_snap', [ - 'engine' => 'MyISAM', + 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'id' => false, 'primary_key' => ['user_id', 'forum_id']