migrateScheme method

  1. @protected
void migrateScheme(
  1. StdMap stored,
  2. StdMap actual
)

Migrates a scheme from the stored overall version to the actual one.

Implementation

@protected
void migrateScheme(StdMap stored, StdMap actual) {
  var plan = SchemeMigrationPlan(stored, actual, scheme);
  log.info(
    "Scheme ${stored.isEmpty ? "creation" : "migration"} "
    "plan:\n${plan.dump()}",
  );

  finalize(
    () {
      if (plan.performRevisionUpgrade)
        scheme.upgrader?.before(
          this,
          plan.storedSchemeRevision!,
          plan.actualSchemeRevision,
        );

      var dropTable = MessageFormat("DROP TABLE {0}");
      var dropIndex = MessageFormat("DROP INDEX {0}");
      var dropColumn = MessageFormat(
        "ALTER TABLE $tableMapping DROP COLUMN {0}",
      );

      for (var si in plan.dropSearchIndexes)
        _database!.execute(dropTable.xFormat([sqlIdentifier(si)]));
      for (var pi in plan.dropPartialIndexes)
        _database!.execute(dropIndex.xFormat([sqlIdentifier(pi)]));
      for (var vc in plan.dropVirtualColumns.xToList().reversed)
        _database!.execute(dropColumn.xFormat([sqlIdentifier(vc)]));
      if (plan.dropKindColumnAndIndex) {
        _database!.execute(dropIndex.xFormat([columnKind]));
        _database!.execute(dropColumn.xFormat([columnKind]));
      }

      var makeColumn = MessageFormat(
        "ALTER TABLE $tableMapping ADD COLUMN {0} AS ({1}) VIRTUAL",
      );
      var makeIndex = MessageFormat(
        "CREATE INDEX {0} ON $tableMapping ({1}) WHERE {2}",
      );
      var makeTable = MessageFormat(
        "CREATE VIRTUAL TABLE {0} "
        "USING FTS5($columnKeyId UNINDEXED, {1}, tokenize = ''{2}')",
      );

      if (plan.makeKindColumnAndIndex) {
        _database!.execute(
          formatMakeColumn(
            makeColumn,
            columnKind,
            [],
            plan.actualKindPath == unsetKindPath
                ? SqlL(null)
                : SqlJX(plan.actualKindPath),
          ),
        );
        _database!.execute(
          formatMakeIndex(makeIndex, columnKind, [IndexColumn(columnKind)]),
        );
      }
      for (var vc in plan.makeVirtualColumns) {
        var column = scheme.getVirtualColumn(vc);
        _database!.execute(
          formatMakeColumn(makeColumn, vc, column.kinds, column.sql),
        );
      }
      for (var pi in plan.makePartialIndexes) {
        var index = scheme.getPartialIndex(pi);
        _database!.execute(formatMakeIndex(makeIndex, pi, index.columns));
      }
      for (var si in plan.makeSearchIndexes) {
        var index = scheme.getSearchIndex(si);
        _database!.execute(
          makeTable.xFormat([
            sqlIdentifier(si),
            index.columnsDdlSql(),
            escapeSingleQuotes(index.tokenizer),
          ]),
        );
      }

      initializeUpdatersLookup();

      if (plan.makeSearchIndexes.isNotEmpty) {
        var where = SqlI(columnKind).equal(SqlP("@$columnKind"));
        QueryId qid = prepare(
          columns: [SqlJT(SqlI(columnValue))],
          where: where,
          limit: queryDefaultLimit,
        );
        finalize(() {
          for (var si in plan.makeSearchIndexes) {
            var index = scheme.getSearchIndex(si);
            for (var MapEntry(key: kind, value: srGetter)
                in index.getters.entries) {
              QueryRowPosition? start;
              do {
                QueryRows rows = query(
                  qid,
                  parameters: {"@$columnKind": kind},
                  start: start,
                );
                for (var row in rows) {
                  FullId id = row.id;
                  SearchRow searchRow = callSRG(
                    srGetter,
                    (jsonDecode(row.columns.single! as String) as Object?)
                        .xAs<Object>(),
                  );
                  _searchIndexUpdaters[index]!.insert.executeWith(
                    StatementParameters.named({
                      ...index.searchRowToParameters(searchRow),
                      ":$columnRowId": id.rowid,
                      ":$columnKeyId": id.keyid,
                    }),
                  );
                  if (!updated())
                    throw JsonSqliteException(
                      "Search index migration failed for rowid ${id.rowid}, "
                      "kind \"$kind\", index name \"${index.name}\"",
                    );
                }
                start = rows.length == queryDefaultLimit
                    ? queryRowGetPosition(rows.last)
                    : null;
              } while (start != null);
            }
          }
        }, finale: (_) => free(qid));
      }

      _database!.execute(
        "INSERT OR REPLACE INTO $tableScheme "
        "($columnKeyId, $columnValue) VALUES (?, jsonb(?))",
        [keyDefinition, jsonToString(actual)],
      );
      ////
    },
    finale: (Object? thrown) {
      if (plan.performRevisionUpgrade)
        scheme.upgrader?.after(this, finished: thrown == null);
    },
  );
}