All files / src/tools/auth0/handlers migrations.ts

97.05% Statements 33/34
85.71% Branches 12/14
100% Functions 8/8
96.87% Lines 31/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 791x 1x     1x         1x   137x               11x 11x 10x   1x             1x 5x   5x 5x   5x 4x 4x 4x           3x 1x           2x                 5x 5x 5x 5x 9x   5x 5x     5x 3x 4x   5x      
import DefaultHandler, { order } from './default';
import log from '../../../logger';
import { Asset, Assets } from '../../../types';
 
export const schema = {
  type: 'object',
  additionalProperties: { type: 'boolean' },
};
 
export default class MigrationsHandler extends DefaultHandler {
  constructor(options: DefaultHandler) {
    super({
      ...options,
      type: 'migrations',
    });
  }
 
  //TODO: standardize empty object literal with more intentional empty indicator
  async getType(): Promise<Asset[] | {}> {
    try {
      const migrations = await this.client.migrations.getMigrations();
      return migrations.flags;
    } catch (err) {
      Eif (err.statusCode === 404) return {};
      throw err;
    }
  }
 
  // Run at the end since switching a flag will depend on other applying other changes
  @order('150')
  async processChanges(assets: Assets): Promise<void> {
    const { migrations } = assets;
 
    Eif (migrations && Object.keys(migrations).length > 0) {
      const flags = await this.removeUnavailableMigrations(migrations);
 
      if (Object.keys(flags).length > 0) {
        await this.client.migrations.updateMigrations({ flags });
        this.updated += 1;
        this.didUpdate(flags);
      }
    }
  }
 
  logUnavailableMigrations(ignoreUnavailableMigrations: boolean, ignoredMigrations: string[]) {
    if (ignoreUnavailableMigrations) {
      log.info(
        `The following migrations are not available '${ignoredMigrations.join(
          ','
        )}'. The migrations will be ignored because you have AUTH0_IGNORE_UNAVAILABLE_MIGRATIONS=true in your configuration.`
      );
    } else {
      log.warn(
        `The following disabled migrations are not available '${ignoredMigrations.join(
          ','
        )}'. The migrations will be ignored, remove the migrations to avoid future warnings.`
      );
    }
  }
 
  async removeUnavailableMigrations(migrations: Asset[]): Promise<Asset[]> {
    const flags = { ...migrations };
    const ignoreUnavailableMigrations = !!this.config('AUTH0_IGNORE_UNAVAILABLE_MIGRATIONS');
    const existingMigrations = await this.getType();
    const unavailableMigrations = Object.keys(flags).filter(
      (flag) => !(flag in existingMigrations)
    );
    const ignoredMigrations = unavailableMigrations.filter(
      (flag) => ignoreUnavailableMigrations || flags[flag] === false
    );
 
    if (ignoredMigrations.length > 0) {
      this.logUnavailableMigrations(ignoreUnavailableMigrations, ignoredMigrations);
      ignoredMigrations.forEach((flag) => delete flags[flag]);
    }
    return flags;
  }
}