"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrate = exports.MIGRATIONS = exports.buildMigrations = void 0;
const backend_core_1 = require("@devlego/backend-core");
const types_1 = require("@devlego/types");
const environment_1 = __importDefault(require("../environment"));
// migration functions
const userEmailViewCasing = __importStar(require("./functions/userEmailViewCasing"));
const quota1 = __importStar(require("./functions/quotas1"));
const appUrls = __importStar(require("./functions/appUrls"));
const developerQuota = __importStar(require("./functions/developerQuota"));
const publishedAppsQuota = __importStar(require("./functions/publishedAppsQuota"));
const backfill = __importStar(require("./functions/backfill"));
/**
 * Populate the migration function and additional configuration from
 * the static migration definitions.
 */
const buildMigrations = () => {
    const definitions = backend_core_1.migrations.DEFINITIONS;
    const serverMigrations = [];
    for (const definition of definitions) {
        switch (definition.name) {
            case types_1.MigrationName.USER_EMAIL_VIEW_CASING: {
                serverMigrations.push(Object.assign(Object.assign({}, definition), { fn: userEmailViewCasing.run }));
                break;
            }
            case types_1.MigrationName.QUOTAS_1: {
                serverMigrations.push(Object.assign(Object.assign({}, definition), { fn: quota1.run }));
                break;
            }
            case types_1.MigrationName.APP_URLS: {
                serverMigrations.push(Object.assign(Object.assign({}, definition), { appOpts: { all: true }, fn: appUrls.run }));
                break;
            }
            case types_1.MigrationName.DEVELOPER_QUOTA: {
                serverMigrations.push(Object.assign(Object.assign({}, definition), { fn: developerQuota.run }));
                break;
            }
            case types_1.MigrationName.PUBLISHED_APP_QUOTA: {
                serverMigrations.push(Object.assign(Object.assign({}, definition), { fn: publishedAppsQuota.run }));
                break;
            }
            case types_1.MigrationName.EVENT_APP_BACKFILL: {
                serverMigrations.push(Object.assign(Object.assign({}, definition), { appOpts: { all: true }, fn: backfill.app.run, silent: !!environment_1.default.SELF_HOSTED, preventRetry: !!environment_1.default.SELF_HOSTED }));
                break;
            }
            case types_1.MigrationName.EVENT_GLOBAL_BACKFILL: {
                serverMigrations.push(Object.assign(Object.assign({}, definition), { fn: backfill.global.run, silent: !!environment_1.default.SELF_HOSTED, preventRetry: !!environment_1.default.SELF_HOSTED }));
                break;
            }
            case types_1.MigrationName.EVENT_INSTALLATION_BACKFILL: {
                serverMigrations.push(Object.assign(Object.assign({}, definition), { fn: backfill.installation.run, silent: !!environment_1.default.SELF_HOSTED, preventRetry: !!environment_1.default.SELF_HOSTED }));
                break;
            }
        }
    }
    return serverMigrations;
};
exports.buildMigrations = buildMigrations;
exports.MIGRATIONS = (0, exports.buildMigrations)();
const migrate = (options) => __awaiter(void 0, void 0, void 0, function* () {
    if (environment_1.default.SELF_HOSTED) {
        // self host runs migrations on startup
        // make sure only a single instance runs them
        yield migrateWithLock(options);
    }
    else {
        yield backend_core_1.migrations.runMigrations(exports.MIGRATIONS, options);
    }
});
exports.migrate = migrate;
const migrateWithLock = (options) => __awaiter(void 0, void 0, void 0, function* () {
    // get a new lock client
    const redlock = yield backend_core_1.redis.clients.getMigrationsRedlock();
    // lock for 15 minutes
    const ttl = 1000 * 60 * 15;
    let migrationLock;
    // acquire lock
    try {
        migrationLock = yield redlock.lock("migrations", ttl);
    }
    catch (e) {
        if (e.name === "LockError") {
            return;
        }
        else {
            throw e;
        }
    }
    // run migrations
    try {
        yield backend_core_1.migrations.runMigrations(exports.MIGRATIONS, options);
    }
    finally {
        // release lock
        try {
            yield migrationLock.unlock();
        }
        catch (e) {
            console.error("unable to release migration lock");
        }
    }
});
