ensureHistoryTable method

Future<void> ensureHistoryTable(
  1. SessionExecutor executor
)

Ensures the migration history table exists and contains all known columns.

Implementation

Future<void> ensureHistoryTable(pg.SessionExecutor executor) async {
  await executor.run((session) async {
    await session.execute('''
        CREATE TABLE IF NOT EXISTS "$historyTableName" (
          name TEXT PRIMARY KEY,
          applied_at TIMESTAMPTZ NOT NULL,
          statement_count INTEGER NOT NULL,
          kind TEXT NOT NULL DEFAULT 'apply',
          target_name TEXT,
          provider TEXT,
          checksum TEXT,
          before_schema TEXT,
          after_schema TEXT,
          warnings TEXT,
          rebuild_required BOOLEAN NOT NULL DEFAULT FALSE
        )
      ''', ignoreRows: true);
    await session.execute(
      'ALTER TABLE "$historyTableName" '
      'ADD COLUMN IF NOT EXISTS kind TEXT NOT NULL DEFAULT \'apply\'',
      ignoreRows: true,
    );
    await session.execute(
      'ALTER TABLE "$historyTableName" '
      'ADD COLUMN IF NOT EXISTS target_name TEXT',
      ignoreRows: true,
    );
    await session.execute(
      'ALTER TABLE "$historyTableName" '
      'ADD COLUMN IF NOT EXISTS provider TEXT',
      ignoreRows: true,
    );
    await session.execute(
      'ALTER TABLE "$historyTableName" '
      'ADD COLUMN IF NOT EXISTS checksum TEXT',
      ignoreRows: true,
    );
    await session.execute(
      'ALTER TABLE "$historyTableName" '
      'ADD COLUMN IF NOT EXISTS before_schema TEXT',
      ignoreRows: true,
    );
    await session.execute(
      'ALTER TABLE "$historyTableName" '
      'ADD COLUMN IF NOT EXISTS after_schema TEXT',
      ignoreRows: true,
    );
    await session.execute(
      'ALTER TABLE "$historyTableName" '
      'ADD COLUMN IF NOT EXISTS warnings TEXT',
      ignoreRows: true,
    );
    await session.execute(
      'ALTER TABLE "$historyTableName" '
      'ADD COLUMN IF NOT EXISTS rebuild_required BOOLEAN NOT NULL DEFAULT FALSE',
      ignoreRows: true,
    );
  });
}