ensureSchema method

Future<void> ensureSchema()

Creates the table, and its history table when history is enabled.

With types the columns take them, and the bookkeeping columns the types dartvel db migrate gives a generated model's. Without, nothing is declared, which SQLite and the in-memory adapter accept and PostgreSQL and MySQL refuse. The history table is the framework's own shape, and is always typed as the migration types it.

Implementation

Future<void> ensureSchema() async {
  final Map<String, String>? declared = types;
  final List<String> definitions = declared == null
      ? _storedColumns
      : <String>[
          for (final String column in columns) '$column ${declared[column]}',
          '$versionColumn INTEGER NOT NULL DEFAULT 1',
          '$deletedColumn TEXT',
        ];
  await database.execute(
    'CREATE TABLE IF NOT EXISTS $table (${definitions.join(', ')})',
  );
  if (historyPolicy != null) {
    await database.execute(
      'CREATE TABLE IF NOT EXISTS $historyTable (entry_id TEXT, '
      'record_key TEXT, record_version INTEGER, actor TEXT, tenant TEXT, '
      'transaction_id TEXT, occurred_at TEXT, changes TEXT, '
      'deleted INTEGER, restored INTEGER)',
    );
  }
}