cakephp / cakephp/phinx

Migration snapshot tool creates duplicated index code for UNIQUE indexes

Open
#1,237 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
4.5k
Forks
884
PR merge metrics
No merged PRs in 30d

Description

Goal : create a migration from an existing database (MySQL)

  • Database where migration is being created from : mysqlnd 5.0.12-dev - 20150407 using phpMyAdmin. PHP version: 7.1.9

Indexes looks like this in phpMyAdmin's table view :
indexes of table
Notice how the index user_id is Unique

When following the Official CakePHP's migrations cookbook, doing the command bin/cake bake migration_snapshot Initial works correctly, but then I had problems creating an SQLite database as the error General error 1 : user_id_index already exists in [...] (it was similar to this, I'm writing this from memory) and I banged my head on why was that.

I took a look at the code generated by the snapshot command and I saw this :

// [...]
->addIndex(
    [
        'user_id',
    ],
    ['unique' => true]
)
->addIndex(
    [
        'user_id',
    ]
)
// [...]

Removing the second ->addIndex() solves the problem when creating the SQLite database using the migration(s).

To recap
  • Trying to go from MySQL to SQLite using migrations
  • Command bin/cake bake migration_snapshot Initial generates files without errors
  • then trying the command bin/cake migrations migrate create the SQLite database with errors :
    • General error 1 : user_id_index already exists in [...] in command line
    • only one table exists in SQLite database : phinxlog, which has no rows.
  • Removing duplicated code fixes the problem.

I feel like that duplicated code shouldn't have been generated and I'd consider that a bug.

I haven't done much testing, so I dunno about reproductability.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue by running bin/cake bake migration_snapshot Initial against the described MySQL database, then applying the generated migration to SQLite with bin/cake migrations migrate. Inspect the generated migration's addIndex() calls for the UNIQUE user_id index; done means the duplicate index code is absent and the SQLite migration completes without the user_id_index already exists error.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php, sqlite
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.