CodeIgniter icon indicating copy to clipboard operation
CodeIgniter copied to clipboard

Migration show an error, but applied

Open shlangus opened this issue 10 years ago • 11 comments

For an “up” migration: If you have any migration files with mismatched file's and class's names, migration will show an error "The migration class "Migration_Class_name" could not be found", but even so will apply, if the broken file's version greater than target version (i.e. we actually don't need to touch the broken file for migrate up to the target). If you try to migrate through several versions than the broken file can interrupt migration process with error, but all "good" files will work well, so migration version will increase but still be lower than target. Furthermore, if you try to go back to the last migration's version (I think it's very likely in case you got error), the migration will also show error, and won't make any changes. It's happens because migration files are checked in inverse order, so the broken file is interrupting a migration process before any "good" files can be applied. The reason in that condition checks all files even if they actually won't been applied.
For a "down" migration the situation are mirrored.

Let me show an example. I will use sequential migrations for simplifying. Let we have 4 migration file — 001-004, where file 003 is broken: it have name “003_Migrate_Create_users_table”, but consist class “Migrate_Broken_users_table”. So, current version is 1, and I want to migrate to version 2. I'm change version in the config file, and then I'm call migration->current(), which show an error, but applied. Now, I want to come back to the previous version. I set version to 1, and run migration->current().I get an error, and no changes were applied — I still have version 2. If I'll try to migrate from version 1 to 4, I’ll get an error too, and actually I'll been migrated to version 2.

shlangus avatar Mar 16 '16 09:03 shlangus

This is all very confusing the way you've put it ... let's see if I got it:

  1. You have migration numbers from 1 to 4
  2. You want to migrate from 1 to 2, not caring about 3 and 4
  3. The migration reports an error but still applies the migration version 2 file

Is that correct?

narfbg avatar Mar 16 '16 10:03 narfbg

In general, yes. I would go on the list:

  1. I want to migrate back from 2 to 1.
  2. The migration reports an error and don't applies, so I still have version 2.

shlangus avatar Mar 16 '16 10:03 shlangus

I left those intentionally as they depend on how the other three should work and we need to solve that first. Which brings me to the point - do you expect no migrations to be applied in case of an error? Or that migrations with numbers larger than what you've specified shouldn't be checked at all?

There are valid use cases for both ...

narfbg avatar Mar 16 '16 11:03 narfbg

Actually I expect all of this. I understand that the broken migration files is not normal and it's completely my problem, but the way this files can affect on migration process is not clear.

In the documentation says that migration->current() returns

current version string on success, FALSE on failure

So, when I got FALSE from migration->current(), I decide that something went wrong and expected that no changes applied to DB.

shlangus avatar Mar 16 '16 11:03 shlangus

What do you mean you expect "all of this"? It's one of two options, they cancel each other.

narfbg avatar Mar 16 '16 11:03 narfbg

I mean that in case of an error no migrations must be applied, but no need to check files with numbers larger than what I have specified.

shlangus avatar Mar 16 '16 12:03 shlangus

How's that?

narfbg avatar Mar 16 '16 14:03 narfbg

Seems pretty good. Thank you.

shlangus avatar Mar 16 '16 16:03 shlangus

Well, I know it looks good, but did you test it?

narfbg avatar Mar 16 '16 17:03 narfbg

Yes, I did. I was repeat all manipulations that I initially described, and now it's worked clear.

shlangus avatar Mar 16 '16 18:03 shlangus

Great then, thanks!

narfbg avatar Mar 16 '16 19:03 narfbg