SchemeMigrationPlan constructor

SchemeMigrationPlan(
  1. StdMap stored,
  2. StdMap actual,
  3. Scheme scheme
)

Creates and initializes a SchemeMigrationPlan.

This constructor accepts the following parameters:

  • stored is the overall version of the scheme loaded from the database or an empty map if the database is being created;
  • actual is the overall version of scheme;
  • scheme is the scheme expected by the application.

See also: Scheme.toDefinition.

Implementation

SchemeMigrationPlan(StdMap stored, StdMap actual, Scheme scheme) {
  storedSchemaVersion = stored[Scheme.kSchemaVersion].xAs<int?>();
  actualSchemaVersion = actual[Scheme.kSchemaVersion].xAs<int>();
  if (storedSchemaVersion != null &&
      storedSchemaVersion! != actualSchemaVersion)
    throw JsonSqliteException(
      "The database schema version does not match the expected version "
      "(stored=$storedSchemaVersion; actual=$actualSchemaVersion)",
    );
  storedSchemeRevision = stored[Scheme.kSchemeRevision].xAs<int?>();
  actualSchemeRevision = actual[Scheme.kSchemeRevision].xAs<int>();
  if (storedSchemeRevision != null &&
      storedSchemeRevision! > actualSchemeRevision)
    throw JsonSqliteException(
      "The database scheme revision is higher than expected "
      "(stored=$storedSchemeRevision; actual=$actualSchemeRevision)",
    );
  storedKindPath = stored[Scheme.kKindPath].xAs<String?>();
  actualKindPath = actual[Scheme.kKindPath].xAs<String>();
  storedVirtualColumns = getElementsMap(stored, Scheme.kVirtualColumns);
  actualVirtualColumns = getElementsMap(actual, Scheme.kVirtualColumns);
  storedPartialIndexes = getElementsMap(stored, Scheme.kPartialIndexes);
  actualPartialIndexes = getElementsMap(actual, Scheme.kPartialIndexes);
  storedSearchIndexes = getElementsMap(stored, Scheme.kSearchIndexes);
  actualSearchIndexes = getElementsMap(actual, Scheme.kSearchIndexes);
  performRevisionUpgrade =
      storedSchemeRevision != null &&
      storedSchemeRevision != actualSchemeRevision;
  if (storedKindPath != actualKindPath) {
    dropKindColumnAndIndex = storedKindPath != null;
    makeKindColumnAndIndex = true;
    dropVirtualColumns.addAll(storedVirtualColumns.keys);
    makeVirtualColumns.addAll(actualVirtualColumns.keys);
    dropPartialIndexes.addAll(storedPartialIndexes.keys);
    makePartialIndexes.addAll(actualPartialIndexes.keys);
    dropSearchIndexes.addAll(storedSearchIndexes.keys);
    makeSearchIndexes.addAll(actualSearchIndexes.keys);
    return;
  }
  dropKindColumnAndIndex = false;
  makeKindColumnAndIndex = false;
  sortElementsMigration(
    storedVirtualColumns,
    actualVirtualColumns,
    dropVirtualColumns,
    makeVirtualColumns,
  );
  sortElementsMigration(
    storedPartialIndexes,
    actualPartialIndexes,
    dropPartialIndexes,
    makePartialIndexes,
  );
  sortElementsMigration(
    storedSearchIndexes,
    actualSearchIndexes,
    dropSearchIndexes,
    makeSearchIndexes,
  );
  var lcDVC = dropVirtualColumns.map(lowercaseAscii).toSet();
  var lcMVC = makeVirtualColumns.map(lowercaseAscii).toSet();
  var lcUpdatedColumns = lcDVC.intersection(lcMVC);
  var retainedIndexes = actualPartialIndexes.keys.toSet().difference(
    makePartialIndexes,
  );
  for (var retainedIndex in retainedIndexes) {
    var columns = scheme.getPartialIndex(retainedIndex).columns;
    if (columns.any((var c) => lcUpdatedColumns.contains(c.lcName))) {
      dropPartialIndexes.add(retainedIndex);
      makePartialIndexes.add(retainedIndex);
    }
  }
}