mardi 16 juin 2020

How to take backup of DB table before truncating in a Laravel migration file?

The question has similarity with this.

Although that question seems unresolved, the comment section points out:

A true revertible migration would make a backup copy of the table visitors on up() before truncating and then copy the backup into the original on down().

Could not find any working solution for implementing the above steps.

How to take the dump of the DB table before truncating? Found some examples showing how to import from a .sql file, but that is not helpful here without first exporting the data.

The code would look something like:

class TruncateApiKeysTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // take backup of current api_keys
        // run the truncate
        if (Schema::hasTable('api_keys')) {
            Schema::table('api_keys', function(Blueprint $table) {
                $table->truncate();
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
       // import data from the sql dump file
       // insert data into api_keys to revert changes
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire