Skip to content

Instantly share code, notes, and snippets.

@hose1021
Created October 17, 2021 21:25
Show Gist options
  • Save hose1021/f10250de6a03f5d82eefbe02d9d16f5d to your computer and use it in GitHub Desktop.
Save hose1021/f10250de6a03f5d82eefbe02d9d16f5d to your computer and use it in GitHub Desktop.

Revisions

  1. hose1021 created this gist Oct 17, 2021.
    72 changes: 72 additions & 0 deletions DisableForeignKeys.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    <?php

    namespace Shopper\Framework\Traits\Database;

    use Illuminate\Support\Facades\DB;

    trait DisableForeignKeys
    {
    /**
    * Command to disable foreign key for each database management.
    *
    * @var array
    */
    private $commands = [
    'mysql' => [
    'enable' => 'SET FOREIGN_KEY_CHECKS=1;',
    'disable' => 'SET FOREIGN_KEY_CHECKS=0;',
    ],
    'sqlite' => [
    'enable' => 'PRAGMA foreign_keys = ON;',
    'disable' => 'PRAGMA foreign_keys = OFF;',
    ],
    'sqlsrv' => [
    'enable' => 'EXEC sp_msforeachtable @command1="print \'?\'", @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all";',
    'disable' => 'EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all";',
    ],
    'pgsql' => [
    'enable' => 'SET CONSTRAINTS ALL IMMEDIATE;',
    'disable' => 'SET CONSTRAINTS ALL DEFERRED;',
    ],
    ];

    /**
    * Disable foreign key checks for current db driver.
    */
    protected function disableForeignKeys()
    {
    DB::statement($this->getDisableStatement());
    }

    /**
    * Enable foreign key checks for current db driver.
    */
    protected function enableForeignKeys()
    {
    DB::statement($this->getEnableStatement());
    }

    /**
    * Return current driver enable command.
    */
    private function getEnableStatement()
    {
    return $this->getDriverCommands()['enable'];
    }

    /**
    * Return current driver disable command.
    */
    private function getDisableStatement()
    {
    return $this->getDriverCommands()['disable'];
    }

    /**
    * Returns command array for current db driver.
    */
    private function getDriverCommands()
    {
    return $this->commands[DB::getDriverName()];
    }
    }