CodeIgniter - ci_sessions migrations CodeIgniter - ci_sessions migrations codeigniter codeigniter

CodeIgniter - ci_sessions migrations


Please read the documentation here:

CodeIgniter Migrations

In essence, you will want to use the CodeIgniter dbforge class to create the table. Using your code above:

    $this->dbforge->add_field(array(        'session_id' => array(            'type' => 'VARCHAR',            'constraint' => '40'        ),        'ip_address' => array(            'type' => 'VARCHAR',            'constraint' => '16'        ),        'user_agent' => array(            'type' => 'VARCHAR',            'constraint' => '150'        ),        'last_activity' => array(            'type' => 'INT',            'constraint' => '10'        ),        'user_data' => array(            'type' => 'TEXT'        )    ));    $this->dbforge->add_key('session_id', TRUE);    $this->dbforge->create_table('ci_sessions');

Documentation on dbforge class can be found here:

ellislab.com/codeigniter/user-guide/database/forge.html

Note: I don't recommend messing with the CI Sessions table, though.


You can also simply do this if you've a block of large SQL query you know is "safe" and you want to save time (in my opinion it's a judgement call based on the complexity of the table and the chance of making an error when translating the raw SQL into arrays - after all in migrations everything is hardcoded and you're not worrying about the chances of SQL injection etc.)

    $sql = "CREATE TABLE IF NOT EXISTS  `ci_sessions` (    session_id varchar(40) DEFAULT '0' NOT NULL,    ip_address varchar(45) 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`))";    $this->db->query($sql);

However I don't think I'd recommend loading the sessions table as a migration, because if $config['sess_use_database'] in config/config.php is true, when you go to your migration URL it'll fail, as Codeigniter will first try to create a session entry in the database for your browser and the database table doesn't yet exist..

A Database Error OccurredError Number: 1146Table 'characterhub.ci_sessions' doesn't exist

So for it to work, you or whomever is performing the migration has to set sess_use_database to false first, then run the migration, then change it back to true again.