Skip to content

Commit 19eb3f0

Browse files
authored
fix(cloud_auth): Migration of Cloud Auth tables (#383)
When wrapping over the Cloud Auth database, ensure that migrations are run at the correct times. Improves the interface of `createMigration` to better support these wrapper databases and add a test database for ensuring that mutual database upgrades are supported.
1 parent 1586345 commit 19eb3f0

File tree

14 files changed

+13120
-10
lines changed

14 files changed

+13120
-10
lines changed

services/celest_cloud_auth/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.2+2
2+
3+
- fix: Migration of Cloud Auth tables
4+
15
## 0.3.2+1
26

37
- fix: Session duration on Web

services/celest_cloud_auth/build.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ targets:
88
enabled: true
99
generate_for:
1010
- lib/src/database/**
11+
- test/database/wrapper/**
1112
options: &options
1213
databases:
1314
auth_database: lib/src/database/auth_database.dart
15+
test_database: test/database/wrapper/wrapper_database.dart
1416
schema_dir: drift_schema/
1517
test_dir: test/database/
1618

@@ -32,4 +34,5 @@ targets:
3234
enabled: true
3335
generate_for:
3436
- lib/src/database/**
37+
- test/database/wrapper/**
3538
options: *options

services/celest_cloud_auth/drift_schema/test_database/drift_schema_v1.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

services/celest_cloud_auth/drift_schema/test_database/drift_schema_v2.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

services/celest_cloud_auth/lib/src/database/auth_database_accessors.dart

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,37 @@ mixin CloudAuthDatabaseMixin on GeneratedDatabase {
5353
MigrationStrategy createMigration({
5454
OnCreate? onCreate,
5555
OnUpgrade? onUpgrade,
56-
OnBeforeOpen? onBeforeOpen,
56+
OnBeforeOpen? beforeOpen,
57+
ResolvedProject? project,
58+
Iterable<String> additionalCedarTypes = const {},
59+
Map<EntityUid, Entity> additionalCedarEntities = const {},
60+
PolicySet? additionalCedarPolicies,
5761
}) {
5862
final defaultStrategy = MigrationStrategy();
5963
onCreate ??= defaultStrategy.onCreate;
6064
onUpgrade ??= defaultStrategy.onUpgrade;
6165
return MigrationStrategy(
6266
onCreate: onCreate,
63-
onUpgrade: (m, from, to) async {
64-
await onUpgrade!(m, from, to);
65-
await cloudAuth.onUpgrade(m);
66-
},
67+
onUpgrade: onUpgrade,
6768
beforeOpen: (details) async {
68-
await onBeforeOpen?.call(details);
69-
await cloudAuth.onBeforeOpen(details);
69+
// First ensure that the cloud auth tables are up-to-date.
70+
//
71+
// This must be run here since the cloud auth tables may be updated
72+
// out-of-sync with the wrapper database, meaning onUpgrade may miss
73+
// updates related to the cloud auth tables.
74+
await cloudAuth.onUpgrade(Migrator(this));
75+
76+
// Then seed the database with core types, entities, and relationships.
77+
await cloudAuth.onBeforeOpen(
78+
details,
79+
project: project,
80+
additionalCedarTypes: additionalCedarTypes,
81+
additionalCedarEntities: additionalCedarEntities,
82+
additionalCedarPolicies: additionalCedarPolicies,
83+
);
84+
85+
// Then call beforeOpen, which may reference cloud auth tables.
86+
await beforeOpen?.call(details);
7087
},
7188
);
7289
}
@@ -336,12 +353,26 @@ class CloudAuthDatabaseAccessors extends DatabaseAccessor<GeneratedDatabase>
336353
@internal int? to,
337354
}) async {
338355
try {
339-
from ??= await cloudAuthMetaDrift.getSchemaVersion().getSingle();
356+
from ??= await cloudAuthMetaDrift.getSchemaVersion().getSingleOrNull();
340357
} on Object catch (e, st) {
341358
_logger.finest('Error getting latest schema version', e, st);
359+
if (from == null) {
360+
// We're mising the meta tables for some reason.
361+
//
362+
// This should never happen.
363+
throw StateError('Invalid schema detected');
364+
}
365+
}
366+
367+
// If the table is empty, then the database was just created and thus
368+
// the schema version is current.
369+
if (from == null) {
370+
await cloudAuthMetaDrift.setSchemaVersion(
371+
schemaVersion: schemaVersion,
372+
);
373+
from = schemaVersion;
342374
}
343375

344-
from ??= 1;
345376
to ??= schemaVersion;
346377
if (from < to) {
347378
_logger.fine('Migrating from version $from to version $to');
@@ -359,10 +390,17 @@ class CloudAuthDatabaseAccessors extends DatabaseAccessor<GeneratedDatabase>
359390
Future<void> onBeforeOpen(
360391
OpeningDetails details, {
361392
ResolvedProject? project,
393+
Iterable<String> additionalCedarTypes = const {},
394+
Map<EntityUid, Entity> additionalCedarEntities = const {},
395+
PolicySet? additionalCedarPolicies,
362396
}) async {
363397
await _withoutForeignKeys(() async {
364398
if (details.wasCreated) {
365-
await seed();
399+
await seed(
400+
additionalCedarTypes: additionalCedarTypes,
401+
additionalCedarEntities: additionalCedarEntities,
402+
additionalCedarPolicies: additionalCedarPolicies,
403+
);
366404
}
367405
await upsertProject(project: project);
368406
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// dart format width=80
2+
// GENERATED CODE, DO NOT EDIT BY HAND.
3+
// ignore_for_file: type=lint
4+
import 'package:drift/drift.dart';
5+
import 'package:drift/internal/migrations.dart';
6+
import 'schema_v1.dart' as v1;
7+
import 'schema_v2.dart' as v2;
8+
9+
class GeneratedHelper implements SchemaInstantiationHelper {
10+
@override
11+
GeneratedDatabase databaseForVersion(QueryExecutor db, int version) {
12+
switch (version) {
13+
case 1:
14+
return v1.DatabaseAtV1(db);
15+
case 2:
16+
return v2.DatabaseAtV2(db);
17+
default:
18+
throw MissingSchemaException(version, versions);
19+
}
20+
}
21+
22+
static const versions = const [1, 2];
23+
}

0 commit comments

Comments
 (0)