Skip to content

Commit

Permalink
Merge pull request #625 from art-institute-of-chicago/feature/renamed…
Browse files Browse the repository at this point in the history
…-catalogs-to-publications

Rename catalogs to publications [WEB-2971]
  • Loading branch information
web-dev-trev authored and nikhiltri committed Dec 9, 2024
2 parents dbde70e + bba12f2 commit 29e4085
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 9 deletions.
6 changes: 3 additions & 3 deletions app/Http/Controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function index()
public function autocomplete()
{
$collection = GeneralSearch::search(request('q'))
->resources(['artworks', 'exhibitions', 'artists', 'agents', 'events', 'articles', 'digital-catalogs', 'printed-catalogs', 'highlights'])
->resources(['artworks', 'exhibitions', 'artists', 'agents', 'events', 'articles', 'digital-publications', 'printed-publications', 'highlights'])
->getSearch(self::AUTOCOMPLETE_PER_PAGE);

foreach ($collection as &$item) {
Expand Down Expand Up @@ -439,8 +439,8 @@ protected function buildSearchLinks($all, $active = 'all')
}
array_push($links, $this->buildLabel('Interactive Features', $all->total(), route('search.interactive-features', ['q' => request('q')]), $active == 'interactive-features'));

if (QueryHelpers::extractAggregation($aggregations, ['digital-catalogs', 'printed-catalogs'])) {
array_push($links, $this->buildLabel('Publications', QueryHelpers::extractAggregation($aggregations, ['digital-catalogs', 'printed-catalogs']), route('search.publications', ['q' => request('q')]), $active == 'publications'));
if (QueryHelpers::extractAggregation($aggregations, ['digital-publications', 'printed-publications'])) {
array_push($links, $this->buildLabel('Publications', QueryHelpers::extractAggregation($aggregations, ['digital-publications', 'printed-publications']), route('search.publications', ['q' => request('q')]), $active == 'publications'));
}

if (QueryHelpers::extractAggregation($aggregations, 'educator-resources')) {
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Api/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class Search extends BaseApiModel
'sections' => 'App\Models\Api\Section',
'events' => 'App\Models\Event',
'articles' => 'App\Models\Article',
'printed-catalogs' => 'App\Models\PrintedPublication',
'digital-catalogs' => 'App\Models\DigitalPublication',
'printed-publications' => 'App\Models\PrintedPublication',
'digital-publications' => 'App\Models\DigitalPublication',
'digital-publication-articles' => 'App\Models\DigitalPublicationArticle',
'static-pages' => 'App\Models\Page',
'generic-pages' => 'App\Models\GenericPage',
Expand Down
2 changes: 1 addition & 1 deletion app/Models/PrintedPublication.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class PrintedPublication extends AbstractModel

public function categories()
{
return $this->belongsToMany('App\Models\CatalogCategory', 'catalog_category_printed_catalog', 'printed_catalog_id');
return $this->belongsToMany('App\Models\CatalogCategory', 'catalog_category_printed_publication', 'printed_publication_id');
}

public function scopeIds($query, $ids = [])
Expand Down
4 changes: 2 additions & 2 deletions app/Repositories/Api/SearchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function forSearchQuery($string, $perPage = null, $columns = [], $pageNam
'agents',
'events',
'articles',
'digital-catalogs',
'printed-catalogs',
'digital-publications',
'printed-publications',
'generic-pages',
'educator-resources',
'press-releases',
Expand Down
2 changes: 1 addition & 1 deletion app/Repositories/PublicationsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PublicationsRepository
public function searchApi($string, $perPage = null, $page = null, $columns = [])
{
// Find top-level catalogs that match the search
$search = Search::query()->search($string)->published()->resources(['digital-catalogs', 'printed-catalogs']);
$search = Search::query()->search($string)->published()->resources(['digital-publications', 'printed-publications']);
$results = $search->getSearch($perPage, $columns, null, $page);

// Now find matching `articles` and add them to the catalogs
Expand Down
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");
}
}
};
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\')');
}
};

0 comments on commit 29e4085

Please sign in to comment.