Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions services/celest_cloud_auth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.2+2

- fix: Migration of Cloud Auth tables

## 0.3.2+1

- fix: Session duration on Web
Expand Down
3 changes: 3 additions & 0 deletions services/celest_cloud_auth/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ targets:
enabled: true
generate_for:
- lib/src/database/**
- test/database/wrapper/**
options: &options
databases:
auth_database: lib/src/database/auth_database.dart
test_database: test/database/wrapper/wrapper_database.dart
schema_dir: drift_schema/
test_dir: test/database/

Expand All @@ -32,4 +34,5 @@ targets:
enabled: true
generate_for:
- lib/src/database/**
- test/database/wrapper/**
options: *options

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,37 @@ mixin CloudAuthDatabaseMixin on GeneratedDatabase {
MigrationStrategy createMigration({
OnCreate? onCreate,
OnUpgrade? onUpgrade,
OnBeforeOpen? onBeforeOpen,
OnBeforeOpen? beforeOpen,
ResolvedProject? project,
Iterable<String> additionalCedarTypes = const {},
Map<EntityUid, Entity> additionalCedarEntities = const {},
PolicySet? additionalCedarPolicies,
}) {
final defaultStrategy = MigrationStrategy();
onCreate ??= defaultStrategy.onCreate;
onUpgrade ??= defaultStrategy.onUpgrade;
return MigrationStrategy(
onCreate: onCreate,
onUpgrade: (m, from, to) async {
await onUpgrade!(m, from, to);
await cloudAuth.onUpgrade(m);
},
onUpgrade: onUpgrade,
beforeOpen: (details) async {
await onBeforeOpen?.call(details);
await cloudAuth.onBeforeOpen(details);
// First ensure that the cloud auth tables are up-to-date.
//
// This must be run here since the cloud auth tables may be updated
// out-of-sync with the wrapper database, meaning onUpgrade may miss
// updates related to the cloud auth tables.
await cloudAuth.onUpgrade(Migrator(this));

// Then seed the database with core types, entities, and relationships.
await cloudAuth.onBeforeOpen(
details,
project: project,
additionalCedarTypes: additionalCedarTypes,
additionalCedarEntities: additionalCedarEntities,
additionalCedarPolicies: additionalCedarPolicies,
);

// Then call beforeOpen, which may reference cloud auth tables.
await beforeOpen?.call(details);
},
);
}
Expand Down Expand Up @@ -336,12 +353,26 @@ class CloudAuthDatabaseAccessors extends DatabaseAccessor<GeneratedDatabase>
@internal int? to,
}) async {
try {
from ??= await cloudAuthMetaDrift.getSchemaVersion().getSingle();
from ??= await cloudAuthMetaDrift.getSchemaVersion().getSingleOrNull();
} on Object catch (e, st) {
_logger.finest('Error getting latest schema version', e, st);
if (from == null) {
// We're mising the meta tables for some reason.
//
// This should never happen.
throw StateError('Invalid schema detected');
}
}

// If the table is empty, then the database was just created and thus
// the schema version is current.
if (from == null) {
await cloudAuthMetaDrift.setSchemaVersion(
schemaVersion: schemaVersion,
);
from = schemaVersion;
}

from ??= 1;
to ??= schemaVersion;
if (from < to) {
_logger.fine('Migrating from version $from to version $to');
Expand All @@ -359,10 +390,17 @@ class CloudAuthDatabaseAccessors extends DatabaseAccessor<GeneratedDatabase>
Future<void> onBeforeOpen(
OpeningDetails details, {
ResolvedProject? project,
Iterable<String> additionalCedarTypes = const {},
Map<EntityUid, Entity> additionalCedarEntities = const {},
PolicySet? additionalCedarPolicies,
}) async {
await _withoutForeignKeys(() async {
if (details.wasCreated) {
await seed();
await seed(
additionalCedarTypes: additionalCedarTypes,
additionalCedarEntities: additionalCedarEntities,
additionalCedarPolicies: additionalCedarPolicies,
);
}
await upsertProject(project: project);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// dart format width=80
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
import 'package:drift/drift.dart';
import 'package:drift/internal/migrations.dart';
import 'schema_v1.dart' as v1;
import 'schema_v2.dart' as v2;

class GeneratedHelper implements SchemaInstantiationHelper {
@override
GeneratedDatabase databaseForVersion(QueryExecutor db, int version) {
switch (version) {
case 1:
return v1.DatabaseAtV1(db);
case 2:
return v2.DatabaseAtV2(db);
default:
throw MissingSchemaException(version, versions);
}
}

static const versions = const [1, 2];
}
Loading