-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #625 from art-institute-of-chicago/feature/renamed…
…-catalogs-to-publications Rename catalogs to publications [WEB-2971]
- Loading branch information
Showing
7 changed files
with
214 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...igrations/2024_11_22_142707_rename_digital_and_printed_catalog_tables_to_publications.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
// Instead of trying to drop specific constraints, we'll query the system tables | ||
// to find and drop the existing constraints | ||
$this->dropSequenceConstraints(); | ||
|
||
// Rename digital sequences | ||
DB::unprepared('ALTER SEQUENCE digital_catalog_page_id_seq RENAME TO digital_publication_page_id_seq'); | ||
DB::unprepared('ALTER SEQUENCE digital_catalog_revisions_id_seq RENAME TO digital_publication_revisions_id_seq'); | ||
DB::unprepared('ALTER SEQUENCE digital_catalog_slugs_id_seq RENAME TO digital_publication_slugs_id_seq'); | ||
DB::unprepared('ALTER SEQUENCE digital_catalogs_id_seq RENAME TO digital_publications_id_seq'); | ||
|
||
// Rename printed sequences | ||
DB::unprepared('ALTER SEQUENCE printed_catalog_revisions_id_seq RENAME TO printed_publication_revisions_id_seq'); | ||
DB::unprepared('ALTER SEQUENCE printed_catalog_slugs_id_seq RENAME TO printed_publication_slugs_id_seq'); | ||
DB::unprepared('ALTER SEQUENCE printed_catalogs_id_seq RENAME TO printed_publications_id_seq'); | ||
|
||
// Update sequence defaults | ||
DB::unprepared('ALTER TABLE digital_publication_page ALTER COLUMN id SET DEFAULT nextval(\'digital_publication_page_id_seq\')'); | ||
DB::unprepared('ALTER TABLE digital_publication_revisions ALTER COLUMN id SET DEFAULT nextval(\'digital_publication_revisions_id_seq\')'); | ||
DB::unprepared('ALTER TABLE digital_publication_slugs ALTER COLUMN id SET DEFAULT nextval(\'digital_publication_slugs_id_seq\')'); | ||
DB::unprepared('ALTER TABLE digital_publications ALTER COLUMN id SET DEFAULT nextval(\'digital_publications_id_seq\')'); | ||
|
||
DB::unprepared('ALTER TABLE printed_publication_revisions ALTER COLUMN id SET DEFAULT nextval(\'printed_publication_revisions_id_seq\')'); | ||
DB::unprepared('ALTER TABLE printed_publication_slugs ALTER COLUMN id SET DEFAULT nextval(\'printed_publication_slugs_id_seq\')'); | ||
DB::unprepared('ALTER TABLE printed_publications ALTER COLUMN id SET DEFAULT nextval(\'printed_publications_id_seq\')'); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
// Drop existing sequence defaults | ||
$this->dropSequenceConstraints(); | ||
|
||
// Rename digital sequences back | ||
DB::unprepared('ALTER SEQUENCE digital_publication_page_id_seq RENAME TO digital_catalog_page_id_seq'); | ||
DB::unprepared('ALTER SEQUENCE digital_publication_revisions_id_seq RENAME TO digital_catalog_revisions_id_seq'); | ||
DB::unprepared('ALTER SEQUENCE digital_publication_slugs_id_seq RENAME TO digital_catalog_slugs_id_seq'); | ||
DB::unprepared('ALTER SEQUENCE digital_publications_id_seq RENAME TO digital_catalogs_id_seq'); | ||
|
||
// Rename printed sequences back | ||
DB::unprepared('ALTER SEQUENCE printed_publication_revisions_id_seq RENAME TO printed_catalog_revisions_id_seq'); | ||
DB::unprepared('ALTER SEQUENCE printed_publication_slugs_id_seq RENAME TO printed_catalog_slugs_id_seq'); | ||
DB::unprepared('ALTER SEQUENCE printed_publications_id_seq RENAME TO printed_catalogs_id_seq'); | ||
|
||
// Update sequence defaults | ||
DB::unprepared('ALTER TABLE digital_publication_page ALTER COLUMN id SET DEFAULT nextval(\'digital_catalog_page_id_seq\')'); | ||
DB::unprepared('ALTER TABLE digital_publication_revisions ALTER COLUMN id SET DEFAULT nextval(\'digital_catalog_revisions_id_seq\')'); | ||
DB::unprepared('ALTER TABLE digital_publication_slugs ALTER COLUMN id SET DEFAULT nextval(\'digital_catalog_slugs_id_seq\')'); | ||
DB::unprepared('ALTER TABLE digital_publications ALTER COLUMN id SET DEFAULT nextval(\'digital_catalogs_id_seq\')'); | ||
|
||
DB::unprepared('ALTER TABLE printed_publication_revisions ALTER COLUMN id SET DEFAULT nextval(\'printed_catalog_revisions_id_seq\')'); | ||
DB::unprepared('ALTER TABLE printed_publication_slugs ALTER COLUMN id SET DEFAULT nextval(\'printed_catalog_slugs_id_seq\')'); | ||
DB::unprepared('ALTER TABLE printed_publications ALTER COLUMN id SET DEFAULT nextval(\'printed_catalogs_id_seq\')'); | ||
} | ||
|
||
/** | ||
* Drop sequence constraints for all relevant tables | ||
*/ | ||
private function dropSequenceConstraints(): void | ||
{ | ||
$tables = [ | ||
'digital_publication_page', | ||
'digital_publication_revisions', | ||
'digital_publication_slugs', | ||
'digital_publications', | ||
'printed_publication_revisions', | ||
'printed_publication_slugs', | ||
'printed_publications' | ||
]; | ||
|
||
foreach ($tables as $table) { | ||
// Get all constraints for the id column of the table | ||
$constraints = DB::select(" | ||
SELECT con.conname as constraint_name | ||
FROM pg_constraint con | ||
INNER JOIN pg_class rel ON rel.oid = con.conrelid | ||
INNER JOIN pg_namespace nsp ON nsp.oid = rel.relnamespace | ||
WHERE rel.relname = ? | ||
AND con.conname LIKE '%id%' | ||
", [$table]); | ||
|
||
// Drop each constraint found | ||
foreach ($constraints as $constraint) { | ||
DB::statement("ALTER TABLE {$table} DROP CONSTRAINT IF EXISTS {$constraint->constraint_name}"); | ||
} | ||
|
||
// Also drop the default constraint if it exists | ||
DB::statement("ALTER TABLE {$table} ALTER COLUMN id DROP DEFAULT"); | ||
} | ||
} | ||
}; |
99 changes: 99 additions & 0 deletions
99
database/migrations/2024_11_22_145448_rename_printed_catalog_category_tables.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
// First drop the foreign key constraints | ||
Schema::table('catalog_category_printed_catalog', function (Blueprint $table) { | ||
$table->dropForeign('catalog_category_printed_catalog_catalog_category_id_foreign'); | ||
$table->dropForeign('catalog_category_printed_catalog_printed_catalog_id_foreign'); | ||
}); | ||
|
||
// Drop the index | ||
DB::statement('DROP INDEX IF EXISTS idx_catalog_category_printed_catalog_E0dSH'); | ||
|
||
// Rename the sequence | ||
DB::unprepared('ALTER SEQUENCE catalog_category_printed_catalog_id_seq RENAME TO catalog_category_printed_publication_id_seq'); | ||
|
||
// Rename the table | ||
Schema::rename('catalog_category_printed_catalog', 'catalog_category_printed_publication'); | ||
|
||
// Update column name to match new convention | ||
Schema::table('catalog_category_printed_publication', function (Blueprint $table) { | ||
$table->renameColumn('printed_catalog_id', 'printed_publication_id'); | ||
}); | ||
|
||
// Recreate the index with new name | ||
DB::statement('CREATE INDEX idx_catalog_category_printed_publication ON catalog_category_printed_publication (printed_publication_id, catalog_category_id)'); | ||
|
||
// Add back foreign key constraints with updated references | ||
Schema::table('catalog_category_printed_publication', function (Blueprint $table) { | ||
$table->foreign('catalog_category_id') | ||
->references('id') | ||
->on('catalog_categories') | ||
->onDelete('cascade'); | ||
|
||
$table->foreign('printed_publication_id') | ||
->references('id') | ||
->on('printed_publications') | ||
->onDelete('cascade'); | ||
}); | ||
|
||
// Update the sequence default | ||
DB::unprepared('ALTER TABLE catalog_category_printed_publication ALTER COLUMN id SET DEFAULT nextval(\'catalog_category_printed_publication_id_seq\')'); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
// Drop foreign key constraints | ||
Schema::table('catalog_category_printed_publication', function (Blueprint $table) { | ||
$table->dropForeign(['catalog_category_id']); | ||
$table->dropForeign(['printed_publication_id']); | ||
}); | ||
|
||
// Drop the index | ||
DB::statement('DROP INDEX IF EXISTS idx_catalog_category_printed_publication'); | ||
|
||
// Rename the sequence back | ||
DB::unprepared('ALTER SEQUENCE catalog_category_printed_publication_id_seq RENAME TO catalog_category_printed_catalog_id_seq'); | ||
|
||
// Rename column back to original | ||
Schema::table('catalog_category_printed_publication', function (Blueprint $table) { | ||
$table->renameColumn('printed_publication_id', 'printed_catalog_id'); | ||
}); | ||
|
||
// Rename the table back | ||
Schema::rename('catalog_category_printed_publication', 'catalog_category_printed_catalog'); | ||
|
||
// Recreate the original index | ||
DB::statement('CREATE INDEX idx_catalog_category_printed_catalog_E0dSH ON catalog_category_printed_catalog (printed_catalog_id, catalog_category_id)'); | ||
|
||
// Add back original foreign key constraints | ||
Schema::table('catalog_category_printed_catalog', function (Blueprint $table) { | ||
$table->foreign('catalog_category_id') | ||
->references('id') | ||
->on('catalog_categories') | ||
->onDelete('cascade'); | ||
|
||
$table->foreign('printed_catalog_id') | ||
->references('id') | ||
->on('printed_publications') | ||
->onDelete('cascade'); | ||
}); | ||
|
||
// Update the sequence default | ||
DB::unprepared('ALTER TABLE catalog_category_printed_catalog ALTER COLUMN id SET DEFAULT nextval(\'catalog_category_printed_catalog_id_seq\')'); | ||
} | ||
}; |