Skip to content

Bug: php spark migrate behavior issues #10275

@sxpert

Description

@sxpert

PHP Version

8.5

CodeIgniter4 Version

4.7.3

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

macOS

Which server did you use?

cli-server (PHP built-in webserver)

Environment

development

Database

postgresql 16

What happened?

start with an empty database

testdb=# \d
Did not find any relations.
user@machine backend % php spark migrate:status

CodeIgniter v4.7.3 Command Line Tool - Server Time: 2026-06-04 16:39:32 UTC+02:00

+-----------+-------------------+-----------------------------------+-------+-------------+-------+
| Namespace | Version           | Filename                          | Group | Migrated On | Batch |
+-----------+-------------------+-----------------------------------+-------+-------------+-------+
| App       | 2026-06-04-113129 | CreateInfTypesGroupesTable        | ---   | ---         | ---   |
| App       | 2026-06-04-133011 | CreateInfAccesGroupesTable        | ---   | ---         | ---   |
| App       | 2026-06-04-133011 | CreateInfAccesTable               | ---   | ---         | ---   |
| App       | 2026-06-04-133011 | CreateInfAccesUnitesTable         | ---   | ---         | ---   |
| App       | 2026-06-04-133011 | CreateInfCriteresGroupesTable     | ---   | ---         | ---   |
| App       | 2026-06-04-133011 | CreateInfEtatsTable               | ---   | ---         | ---   |
| App       | 2026-06-04-133011 | CreateInfGestionnairesTable       | ---   | ---         | ---   |
| App       | 2026-06-04-133011 | CreateInfGroupesTable             | ---   | ---         | ---   |
| App       | 2026-06-04-133011 | CreateInfRolesTable               | ---   | ---         | ---   |
| App       | 2026-06-04-133011 | CreateInfUnitesGroupesTable       | ---   | ---         | ---   |
| App       | 2026-06-04-133011 | CreateInfUtilisateursGroupesTable | ---   | ---         | ---   |
| App       | 2026-06-04-133012 | CreateInfUtilisateursTable        | ---   | ---         | ---   |
| App       | 2026-06-04-133012 | CreateRefCriteresTable            | ---   | ---         | ---   |
| App       | 2026-06-04-133012 | CreateRefStatutsTable             | ---   | ---         | ---   |
| App       | 2026-06-04-133012 | CreateRefUnitesTable              | ---   | ---         | ---   |
+-----------+-------------------+-----------------------------------+-------+-------------+-------+
testdb=# \d
                List of relations
 Schema |       Name        |   Type   |  Owner
--------+-------------------+----------+---------
 public | migrations        | table    | testuser
 public | migrations_id_seq | sequence | testuser
(2 rows)
user@machine backend % php spark migrate:status

CodeIgniter v4.7.3 Command Line Tool - Server Time: 2026-06-04 16:41:45 UTC+02:00

[CodeIgniter\Database\Exceptions\DatabaseException]
pg_query(): Query failed: ERROR:  relation "migrations" already exists
at SYSTEMPATH/Database/BaseConnection.php:863

  Caused by:
  [CodeIgniter\Database\Exceptions\DatabaseException]
  pg_query(): Query failed: ERROR:  relation "migrations" already exists
  at SYSTEMPATH/Database/Postgre/Connection.php:212

  Caused by:
  [ErrorException]
  pg_query(): Query failed: ERROR:  relation "migrations" already exists
  at SYSTEMPATH/Database/Postgre/Connection.php:200

Backtrace:
  1    [internal function]
       CodeIgniter\Debug\Exceptions()->errorHandler(2, 'pg_query(): Query failed: ERROR:  relation "migrations" already exists', '/[censored]/backend/vendor/codeigniter4/framework/system/Database/Postgre/Connection.php', 200)

  2    SYSTEMPATH/Database/Postgre/Connection.php:200
       pg_query(Object(PgSql\Connection), 'CREATE TABLE "migrations" (
        "id" BIGSERIAL NOT NULL,
        "version" VARCHAR(255) NOT NULL,
        "class" VARCHAR(255) NOT NULL,
        "group" VARCHAR(255) NOT NULL,
        "namespace" VARCHAR(255) NOT NULL,
        "time" INT NOT NULL,
        "batch" INT NOT NULL,
        CONSTRAINT "pk_migrations" PRIMARY KEY("id")
)')

  3    SYSTEMPATH/Database/BaseConnection.php:908
       CodeIgniter\Database\Postgre\Connection()->execute('CREATE TABLE "migrations" (
        "id" BIGSERIAL NOT NULL,
        "version" VARCHAR(255) NOT NULL,
        "class" VARCHAR(255) NOT NULL,
        "group" VARCHAR(255) NOT NULL,
        "namespace" VARCHAR(255) NOT NULL,
        "time" INT NOT NULL,
        "batch" INT NOT NULL,
        CONSTRAINT "pk_migrations" PRIMARY KEY("id")
)')

  4    SYSTEMPATH/Database/BaseConnection.php:825
       CodeIgniter\Database\BaseConnection()->simpleQuery('CREATE TABLE "migrations" (
        "id" BIGSERIAL NOT NULL,
        "version" VARCHAR(255) NOT NULL,
        "class" VARCHAR(255) NOT NULL,
        "group" VARCHAR(255) NOT NULL,
        "namespace" VARCHAR(255) NOT NULL,
        "time" INT NOT NULL,
        "batch" INT NOT NULL,
        CONSTRAINT "pk_migrations" PRIMARY KEY("id")
)')

  5    SYSTEMPATH/Database/Forge.php:570
       CodeIgniter\Database\BaseConnection()->query('CREATE TABLE "migrations" (
        "id" BIGSERIAL NOT NULL,
        "version" VARCHAR(255) NOT NULL,
        "class" VARCHAR(255) NOT NULL,
        "group" VARCHAR(255) NOT NULL,
        "namespace" VARCHAR(255) NOT NULL,
        "time" INT NOT NULL,
        "batch" INT NOT NULL,
        CONSTRAINT "pk_migrations" PRIMARY KEY("id")
)')

  6    SYSTEMPATH/Database/MigrationRunner.php:868
       CodeIgniter\Database\Forge()->createTable('migrations', true)

  7    SYSTEMPATH/Database/MigrationRunner.php:699
       CodeIgniter\Database\MigrationRunner()->ensureTable()

  8    SYSTEMPATH/Commands/Database/MigrateStatus.php:115
       CodeIgniter\Database\MigrationRunner()->getHistory('')

  9    SYSTEMPATH/CLI/Commands.php:72
       CodeIgniter\Commands\Database\MigrateStatus()->run([])

 10    SYSTEMPATH/CLI/Console.php:47
       CodeIgniter\CLI\Commands()->run('migrate:status', [])

 11    SYSTEMPATH/Boot.php:442
       CodeIgniter\CLI\Console()->run()

 12    SYSTEMPATH/Boot.php:166
       CodeIgniter\Boot::runCommand(Object(CodeIgniter\CLI\Console))

 13    ROOTPATH/spark:87
       CodeIgniter\Boot::bootSpark(Object(Config\Paths))

Steps to Reproduce

see above

Expected Output

no crash should happen if the migrations table already exists.
this seems attributable to the deprecation of the $IfNotExists parameter and it's non-support in Forge.php line 588 and following

    /**
     * @param array $attributes Table attributes
     *
     * @return string SQL string
     *
     * @deprecated $ifNotExists is no longer used, and will be removed.
     */
    protected function _createTable(string $table, bool $ifNotExists, array $attributes)
    {
        $processedFields = $this->_processFields(true);

        for ($i = 0, $c = count($processedFields); $i < $c; $i++) {
            $processedFields[$i] = ($processedFields[$i]['_literal'] !== false) ? "\n\t" . $processedFields[$i]['_literal']
                : "\n\t" . $this->_processColumn($processedFields[$i]);
        }

        $processedFields = implode(',', $processedFields);

        $processedFields .= $this->_processPrimaryKeys($table);
        $processedFields .= current($this->_processForeignKeys($table));

        if ($this->createTableKeys === true) {
            $indexes = current($this->_processIndexes($table));
            if (is_string($indexes)) {
                $processedFields .= $indexes;
            }
        }

        return sprintf(
            $this->createTableStr . '%s',
            'CREATE TABLE',
            $this->db->escapeIdentifiers($table),
            $processedFields,
            $this->_createTableAttributes($attributes),
        );
    }

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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions