yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Migration (--migrationPath) #9698

Closed ghost closed 8 years ago

ghost commented 9 years ago

hi all, sorry for any inconvenience when making a migration indicating the migrationPath, for example:

$./yii migrate --migrationPath=modules/foo/migrations

yii does not save the migration path used in migration. So when I run the command down, I have to specify the migrationPath for each migration or grup. but when many migrations, this becomes a problem.

when i run the command without the parameter migrationPath in a migration that is not in the default directory y get the exception failed to open stream: No such file or directory' obviously

with many migrations and modules do this is difficult, I think yii should store the migrationPath used for each migration.

something like change the migration table to :

CREATE TABLE migration (
        version varchar(180) PRIMARY KEY,
        path varchar(x),
        apply_time integer,
  )

if there is a way to do it, let me note. thk team :D

samdark commented 9 years ago

There's currently no way doing it automatically.

mdmunir commented 9 years ago

Alternative https://github.com/deesoft/yii2-console Add your migration paths as application params

// params.php
return [
    'dee.migration.path' => [
        '@yii/rbac/migrations',
        '@mdm/autonumber/migrations',
        '@mdm/upload/migrations',
    ]
];
ghost commented 9 years ago

modify the database would be the best way? and change the getMigrationHistory() (in yii\console\controllers\MigrationController) to modify the basePath Attribute based on the value of the database

pana1990 commented 9 years ago

maybe "db" should be include too. it is useful when i have two or more dbms.

@samdark @mubushi @mdmunir what do you think about it? i can create pull request for this issue.

samdark commented 9 years ago

I think the best way to solve it is to create bash script that runs a couple of commands specifying necessary --migrationPath. Are there any pros implementing it at console command level?

pana1990 commented 9 years ago

if you want to use " migrate/redo all or migrate/down all"? bash script not works for this case use.

samdark commented 9 years ago

Why?

pana1990 commented 9 years ago

how @mubushi said

"when i run the command without the parameter migrationPath in a migration that is not in the default directory y get the exception failed to open stream: No such file or directory' obviously "

if you want to run migrate/up you have create bash script, if you want to run migrate/down you have create another bash script again.

when you have many migrations and modules do this is difficult, i think to this feature should be include out of the box.

samdark commented 9 years ago

Not really another. It's about adding more lines to existing migrate_down.sh or migrate_up.sh. Same amount of lines as would end up in config, I guess...

pana1990 commented 9 years ago

when you have new migration you have to include it in migrate_down.sh and migrate_up.sh, it is little troublesome. I think to this feature should be handle framework.

it simplify development.

samdark commented 9 years ago

Not when you have new migration but when you have new path for migrations such as new module. That's the same with configs — with each new module you have to add another path to config file.

pana1990 commented 9 years ago

sorry, i dont understand.

if i want to "migrate/redo all" for example how do you do it?

in the following case how do you do it?

 1º migration default path
 2º  '' module user path
 3º  '' default path
 4º  '' module rbac path
samdark commented 9 years ago

Yes:

./yii migrate/redo all
./yii migrate/redo all --migrationPath=module1/foo/migrations
./yii migrate/redo all --migrationPath=module2/foo/migrations
./yii migrate/redo all --migrationPath=module3/foo/migrations
pana1990 commented 9 years ago

@samdark I think you dont understand me.

For example i run the following steps :

1º i create migration test1 in default path and run migrate 2º i run migration in rbac module 3º i create second migration test2 in default path and run migrate 4º i run migration in user module

migration history is : screenshot_2

when i run yii migrate/redo it throws an exception obviusly : screenshot_3

klimov-paul commented 9 years ago

The most proper way to solve such problem would be usage of the namespaces for migrations insteand of paths. In general we can determine the file path from the namespace and thus find all new migrations. While storing full namespaced class names in the migration table allow to revert any migration at any moment no matter where it is declared. This would be a nice solutions for extension building and issues like #8202.

samdark commented 9 years ago

@pana1990 then you commit all these migrations into repository and another developer who's in your team is trying to apply them. How would they do that?

samdark commented 9 years ago

@klimov-paul yes, that could solve the issue.

pana1990 commented 9 years ago

@samdark mmm, it's truth. In rails resolve this issue with schema dump. For more details http://guides.rubyonrails.org/active_record_migrations.html#schema-dumping-and-you

ghost commented 8 years ago

hi guys, @klimov-paul, @samdark , @pana1990 , sorry for the inactivity. i think the approach:

usage of the namespaces for migrations instead of paths.

is the better and simple way to solve this issue.

nkostadinov commented 8 years ago

+1 for @klimov-paul solution. Migrations should use namespaces at least to be consistent. Working with many modules with migrations can be really cumbersome.

mdmunir commented 8 years ago

A simple way to solve this problem and still keep BC is add new property to migration controller e.g migrationLookup. So, instead of use --migrationPath=@newPath we add new path to that propterty. Everytime we need new path, we can add it to migrationLookup

'controllerMap' => [
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationLookup' => [
            '@yii/rbac/migrations',
            '@another/ext/migrations',
        ],
    ]
]

Then, we just change simple code in MigrateController::getMigrationHistory().

PS: We dont need to change table schema.

ghost commented 8 years ago

hi @mdmunir, the database schema does not change, just the whole class name (all namespace) in the "version" field in the table would be stored.

mdmunir commented 8 years ago

@mubushi And what about previous migration file that not in the namespaced class? Is it still compatible?

ghost commented 8 years ago

@mdmunir I believe that the above files are supported, eg up migration ./yii migrate --migrationPath = @ path / to / migrations and the database could keep path \ to \ Migrations \ migrationName

during migrate / down yii find the file, even if does not have namespace as said @klimov-paul

In general we can determine the file path from the namespace and thus find all new migrations. While storing full namespaced class names in the migration table allow to revert any migration at any moment no matter where it is declared.

and solves the problem of locating migration in turning back

. but what you propose I like, because it would not specify the migration path migrate / up and would not have to make many changes XD.

Faryshta commented 8 years ago

I think the best to solved this is to use namespaces and a $depends field like it was proposed in #8202

namespace Acme\Sihpments\migrations;

use yii\db\Migration;

class ShipmentLog extends Migration
{
    $depends = [
         'Acme\Shipments\migration\Shipment'
    ];
}

Then use two tables in the migration table to handle the history of migrations save the full namespaced class

klimov-paul commented 8 years ago

Solution proposed at #12511

klimov-paul commented 8 years ago

Resolved by commit 8aa0e85

mdmunir commented 8 years ago

@klimov-paul are the solution backward compatible? I mean, when i do migrate/up in the past from several directory, then i want migrate/down all, it is going ok?

klimov-paul commented 8 years ago

yes, it should be

mdmunir commented 8 years ago

@klimov-paul When migrate/up screenshot from 2016-09-14 15 52 07

When migrate/down screenshot from 2016-09-14 15 53 57

klimov-paul commented 8 years ago

The file path will NOT be saved - it was never promised. You can put your migrations under the namespace and they will be saved into the history with thier namespace. You can specify several namespaces, but not several file paths.

mdmunir commented 8 years ago

That what i mean. The migration does not remember migration path. Namespace solution only fix next migration but not previous problem.

klimov-paul commented 8 years ago

Several migration path will not be supported - this decision is final. You may update your project to make migration namespaced and manually update the content of the migration table (you can use migration mark command for that) if it is crucial for you.