首页 文章

在laravel 5.6中迁移时如何检查mongo集合是否存在?

提问于
浏览
2

我正在我的一个项目中使用laravel 5.6,mongodb和mysql . 我使用了jessengers mongodb包,通过它我为我的3个集合创建了模式,虽然mongodb是模式较少的数据库,但出于文档目的,我创建了模式 . 其中一个例子是:

<?php

use Jenssegers\Mongodb\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateChatMessagesTable extends Migration
{

    /**
     * The name of the database connection to use.
     *
     * @var string
     */
    protected $connection = 'mongodb';


    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection($this->connection)->create('chat_messages', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('trade_id');
            $table->tinyInteger('type')->default('1');
            $table->text('content');
            $table->integer('recipient_id');
            $table->timestamp('recipient_read_at')->nullable();
            $table->timestamp('created_at')->nullable();
            $table->tinyInteger('status')->default('1');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::connection($this->connection)
            ->table('chat_messages', function (Blueprint $collection)
            {
                $collection->drop();
            });
    }
}

这里的问题是每当我运行 php artisan migrate:fresh 命令时它会给我一个像 collection already exists 这样的错误 . 我需要检查,特别是对于mongodb,如果存在集合,则不要再次迁移集合 .

我猜我应该运行一个类似的查询

if(true == ChatMessage::get()){
//do not run the migration
} else{
//continue the migration
}

仅在迁移文件中,但我从未尝试过这种方式,我将其保留为最后解决方案 . 请指导和帮助我 .

1 回答

  • 0

    我在搜索了laravel的文档后得到了解决方案 .

    有一种方法叫做: hasTable() ,无论collection / table是否存在,都返回 boolean 值 .

    这就是我的方式,现在工作正常:

    <?php
    
    use Jenssegers\Mongodb\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateChatMessagesTable extends Migration
    {
    
        /**
         * The name of the database connection to use.
         *
         * @var string
         */
        protected $connection = 'mongodb';
    
    
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            if(Schema::connection($this->connection)->hasTable('chat_messages') == false) {
                Schema::connection($this->connection)->create('chat_messages', function (Blueprint $table) {
                    $table->increments('id');
                    $table->integer('user_id');
                    $table->integer('trade_id');
                    $table->tinyInteger('type')->default('1');
                    $table->text('content');
                    $table->integer('recipient_id');
                    $table->timestamp('recipient_read_at')->nullable();
                    $table->timestamp('created_at')->nullable();
                    $table->tinyInteger('status')->default('1');
                });
            }
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            if(Schema::connection($this->connection)->hasTable('chat_messages') == false) {
                Schema::connection($this->connection)
                    ->table('chat_messages', function (Blueprint $collection) {
                        $collection->drop();
                    });
            }
        }
    }
    

    我希望将来有人能从这个解决方案中受益 .

相关问题