"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.down = down;
exports.up = up;

var _dateFns = require("date-fns");

var _slug = require("../slug");

async function up(knex) {
  await knex.schema.alterTable('image', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('seasonId');
  }).alterTable('notificationsHistory', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('episodeId');
  }).alterTable('notificationPlatformsCredentials', table => {
    table.dropForeign('userId');
  }).alterTable('accessToken', table => {
    table.dropForeign('userId');
  }).alterTable('seen', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('episodeId');
    table.dropForeign('userId');
  }).alterTable('userRating', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('seasonId');
    table.dropForeign('episodeId');
    table.dropForeign('userId');
  }).alterTable('watchlist', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('userId');
  }).alterTable('listItem', table => {
    table.dropForeign('episodeId');
    table.dropForeign('seasonId');
    table.dropForeign('mediaItemId');
    table.dropForeign('listId');
  }).alterTable('list', table => {
    table.dropForeign('userId');
  }).alterTable('episode', table => {
    table.dropForeign('tvShowId');
    table.dropForeign('seasonId');
  }).alterTable('season', table => {
    table.dropForeign('tvShowId');
  });
  await knex.schema.alterTable('mediaItem', table => {
    table.string('slug');
    table.index('slug');
    table.unique(['mediaType', 'slug']);
  });
  const mediaItems = await knex('mediaItem');

  for (const mediaItem of mediaItems) {
    const releaseYear = mediaItem.releaseDate ? (0, _dateFns.parseISO)(mediaItem.releaseDate).getFullYear() : undefined;
    const slug = (0, _slug.toSlug)(releaseYear ? `${mediaItem.title}-${releaseYear}` : mediaItem.title);
    const slugExists = await knex('mediaItem').where('slug', slug).first();
    await knex('mediaItem').update({
      slug: slugExists ? (0, _slug.toSlug)(`${slug}-${(0, _slug.randomSlugId)()}`) : slug
    }).where('id', mediaItem.id);
  }

  await knex.schema.alterTable('mediaItem', table => {
    table.string('slug').notNullable().alter({
      alterNullable: true
    });
  });
  await knex.schema.alterTable('season', table => {
    table.foreign('tvShowId').references('id').inTable('mediaItem');
  }).alterTable('episode', table => {
    table.foreign('tvShowId').references('id').inTable('mediaItem');
    table.foreign('seasonId').references('id').inTable('season');
  }).alterTable('image', table => {
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('seasonId').references('id').inTable('season');
  }).alterTable('notificationsHistory', table => {
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('episodeId').references('id').inTable('episode');
  }).alterTable('list', table => {
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('listItem', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('seasonId').references('id').inTable('season');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('listId').references('id').inTable('list');
  }).alterTable('watchlist', table => {
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('userRating', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('seasonId').references('id').inTable('season');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('seen', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('accessToken', table => {
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('notificationPlatformsCredentials', table => {
    table.foreign('userId').references('id').inTable('user');
  });
}

async function down(knex) {
  await knex.schema.alterTable('image', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('seasonId');
  }).alterTable('notificationsHistory', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('episodeId');
  }).alterTable('notificationPlatformsCredentials', table => {
    table.dropForeign('userId');
  }).alterTable('accessToken', table => {
    table.dropForeign('userId');
  }).alterTable('seen', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('episodeId');
    table.dropForeign('userId');
  }).alterTable('userRating', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('seasonId');
    table.dropForeign('episodeId');
    table.dropForeign('userId');
  }).alterTable('watchlist', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('userId');
  }).alterTable('listItem', table => {
    table.dropForeign('episodeId');
    table.dropForeign('seasonId');
    table.dropForeign('mediaItemId');
    table.dropForeign('listId');
  }).alterTable('list', table => {
    table.dropForeign('userId');
  }).alterTable('episode', table => {
    table.dropForeign('tvShowId');
    table.dropForeign('seasonId');
  }).alterTable('season', table => {
    table.dropForeign('tvShowId');
  });
  await knex.schema.alterTable('mediaItem', table => {
    table.dropUnique(['mediaType', 'slug']);
    table.dropIndex('slug');
    table.dropColumn('slug');
  });
  await knex.schema.alterTable('season', table => {
    table.foreign('tvShowId').references('id').inTable('mediaItem');
  }).alterTable('episode', table => {
    table.foreign('tvShowId').references('id').inTable('mediaItem');
    table.foreign('seasonId').references('id').inTable('season');
  }).alterTable('image', table => {
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('seasonId').references('id').inTable('season');
  }).alterTable('notificationsHistory', table => {
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('episodeId').references('id').inTable('episode');
  }).alterTable('list', table => {
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('listItem', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('seasonId').references('id').inTable('season');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('listId').references('id').inTable('list');
  }).alterTable('watchlist', table => {
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('userRating', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('seasonId').references('id').inTable('season');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('seen', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('accessToken', table => {
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('notificationPlatformsCredentials', table => {
    table.foreign('userId').references('id').inTable('user');
  });
}
//# sourceMappingURL=20220312002700_mediaItemSlug.js.map