"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessControl1780900000000 = void 0;
class AccessControl1780900000000 {
    constructor() {
        this.name = 'AccessControl1780900000000';
    }
    async getColumnDefinition(queryRunner, tableName, columnName) {
        const [column] = await queryRunner.query(`SELECT COLUMN_TYPE, CHARACTER_SET_NAME, COLLATION_NAME
       FROM INFORMATION_SCHEMA.COLUMNS
       WHERE TABLE_SCHEMA = DATABASE()
         AND TABLE_NAME = ?
         AND COLUMN_NAME = ?
       LIMIT 1`, [tableName, columnName]);
        return column;
    }
    buildModifySql(columnName, definition, nullable) {
        const nullSql = nullable ? 'NULL' : 'NOT NULL';
        if (definition.CHARACTER_SET_NAME && definition.COLLATION_NAME) {
            return `MODIFY \`${columnName}\` ${definition.COLUMN_TYPE} CHARACTER SET ${definition.CHARACTER_SET_NAME} COLLATE ${definition.COLLATION_NAME} ${nullSql}`;
        }
        return `MODIFY \`${columnName}\` ${definition.COLUMN_TYPE} ${nullSql}`;
    }
    async up(queryRunner) {
        await queryRunner.query(`CREATE TABLE IF NOT EXISTS \`access_roles\` (\`id\` varchar(36) NOT NULL, \`owner_id\` varchar(191) NOT NULL, \`name\` varchar(191) NOT NULL, \`slug\` varchar(191) NOT NULL, \`description\` varchar(191) NOT NULL DEFAULT 'NT/APL', \`is_system\` tinyint NOT NULL DEFAULT 0, \`hierarchy\` int NOT NULL DEFAULT '0', \`created_at\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), \`updated_at\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), UNIQUE INDEX \`IDX_access_roles_owner_slug\` (\`owner_id\`, \`slug\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE IF NOT EXISTS \`role_permissions\` (\`id\` varchar(36) NOT NULL, \`role_id\` varchar(191) NOT NULL, \`permission_key\` varchar(191) NOT NULL, \`permission_value\` varchar(191) NOT NULL, \`created_at\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), \`updated_at\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), UNIQUE INDEX \`IDX_role_permissions_role_key\` (\`role_id\`, \`permission_key\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        const ownerId = await this.getColumnDefinition(queryRunner, 'owner', 'id');
        if (ownerId) {
            await queryRunner.query(`ALTER TABLE \`access_roles\` ${this.buildModifySql('owner_id', ownerId, false)}`);
        }
        const roleId = await this.getColumnDefinition(queryRunner, 'access_roles', 'id');
        if (roleId) {
            await queryRunner.query(`ALTER TABLE \`role_permissions\` ${this.buildModifySql('role_id', roleId, false)}`);
        }
        const accessRolesFk = await queryRunner.query(`SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'access_roles' AND CONSTRAINT_NAME = 'FK_access_roles_owner' LIMIT 1`);
        if (accessRolesFk.length === 0) {
            await queryRunner.query(`ALTER TABLE \`access_roles\` ADD CONSTRAINT \`FK_access_roles_owner\` FOREIGN KEY (\`owner_id\`) REFERENCES \`owner\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
        }
        const rolePermissionsFk = await queryRunner.query(`SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'role_permissions' AND CONSTRAINT_NAME = 'FK_role_permissions_role' LIMIT 1`);
        if (rolePermissionsFk.length === 0) {
            await queryRunner.query(`ALTER TABLE \`role_permissions\` ADD CONSTRAINT \`FK_role_permissions_role\` FOREIGN KEY (\`role_id\`) REFERENCES \`access_roles\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
        }
    }
    async down(queryRunner) {
        await queryRunner.query(`ALTER TABLE \`role_permissions\` DROP FOREIGN KEY \`FK_role_permissions_role\``);
        await queryRunner.query(`ALTER TABLE \`access_roles\` DROP FOREIGN KEY \`FK_access_roles_owner\``);
        await queryRunner.query(`DROP TABLE \`role_permissions\``);
        await queryRunner.query(`DROP TABLE \`access_roles\``);
    }
}
exports.AccessControl1780900000000 = AccessControl1780900000000;
//# sourceMappingURL=1780900000000-AccessControl.js.map