"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.down = exports.up = void 0;
const tslib_1 = require("tslib");
const Sequelize = tslib_1.__importStar(require("sequelize"));
function up(utils) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        {
            yield utils.queryInterface.addColumn('actorImage', 'actorId', {
                type: Sequelize.INTEGER,
                defaultValue: null,
                allowNull: true,
                references: {
                    model: 'actor',
                    key: 'id'
                },
                onDelete: 'CASCADE'
            }, { transaction: utils.transaction });
            {
                const query = `UPDATE "actorImage" SET "actorId" = (SELECT "id" FROM "actor" WHERE "actor"."avatarId" = "actorImage"."id") ` +
                    `WHERE "type" = 1`;
                yield utils.sequelize.query(query, { type: Sequelize.QueryTypes.UPDATE, transaction: utils.transaction });
            }
            {
                const query = `UPDATE "actorImage" SET "actorId" = (SELECT "id" FROM "actor" WHERE "actor"."bannerId" = "actorImage"."id") ` +
                    `WHERE "type" = 2`;
                yield utils.sequelize.query(query, { type: Sequelize.QueryTypes.UPDATE, transaction: utils.transaction });
            }
            {
                const query = `DELETE FROM "actorImage" WHERE id NOT IN (` +
                    `SELECT "bannerId" FROM actor WHERE "bannerId" IS NOT NULL ` +
                    `UNION select "avatarId" FROM actor WHERE "avatarId" IS NOT NULL` +
                    `);`;
                yield utils.sequelize.query(query, { type: Sequelize.QueryTypes.DELETE, transaction: utils.transaction });
            }
            yield utils.queryInterface.changeColumn('actorImage', 'actorId', {
                type: Sequelize.INTEGER,
                allowNull: false
            }, { transaction: utils.transaction });
            yield utils.queryInterface.removeColumn('actor', 'avatarId', { transaction: utils.transaction });
            yield utils.queryInterface.removeColumn('actor', 'bannerId', { transaction: utils.transaction });
        }
    });
}
exports.up = up;
function down() {
    throw new Error('Not implemented.');
}
exports.down = down;
//# sourceMappingURL=0685-multiple-actor-images.js.map