Skip to content

Bug: DB Session migration generator creates invalid table structure - broken timestamp field syntax #9796

@zadro

Description

@zadro

PHP Version

8.4

CodeIgniter4 Version

4.6.3

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

fpm-fcgi

Database

MariaDB 10.11.14

What happened?

Running php spark make:migration --session generates a migration with invalid Database Forge array syntax.

The timestamp field uses a raw SQL string as an array key without a value:

'timestamp timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL'

This causes the migration to create an incorrectly structured ci_sessions table with timestamp as int(10) unsigned instead of TIMESTAMP type, leading to session corruption and random user logouts in Shield authentication.

Steps to Reproduce

  1. Run command: php spark make:migration --session
  2. Navigate to app/Database/Migrations/ and open the generated migration file
  3. Observe the malformed timestamp field syntax in the addField array
  4. Run: php spark migrate
  5. Check the ci_sessions table structure: SHOW CREATE TABLE ci_sessions;
  6. Confirm timestamp column is created as int(10) unsigned instead of TIMESTAMP
  7. Attempt to use database sessions - sessions will store corrupted timestamp values (e.g., 4294967295) causing Shield to invalidate DB sessions and log users out randomly

Expected Output

The migration template at system/Commands/Generators/Views/migration.tpl.php should use valid Database Forge array syntax for all fields. Specifically, this line:

'timestamp timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL',

Should be replaced with proper array syntax:

'timestamp' => ['type' => 'TIMESTAMP', 'null' => false],

Then after table creation in the template, add a raw query to set the default value since Forge doesn't support DEFAULT CURRENT_TIMESTAMP in field definitions:

$this->forge->createTable('', true);

$this->db->query('ALTER TABLE MODIFY timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL');

$this->db->query('ALTER TABLE ALTER COLUMN timestamp SET DEFAULT CURRENT_TIMESTAMP');

This will create the ci_sessions table with the correct structure:

  • id: varchar(128) NOT NULL, PRIMARY KEY
  • ip_address: varchar(45) NOT NULL
  • timestamp: TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  • data: BLOB NOT NULL
  • KEY on timestamp

The timestamp column will then store actual TIMESTAMP values that Shield can properly validate for session management.

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions