feat(todo): the sync engine and Todo's first models (ADR 0033, D5, D6)

Todo's server syncs with serverpod_offline_sync as Learn's and Train's
do, and holds two synced tables, every payload sealed on the devices:
content_key (the person's data key, sealed to the account) and project
(the Inbox or one of the person's own, changed when edited). A person's
space goes with them, with everything in it.

The migration only adds: the two tables, each indexed on spaceId and
cascading with its space (ON DELETE CASCADE to offline_sync_spaces), the
sync engine's own tables (crdt_*, offline_sync_*) with their foreign
keys, the gen_random_uuid_v7() function their ids default to, and the
modules' version rows. Nothing is dropped or changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-11-01 01:00:53 +01:00
commit 3876e30681
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -19,6 +19,13 @@ import 'package:serverpod_auth_core_server/serverpod_auth_core_server.dart'
as _iacs;
import 'package:serverpod_auth_idp_server/serverpod_auth_idp_server.dart'
as _iais;
import 'package:serverpod_offline_sync_server/serverpod_offline_sync_server.dart'
as _izehhkf5;
import 'privacy/content_key.dart' as _imed3x5d;
import 'project/project.dart' as _i9c44spp;
export 'privacy/content_key.dart';
export 'project/project.dart';
export 'sync_tables.dart';
class Protocol extends _is.DatabaseSerializationManager {
Protocol._();
@ -28,8 +35,137 @@ class Protocol extends _is.DatabaseSerializationManager {
static final Protocol _instance = Protocol._().._registerHostProtocols();
static List<_isp.TableDefinition> get targetTableDefinitions => [
_isp.TableDefinition(
name: 'content_key',
dartName: 'ContentKey',
schema: 'public',
module: 'halkyon_todo',
columns: [
_isp.ColumnDefinition(
name: 'id',
columnType: _isp.ColumnType.uuid,
isNullable: false,
dartType: 'UuidValue?',
columnDefault: 'random_v7',
),
_isp.ColumnDefinition(
name: 'spaceId',
columnType: _isp.ColumnType.bigint,
isNullable: true,
dartType: 'int?',
),
_isp.ColumnDefinition(
name: 'dataKeyId',
columnType: _isp.ColumnType.text,
isNullable: false,
dartType: 'String',
),
_isp.ColumnDefinition(
name: 'sealedKey',
columnType: _isp.ColumnType.bytea,
isNullable: false,
dartType: 'dart:typed_data:ByteData',
),
],
foreignKeys: [
_isp.ForeignKeyDefinition(
constraintName: 'content_key_fk_0',
columns: ['spaceId'],
referenceTable: 'offline_sync_spaces',
referenceTableSchema: 'public',
referenceColumns: ['id'],
onUpdate: _isp.ForeignKeyAction.noAction,
onDelete: _isp.ForeignKeyAction.cascade,
matchType: null,
),
],
indexes: [
_isp.IndexDefinition(
indexName: 'content_key_space_idx',
tableSpace: null,
elements: [
_isp.IndexElementDefinition(
type: _isp.IndexElementDefinitionType.column,
definition: 'spaceId',
),
],
type: 'btree',
isUnique: false,
isPrimary: false,
),
],
managed: true,
),
_isp.TableDefinition(
name: 'project',
dartName: 'Project',
schema: 'public',
module: 'halkyon_todo',
columns: [
_isp.ColumnDefinition(
name: 'id',
columnType: _isp.ColumnType.uuid,
isNullable: false,
dartType: 'UuidValue?',
columnDefault: 'random_v7',
),
_isp.ColumnDefinition(
name: 'spaceId',
columnType: _isp.ColumnType.bigint,
isNullable: true,
dartType: 'int?',
),
_isp.ColumnDefinition(
name: 'keyId',
columnType: _isp.ColumnType.text,
isNullable: true,
dartType: 'String?',
),
_isp.ColumnDefinition(
name: 'payload',
columnType: _isp.ColumnType.bytea,
isNullable: false,
dartType: 'dart:typed_data:ByteData',
),
_isp.ColumnDefinition(
name: 'nonce',
columnType: _isp.ColumnType.bytea,
isNullable: true,
dartType: 'dart:typed_data:ByteData?',
),
],
foreignKeys: [
_isp.ForeignKeyDefinition(
constraintName: 'project_fk_0',
columns: ['spaceId'],
referenceTable: 'offline_sync_spaces',
referenceTableSchema: 'public',
referenceColumns: ['id'],
onUpdate: _isp.ForeignKeyAction.noAction,
onDelete: _isp.ForeignKeyAction.cascade,
matchType: null,
),
],
indexes: [
_isp.IndexDefinition(
indexName: 'project_space_idx',
tableSpace: null,
elements: [
_isp.IndexElementDefinition(
type: _isp.IndexElementDefinitionType.column,
definition: 'spaceId',
),
],
type: 'btree',
isUnique: false,
isPrimary: false,
),
],
managed: true,
),
..._ik8molhd.Protocol.targetTableDefinitions,
..._iais.Protocol.targetTableDefinitions,
..._izehhkf5.Protocol.targetTableDefinitions,
..._ipoqfiqr.Protocol.targetTableDefinitions,
..._iacs.Protocol.targetTableDefinitions,
..._isp.Protocol.targetTableDefinitions,
@ -62,12 +198,27 @@ class Protocol extends _is.DatabaseSerializationManager {
}
}
if (t == _imed3x5d.ContentKey) {
return _imed3x5d.ContentKey.fromJson(data) as T;
}
if (t == _i9c44spp.Project) {
return _i9c44spp.Project.fromJson(data) as T;
}
if (t == _is.getType<_imed3x5d.ContentKey?>()) {
return (data != null ? _imed3x5d.ContentKey.fromJson(data) : null) as T;
}
if (t == _is.getType<_i9c44spp.Project?>()) {
return (data != null ? _i9c44spp.Project.fromJson(data) : null) as T;
}
try {
return _ik8molhd.Protocol().deserialize<T>(data, t);
} on _is.DeserializationTypeNotFoundException catch (_) {}
try {
return _iais.Protocol().deserialize<T>(data, t);
} on _is.DeserializationTypeNotFoundException catch (_) {}
try {
return _izehhkf5.Protocol().deserialize<T>(data, t);
} on _is.DeserializationTypeNotFoundException catch (_) {}
try {
return _ipoqfiqr.Protocol().deserialize<T>(data, t);
} on _is.DeserializationTypeNotFoundException catch (_) {}
@ -82,6 +233,8 @@ class Protocol extends _is.DatabaseSerializationManager {
static String? getClassNameForType(Type type) {
return switch (type) {
_imed3x5d.ContentKey => 'ContentKey',
_i9c44spp.Project => 'Project',
_ => null,
};
}
@ -98,6 +251,12 @@ class Protocol extends _is.DatabaseSerializationManager {
);
}
switch (data) {
case _imed3x5d.ContentKey():
return 'ContentKey';
case _i9c44spp.Project():
return 'Project';
}
className = _ik8molhd.Protocol().getClassNameForObject(data);
if (className != null) {
return className.contains('.') ? className : 'halkyon_base.$className';
@ -108,6 +267,12 @@ class Protocol extends _is.DatabaseSerializationManager {
? className
: 'serverpod_auth_idp.$className';
}
className = _izehhkf5.Protocol().getClassNameForObject(data);
if (className != null) {
return className.contains('.')
? className
: 'serverpod_offline_sync.$className';
}
className = _ipoqfiqr.Protocol().getClassNameForObject(data);
if (className != null) {
return className.contains('.') ? className : 'halkyon_oidc.$className';
@ -131,6 +296,12 @@ class Protocol extends _is.DatabaseSerializationManager {
if (dataClassName is! String) {
return super.deserializeByClassName(data);
}
if (dataClassName == 'ContentKey') {
return deserialize<_imed3x5d.ContentKey>(data['data']);
}
if (dataClassName == 'Project') {
return deserialize<_i9c44spp.Project>(data['data']);
}
if (dataClassName.startsWith('halkyon_base.')) {
data['className'] = dataClassName.substring(13);
return _ik8molhd.Protocol().deserializeByClassName(data);
@ -139,6 +310,10 @@ class Protocol extends _is.DatabaseSerializationManager {
data['className'] = dataClassName.substring(19);
return _iais.Protocol().deserializeByClassName(data);
}
if (dataClassName.startsWith('serverpod_offline_sync.')) {
data['className'] = dataClassName.substring(23);
return _izehhkf5.Protocol().deserializeByClassName(data);
}
if (dataClassName.startsWith('halkyon_oidc.')) {
data['className'] = dataClassName.substring(13);
return _ipoqfiqr.Protocol().deserializeByClassName(data);
@ -157,6 +332,7 @@ class Protocol extends _is.DatabaseSerializationManager {
void _registerHostProtocols() {
_ik8molhd.Protocol().registerHostProtocol('halkyon_todo', this);
_iais.Protocol().registerHostProtocol('halkyon_todo', this);
_izehhkf5.Protocol().registerHostProtocol('halkyon_todo', this);
_ipoqfiqr.Protocol().registerHostProtocol('halkyon_todo', this);
_iacs.Protocol().registerHostProtocol('halkyon_todo', this);
}
@ -175,6 +351,12 @@ class Protocol extends _is.DatabaseSerializationManager {
return table;
}
}
{
var table = _izehhkf5.Protocol().getTableForType(t);
if (table != null) {
return table;
}
}
{
var table = _ipoqfiqr.Protocol().getTableForType(t);
if (table != null) {
@ -193,6 +375,12 @@ class Protocol extends _is.DatabaseSerializationManager {
return table;
}
}
switch (t) {
case _imed3x5d.ContentKey:
return _imed3x5d.ContentKey.t;
case _i9c44spp.Project:
return _i9c44spp.Project.t;
}
return null;
}
@ -218,6 +406,9 @@ class Protocol extends _is.DatabaseSerializationManager {
try {
return _iais.Protocol().mapRecordToJson(record);
} catch (_) {}
try {
return _izehhkf5.Protocol().mapRecordToJson(record);
} catch (_) {}
try {
return _ipoqfiqr.Protocol().mapRecordToJson(record);
} catch (_) {}