Welcome Guest, Not a member yet? Register   Sign In
MIgrations file for ci_sessions - issue creating a relabelled key as required by CI session library
#1

[eluser]Ollie Rattue[/eluser]
Just started playing with the migrations library. First table to create is the ci_sessions table as defined in the user guide at http://ellislab.com/codeigniter/user-gui...sions.html as follows:

Code:
CREATE TABLE IF NOT EXISTS  `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(16) DEFAULT '0' NOT NULL,
user_agent varchar(120) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id),
KEY `last_activity_idx` (`last_activity`)
);

So I write my definition in application/migrations/001_add_ci_sessions:

Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Add_ci_session extends CI_Migration {

// --------------------------------------------------------------------

public function up()
{
  echo "\nCreating ci_sessions table...";
  
  $this->dbforge->add_field(array(
   'session_id' => array(
    'type' => 'VARCHAR',
    'constraint' => 40,
    'default' => 0,
    'null' => FALSE
   ),
   'ip_address' => array(
    'type' => 'VARCHAR',
    'constraint' => 16,
    'default' => 0
   ),
   'user_agent' => array(
    'type' => 'VARCHAR',
    'constraint' => 120
   ),
   'last_activity' => array(
    'type' => 'INT',
    'constraint' => 10,
    'unsigned' => TRUE,
    'default' => 0
   ),
   'user_data' => array(
    'type' => 'text'
   ),
  ));
  
  $this->dbforge->add_key('session_id', TRUE); // primary key
  $this->dbforge->add_key('last_activity');
  $this->dbforge->create_table('ci_sessions');
  
  echo "\nci_sessions table created";
  echo "\n";
}

// --------------------------------------------------------------------

public function down()
{
  $this->dbforge->drop_table('ci_session');
}

// --------------------------------------------------------------------
}

The problem is this line:

Code:
KEY `last_activity_idx` (`last_activity`)

The field has been relabelled `last_activity_idx`. It doesn't look like it is possible to do this using the db_forge library - see http://=http://ellislab.com/codeigniter/...ml#add_key

So how do I do this? Surely the migrations library is capable of migration a CI predefined schema Smile
#2

[eluser]Unknown[/eluser]
Hate to revive an old thread but here is a solution. Just add this at the end of your up function after you call create_table:

Code:
$this->db->query('ALTER TABLE `ci_sessions` ADD KEY `last_activity_idx` (`last_activity`)');

Ghetto, but it works.




Theme © iAndrew 2016 - Forum software by © MyBB