"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddNetworkIdToAddresses1789500000000 = void 0;
const typeorm_1 = require("typeorm");
class AddNetworkIdToAddresses1789500000000 {
    async up(queryRunner) {
        const table = await queryRunner.getTable('addresses');
        if (!table) {
            return;
        }
        if (!table.findColumnByName('network_id')) {
            await queryRunner.addColumn('addresses', new typeorm_1.TableColumn({
                name: 'network_id',
                type: 'varchar',
                length: '255',
                isNullable: true,
            }));
        }
        const hasForeignKey = table.foreignKeys.some((fk) => fk.columnNames.includes('network_id'));
        if (!hasForeignKey) {
            await queryRunner.createForeignKey('addresses', new typeorm_1.TableForeignKey({
                columnNames: ['network_id'],
                referencedColumnNames: ['id'],
                referencedTableName: 'networks',
                onDelete: 'SET NULL',
            }));
        }
    }
    async down(queryRunner) {
        const table = await queryRunner.getTable('addresses');
        if (!table) {
            return;
        }
        const foreignKey = table.foreignKeys.find((fk) => fk.columnNames.includes('network_id'));
        if (foreignKey) {
            await queryRunner.dropForeignKey('addresses', foreignKey);
        }
        if (table.findColumnByName('network_id')) {
            await queryRunner.dropColumn('addresses', 'network_id');
        }
    }
}
exports.AddNetworkIdToAddresses1789500000000 = AddNetworkIdToAddresses1789500000000;
//# sourceMappingURL=1789500000000-AddNetworkIdToAddresses.js.map