From 49f19de3ba7bef2d95e00c4e78c98aaa90b40dc3 Mon Sep 17 00:00:00 2001 From: HB9HIL Date: Fri, 6 Dec 2024 17:52:58 +0100 Subject: [PATCH] feat[clubstations]: New DB structure --- application/config/migration.php | 2 +- application/migrations/230_clubstations.php | 65 +++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 application/migrations/230_clubstations.php diff --git a/application/config/migration.php b/application/config/migration.php index f39a5837f..0e1107dd7 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -22,7 +22,7 @@ $config['migration_enabled'] = TRUE; | */ -$config['migration_version'] = 229; +$config['migration_version'] = 230; /* |-------------------------------------------------------------------------- diff --git a/application/migrations/230_clubstations.php b/application/migrations/230_clubstations.php new file mode 100644 index 000000000..6b7495489 --- /dev/null +++ b/application/migrations/230_clubstations.php @@ -0,0 +1,65 @@ +db->query("SHOW COLUMNS FROM users LIKE 'clubstation'")->num_rows() > 0; + if (!$col_check) { + $sql = "ALTER TABLE users ADD COLUMN clubstation TINYINT(1) DEFAULT 0"; + try { + $this->db->query($sql); + } catch (Exception $e) { + log_message('error', 'Mig 230 - Error adding column clubstation: ' . $e->getMessage()); + } + } else { + log_message('info', 'Mig 230 - Column clubstation already exists, skipping ALTER TABLE.'); + } + + // Create a new table for the club permissions + if (!$this->db->table_exists('club_permissions')) { + $this->dbforge->add_field(array( + 'id' => array( + 'type' => 'INT', + 'constraint' => 6, + 'unsigned' => TRUE, + 'auto_increment' => TRUE, + 'null' => FALSE + ), + 'club_id' => array( + 'type' => 'INT', + 'constraint' => '6', + 'unsigned' => TRUE, + 'null' => FALSE, + ), + 'user_id' => array( + 'type' => 'INT', + 'constraint' => '6', + 'unsigned' => TRUE, + 'null' => FALSE, + ), + 'p_level' => array( + 'type' => 'INT', + 'constraint' => '6', + 'unsigned' => TRUE, + 'null' => FALSE, + ), + )); + $this->dbforge->add_key('id', TRUE); + $this->dbforge->create_table('club_permissions'); + + // Add a foreign key for the user_id + $sql = "ALTER TABLE club_permissions ADD CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"; + try { + $this->db->query($sql); + } catch (Exception $e) { + log_message('error', 'Mig 230 - Error adding foreign key fk_user_id: ' . $e->getMessage()); + } + } + } + + public function down() { + // we don't want to loose data in case of a down migration, so we don't drop the column here + } +}