From 8e62c4c52c27f9432820915deeb699c3d1f58ce7 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sat, 6 Oct 2018 14:15:54 +0200 Subject: Added new user models --- db/migrations/ChangesReferences.php | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 db/migrations/ChangesReferences.php (limited to 'db/migrations/ChangesReferences.php') diff --git a/db/migrations/ChangesReferences.php b/db/migrations/ChangesReferences.php new file mode 100644 index 00000000..d50f552e --- /dev/null +++ b/db/migrations/ChangesReferences.php @@ -0,0 +1,64 @@ +getReferencingTables($fromTable, $fromColumn); + + foreach ($references as $reference) { + /** @var stdClass $reference */ + $this->schema->table($reference->table, function (Blueprint $table) use ($reference) { + $table->dropForeign($reference->constraint); + }); + + $this->schema->table($reference->table, + function (Blueprint $table) use ($reference, $targetTable, $targetColumn, $type) { + $table->{$type}($reference->column)->change(); + + $table->foreign($reference->column) + ->references($targetColumn)->on($targetTable) + ->onDelete('cascade'); + }); + } + } + + /** + * @param string $table + * @param string $column + * @return array + */ + protected function getReferencingTables($table, $column): array + { + return $this->schema + ->getConnection() + ->select( + ' + SELECT + `TABLE_NAME` as "table", + `COLUMN_NAME` as "column", + `CONSTRAINT_NAME` as "constraint" + FROM information_schema.KEY_COLUMN_USAGE + WHERE REFERENCED_TABLE_SCHEMA = ? + AND REFERENCED_TABLE_NAME = ? + AND REFERENCED_COLUMN_NAME = ? + ', + [ + $this->schema->getConnection()->getDatabaseName(), + $table, + $column, + ] + ); + } +} -- cgit v1.2.3-54-g00ecf