All files / src/query-interface schema-synchronizer.ts

98.18% Statements 54/55
92.59% Branches 25/27
96.15% Functions 25/26
98.11% Lines 52/53

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251                                                                                                                                  131x   131x   131x                                   571x 571x 571x                               258x 258x 41x 76x     258x               258x 258x 428x 428x     258x 428x                           2x 2x 4x   2x                   2x 6x                               706x 706x 38x   706x 706x                   151x 151x 151x 151x   534x   5x 151x                 706x   668x 8x   698x             559x 938x           708x 932x                     428x 426x   2x 2x 2x 2x 2x     8x 1x          
import { SqlLiteral } from '../ddl/sql-literal.ts';
import type { ModelDefinition } from '../model/model-definition.ts';
import type { ModelRuntime, QueryOptionsBase } from '../model/runtime.ts';
import { AlterPlanner } from './alter-planner.ts';
import { ModelTables } from './model-tables.ts';
import type { QueryInterface } from './query-interface.ts';
import { SyncPlanner } from './sync-planner.ts';
 
/**
 * `alter` options of `sync()`.
 *
 * @example
 * ```ts
 * const alter: AlterOptions = { drop: false };
 * await sequelize.sync({ alter });
 * ```
 */
export type AlterOptions = Readonly<{
  /** Drop columns that are not in the model. Default true. */
  drop?: boolean;
}>;
 
/**
 * Options of `sync()`: `force`, `alter`, `match`, `schema` plus the common query options.
 *
 * @example
 * ```ts
 * const options: SyncOptions = { alter: true, match: /_test$/, schema: 'tenant_1' };
 * await sequelize.sync(options);
 * ```
 */
export type SyncOptions = QueryOptionsBase &
  Readonly<{
    /** Drop and re-create tables. */
    force?: boolean;
    /** Alter existing tables to match models; `{ drop: false }` keeps extra columns. */
    alter?: boolean | AlterOptions;
    /** Only sync when the database name matches this pattern. */
    match?: RegExp;
    /** Create the tables in this schema instead of the models' schema (references stay in it too). */
    schema?: string;
    /** Run the `beforeSync` / `afterSync` hooks. Default true. */
    hooks?: boolean;
  }>;
 
/** Wraps the sync of one model (used to run its sync hooks). */
export type SyncWrapper = (definition: ModelDefinition, run: () => Promise<void>) => Promise<void>;
 
/**
 * Options of `drop()`: `cascade`, `schema` plus the common query options.
 *
 * @example
 * ```ts
 * const options: DropOptions = { cascade: true, schema: 'tenant_1' };
 * await sequelize.drop(options);
 * ```
 */
export type DropOptions = QueryOptionsBase &
  Readonly<{
    /** `DROP TABLE ... CASCADE`. */
    cascade?: boolean;
    /** Drop the tables in this schema instead of the models' schema. */
    schema?: string;
  }>;
 
const sqlLiteral = new SqlLiteral();
 
const planner = new SyncPlanner();
 
const tables = new ModelTables();
 
/**
 * Creates, alters and drops the tables of model definitions (`sync()` / `drop()`): dependency
 * order, deferred foreign keys for reference cycles, `force`, `alter` and the `schema` option.
 *
 * @internal
 */
export class SchemaSynchronizer {
  readonly #runtime: ModelRuntime;
  readonly #queryInterface: QueryInterface;
  readonly #alter: AlterPlanner;
 
  /**
   * @param runtime - Runtime executing the statements.
   * @param queryInterface - Query interface used to inspect and drop tables.
   */
  constructor(runtime: ModelRuntime, queryInterface: QueryInterface) {
    this.#runtime = runtime;
    this.#queryInterface = queryInterface;
    this.#alter = new AlterPlanner(queryInterface, tables);
  }
 
  /**
   * Syncs several models: referenced tables first, foreign keys of reference cycles added once
   * every table exists. With `force`, drops the tables in reverse order first.
   *
   * @param definitions - Model definitions.
   * @param options - `force`, `alter`, `schema` and query options.
   * @param wrap - Runs around the sync of each model.
   */
  async syncAll(
    definitions: readonly ModelDefinition[],
    options: SyncOptions = {},
    wrap: SyncWrapper = (_definition, run) => run(),
  ): Promise<void> {
    const ordered = planner.dependencyOrder(definitions);
    if (options.force === true) {
      await this.#each(ordered.toReversed(), (definition) =>
        this.drop(definition, { ...options, cascade: true }),
      );
    }
    await this.#syncOrdered(ordered, options, wrap);
  }
 
  async #syncOrdered(
    ordered: readonly ModelDefinition[],
    options: SyncOptions,
    wrap: SyncWrapper,
  ): Promise<void> {
    const deferred = planner.deferredReferences(ordered);
    await this.#each(ordered, (definition) =>
      wrap(definition, () =>
        this.sync(definition, { ...options, force: false }, deferred.get(definition)),
      ),
    );
    await this.#each(ordered, (definition) =>
      this.#addDeferredReferences(definition, deferred.get(definition) ?? [], options),
    );
  }
 
  /**
   * Order in which tables can be emptied: tables that reference others first. A reference cycle
   * cannot be ordered, so `cyclic` is set and the order is only a best effort.
   *
   * @param definitions - Model definitions.
   * @returns Definitions with referencing tables first, and whether foreign keys form a cycle.
   */
  truncationOrder(
    definitions: readonly ModelDefinition[],
  ): Readonly<{ models: readonly ModelDefinition[]; cyclic: boolean }> {
    const ordered = planner.dependencyOrder(definitions);
    const cyclic = [...planner.deferredReferences(ordered).values()].some(
      (columns) => columns.length > 0,
    );
    return { models: ordered.toReversed(), cyclic };
  }
 
  /**
   * Drops the tables of several models in reverse foreign-key order.
   *
   * @param definitions - Model definitions.
   * @param options - `cascade`, `schema` and query options.
   */
  async dropAll(definitions: readonly ModelDefinition[], options: DropOptions = {}): Promise<void> {
    await this.#each(planner.dependencyOrder(definitions).toReversed(), (definition) =>
      this.drop(definition, options),
    );
  }
 
  /**
   * Creates (or alters / re-creates) the table of a model and its indexes.
   *
   * @param model - Model definition.
   * @param options - `force`, `alter`, `schema` and query options.
   * @param deferred - Columns created without their foreign keys (added later).
   */
  async sync(
    model: ModelDefinition,
    options: SyncOptions = {},
    deferred: readonly string[] = [],
  ): Promise<void> {
    const definition = tables.inSchema(model, options.schema);
    if (options.force === true) {
      await this.drop(definition, { ...options, cascade: true });
    }
    const statements = await this.#tableStatements(definition, options, deferred);
    await this.#run([...statements, ...tables.indexStatements(definition)], options);
  }
 
  /**
   * Drops the table of a model and its enum types.
   *
   * @param model - Model definition.
   * @param options - `cascade`, `schema` and query options.
   */
  async drop(model: ModelDefinition, options: DropOptions = {}): Promise<void> {
    const definition = tables.inSchema(model, options.schema);
    const table = tables.tableOf(definition);
    await this.#queryInterface.dropTable(table, options);
    const enumFields = [...definition.attributes.values()]
      .filter(
        (attribute) => attribute.type.key === 'ENUM' || attribute.type.subtype?.key === 'ENUM',
      )
      .map((attribute) => attribute.field);
    await this.#queryInterface.dropEnumsOf(table, enumFields, options);
  }
 
  async #tableStatements(
    definition: ModelDefinition,
    options: SyncOptions,
    deferred: readonly string[],
  ): Promise<readonly string[]> {
    const exists =
      options.force !== true &&
      (await this.#queryInterface.tableExists(tables.tableOf(definition), options));
    if (exists && options.alter !== undefined && options.alter !== false) {
      return await this.#alter.statements(definition, options.alter, options);
    }
    return exists ? [] : tables.createStatements(definition, deferred, options.schema);
  }
 
  async #each(
    definitions: readonly ModelDefinition[],
    action: (definition: ModelDefinition) => Promise<void>,
  ): Promise<void> {
    await definitions.reduce<Promise<unknown>>(
      (previous, definition) => previous.then(() => action(definition)),
      Promise.resolve(),
    );
  }
 
  async #run(statements: readonly string[], options: QueryOptionsBase): Promise<void> {
    await statements.reduce<Promise<unknown>>(
      (previous, text) => previous.then(() => this.#runtime.execute({ text, values: [] }, options)),
      Promise.resolve(),
    );
  }
 
  /** Adds the deferred foreign keys of a table that are not there yet. */
  async #addDeferredReferences(
    model: ModelDefinition,
    fields: readonly string[],
    options: QueryOptionsBase & Readonly<{ schema?: string }>,
  ): Promise<void> {
    if (fields.length === 0) {
      return;
    }
    const { schema } = options;
    const definition = tables.inSchema(model, schema);
    const table = tables.tableOf(definition);
    const existing = await this.#alter.foreignKeyColumns(table, options);
    await this.#run(
      tables
        .storedColumns(definition)
        .filter((column) => fields.includes(column.field) && !existing.has(column.field))
        .map((column) => tables.foreignKeySql(sqlLiteral.quoteTable(table), column, schema)),
      options,
    );
  }
}