The Wayback Machine - https://web.archive.org/web/20220411001439/https://github.com/hyperf/hyperf/issues/1587
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Database Migrations - Handling multiple migrations with different connections #1587

Open
goddanao opened this issue Apr 16, 2020 · 3 comments
Labels
enhancement good first issue

Comments

@goddanao
Copy link

@goddanao goddanao commented Apr 16, 2020

Is your feature request related to a problem? Please describe.
I got few migrations each referring a different connection. migrate:fresh, migrate:refresh doesn't handle this scenario since they operate on the default connection or in the specific connection passed via --database param.

Describe the solution you'd like
The simplest solution can be to process only migrations that has the $connection property set to the default connection or in the specific connection passed via --database param.
A better solution could be iterate the distinct connections defined in the migrations and process them according.

@goddanao goddanao added the enhancement label Apr 16, 2020
@goddanao goddanao changed the title [FEATURE] Database Migrations - Handling multiple connection defined via property $connection [FEATURE] Database Migrations - Handling multiple migrations with different connections Apr 16, 2020
@Reasno Reasno added the good first issue label Apr 19, 2020
@Reasno
Copy link
Member

@Reasno Reasno commented Apr 19, 2020

For now you can group migrations by connection in different folders and create a bash script to migrate them all, but PRs are welcome. :)

@Millyn
Copy link
Contributor

@Millyn Millyn commented Jul 1, 2020

我准备完成这个 PR,查看源代码之后,目前的思路是这样。
Hyperf\Database\Schema\Schema 的 create 静态方法增加第三个参数 $connection = default
然后在 Schema.php__callStatic 对新增的参数取值

    public static function __callStatic($name, $arguments)
    {
        $container = ApplicationContext::getContainer();
        $resolver = $container->get(ConnectionResolverInterface::class);
        $connection = $resolver->connection($arguments[2] ?? 'default'); // 修改了这里
        return $connection->getSchemaBuilder()->{$name}(...$arguments);
    }

假设我的 migration 分别是

//one 
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
        }, 'lbs');
// two
        Schema::create('password_resets', function (Blueprint $table) {
            $table->increments('id');
        },'lbs-2');

image

经过测试,指定库进行表迁移是可行,但 migrations 这张表只会在第一个数据库生成,并不会在第二个数据库生成,数据库并不是独立的 migrations 。

所以想请教一下,这块需要怎么样继续改动呢?是否有需要让 migraitons 在不同的库生成呢?

谢谢!

@Reasno

@Reasno
Copy link
Member

@Reasno Reasno commented Jul 1, 2020

@huangzhhui 来看看把 这块我不熟。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue
Projects
None yet
Development

No branches or pull requests

3 participants