All files / src/query-interface query-interface-partitions.ts

100% Statements 5/5
100% Branches 3/3
100% Functions 4/4
100% Lines 5/5

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                                    387x 387x                                                           155x                                                   76x                                         77x      
import type { CreatePartitionOptions, DetachPartitionOptions } from '../ddl/partition-ddl.ts';
import type { PartitionBoundSpec } from '../ddl/partition-types.ts';
import type { TableName } from '../ddl/sql-literal.ts';
import type { QueryOptionsBase, RuntimeOwner } from '../model/runtime.ts';
import { PartitionCommands } from './partition-commands.ts';
import { QueryInterfacePolicies } from './query-interface-policies.ts';
import { StatementRunner } from './statement-runner.ts';
 
/**
 * Declarative partitioning part of {@link QueryInterface}.
 */
export class QueryInterfacePartitions extends QueryInterfacePolicies {
  readonly #partitions: PartitionCommands;
 
  /**
   * @param sequelize - Owning `Sequelize` instance.
   */
  constructor(sequelize: RuntimeOwner) {
    super(sequelize);
    this.#partitions = new PartitionCommands(new StatementRunner(sequelize));
  }
 
  /**
   * Creates a partition of a partitioned table (`CREATE TABLE ... PARTITION OF`). The partitioned parent comes from `createTable` with `partitionBy`.
   *
   * @postgrelize
   * @param parent - Partitioned table name or `{ tableName, schema }`.
   * @param partition - New partition name or `{ tableName, schema }`.
   * @param bound - `{ in }` (list), `{ from, to }` (range), `{ modulus, remainder }` (hash) or `{ default: true }`; values are rendered as literals.
   * @param options - `partitionBy` (sub-partitioning), `ifNotExists` and query options.
   *
   * @example
   * ```ts
   * await queryInterface.createTable('events', { at: DataTypes.DATE }, {
   *   partitionBy: { strategy: 'range', columns: ['at'] },
   * });
   * await queryInterface.createPartition('events', 'events_2026', {
   *   from: ['2026-01-01'],
   *   to: ['2027-01-01'],
   * });
   * ```
   * @see [Constraints and indexes guide](/guide/constraints-indexes)
   */
  async createPartition(
    parent: TableName,
    partition: TableName,
    bound: PartitionBoundSpec,
    options: CreatePartitionOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#partitions.create(parent, partition, bound, options);
  }
 
  /**
   * Attaches an existing table as a partition (`ALTER TABLE ... ATTACH PARTITION`). PostgreSQL scans the table to check the bounds unless a matching `CHECK` constraint exists.
   *
   * @postgrelize
   * @param parent - Partitioned table name or `{ tableName, schema }`.
   * @param partition - Existing table name or `{ tableName, schema }`.
   * @param bound - Partition bounds, see {@link QueryInterface.createPartition}.
   * @param options - Query options.
   *
   * @example
   * ```ts
   * await queryInterface.attachPartition('events', 'events_2027', {
   *   from: ['2027-01-01'],
   *   to: ['2028-01-01'],
   * });
   * ```
   */
  async attachPartition(
    parent: TableName,
    partition: TableName,
    bound: PartitionBoundSpec,
    options: QueryOptionsBase = {},
  ): Promise<void> {
    await this.#partitions.attach(parent, partition, bound, options);
  }
 
  /**
   * Detaches a partition, keeping it as a standalone table (`ALTER TABLE ... DETACH PARTITION`).
   *
   * @postgrelize
   * @param parent - Partitioned table name or `{ tableName, schema }`.
   * @param partition - Partition name or `{ tableName, schema }`.
   * @param options - `concurrently` (PostgreSQL 14+, not inside a transaction), `finalize` and query options.
   *
   * @example
   * ```ts
   * await queryInterface.detachPartition('events', 'events_2026', { concurrently: true });
   * ```
   */
  async detachPartition(
    parent: TableName,
    partition: TableName,
    options: DetachPartitionOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#partitions.detach(parent, partition, options);
  }
}