db->field_exists('slug', 'users')) { $fields = array( 'slug varchar(50) DEFAULT NULL' ); $this->dbforge->add_column('users', $fields); } // Step 2: Create public user-slug for each user if (!$this->load->is_loaded('encryption')) { $this->load->library('encryption'); } $fetch_result = $this->db->get($this->config->item('auth_table')); if ($fetch_result && $fetch_result->num_rows() > 0) { foreach ($fetch_result->result_array() as $user_row) { if ($user_row["slug"] === null) { $user_name = $user_row["user_name"]; $user_slug_base = md5($this->encryption->encrypt($user_name)); $url_slug = substr($user_slug_base, 0, USER_SLUG_LENGTH); // update the slug only in case the slug does not exist yet $user_id = $user_row["user_id"]; $this->db->where('user_id', $user_id); $this->db->update('users', array('slug' => $url_slug)); } } } // Step 3: Make user slug unique $this->db->query("ALTER TABLE `users` modify `slug` varchar(50) NOT NULL"); $this->db->query("ALTER TABLE `users` ADD CONSTRAINT `user_slug_unique` UNIQUE (`slug`)"); } public function down() { if ($this->db->field_exists('slug', 'users')) { $this->dbforge->drop_column('users', 'slug'); } } }