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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ To note a change, add a line under Unreleased, in a group such as Added, Changed

- Documentation for the whole API, built with DocC: doc comments in the public headers, which Xcode shows in Quick Help, and an article with examples for each of CoreZen's three parts: persistence, media and utilities.
- `-[ZENFrameRenderer renderExactFrameAtSeconds:width:height:completion:]`, which renders the frame a player shows at a time, rather than the keyframe at or before it.
- `-[ZENDatabaseSchema initializeDatabase:error:]`, which says whether the schema update worked, and why not when it didn't. An app can stop there, instead of going on to use tables the update didn't change.

### Deprecated

- `-[ZENDatabaseSchema initializeDatabase:]`, which can't say that the update failed. Use `initializeDatabase:error:`.

### Fixed

- Frames rendered from a file whose timestamps don't start at 0, such as an MPEG transport stream, come from the time asked for. Before, times counted from timestamp 0, so frames came from too early and their actual times were off by the file's start, and a rough seek could land a keyframe or more late.
- Frames render from interlaced video, such as a DVD's MPEG-2. FFmpeg's scaler refused to turn an interlaced frame into a progressive image, so every render of one had no image.
- A schema update whose commit fails rolls back. Before, the update's transaction could stay open, so the queue's next transaction ran inside it and was lost with it.

## 0.1.0 - 2026-11-19

Expand Down
14 changes: 11 additions & 3 deletions CoreZen/CoreZen.docc/Persistence.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,22 @@ A later version of the schema adds a branch for version 2, and so on. Each versi

### Open the Database

A ``ZENDatabaseSchema`` brings the database up to the latest version of every table's schema:
A ``ZENDatabaseSchema`` brings the database up to the latest version of every table's schema. Outside a transaction, as in a fetch block, the update commits in a transaction of its own, so its result says whether it saved:

```objc
ZENDatabaseQueue *queue = [ZENDatabaseQueue databaseQueueWithURL:databaseURL];
ZENDatabaseSchema *schema = [ZENDatabaseSchema schemaWithTableClasses:@[NoteTable.class]];
[queue transactionSync:^(ZENDatabase *database) {
[schema initializeDatabase:database];
__block BOOL updated = NO;
__block NSError *error = nil;
[queue fetchSync:^(ZENDatabase *database) {
updated = [schema initializeDatabase:database error:&error];
}];
if (!updated) {
// The update rolled back, so the database has its earlier version, which
// the tables' code may not fit. The error says which statement failed.
[NSApp presentError:error];
return;
}
```

### Load and Save Objects
Expand Down
31 changes: 25 additions & 6 deletions CoreZen/Database/DatabaseSchema.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,32 @@ NS_ASSUME_NONNULL_BEGIN
+ (instancetype)schemaWithTableClasses:(NSArray *)tables;

/// Updates a database to the latest version, then moves the identifier counter
/// past its largest identifier.
/// past its largest identifier, and says whether the update worked.
///
/// The update runs in a transaction, joining the caller's if one is open. If any
/// statement fails, the whole update rolls back, so it runs again the next time.
/// Then it raises the counter that ``ZENGetNextObjectIdentifier`` uses past the
/// largest identifier in any of the tables.
- (void)initializeDatabase:(ZENDatabase *)database;
/// The update runs in a transaction of its own, or joins the caller's if one is
/// open. If any statement in it fails, or its commit does, the whole update rolls
/// back, so the database keeps the version it had, and the update runs again the
/// next time. Joined to the caller's transaction, the update saves only when that
/// transaction commits, and it fails if that transaction is already set to roll
/// back.
///
/// It returns YES once the database is at the latest version, including when it
/// already was. It returns NO, with the error set, if the update failed. The
/// error is in ``ZENDatabaseErrorDomain``, and for a failed statement, its
/// description has SQLite's message. The error pointer may be NULL.
///
/// Either way, it then raises the counter that ``ZENGetNextObjectIdentifier``
/// uses past the largest identifier in any of the tables, which can hold rows
/// even when the update fails.
- (BOOL)initializeDatabase:(ZENDatabase *)database error:(NSError *_Nullable *_Nullable)outError;

/// Updates a database to the latest version, then moves the identifier counter
/// past its largest identifier, without saying whether the update worked.
///
/// It does what ``initializeDatabase:error:`` does, but a caller can't tell that
/// the update failed, and goes on to use tables the update didn't change.
- (void)initializeDatabase:(ZENDatabase *)database
__attribute__((deprecated("Use initializeDatabase:error:, which says whether the update worked")));

@end

Expand Down
41 changes: 34 additions & 7 deletions CoreZen/Database/DatabaseSchema.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ + (instancetype)schemaWithTableClasses:(NSArray *)tables {
return [[ZENDatabaseSchema alloc] initWithTableClasses:tables];
}

- (void)initializeDatabase:(ZENDatabase *)database {
- (BOOL)initializeDatabase:(ZENDatabase *)database error:(NSError **)outError {
// Update the database schema to the latest version, in a transaction of its
// own unless the caller has one open. A failed statement makes the caller's
// transaction roll back too.
Expand All @@ -48,20 +48,47 @@ - (void)initializeDatabase:(ZENDatabase *)database {
[database beginTransaction];
}
BOOL versionChanged = [self updateDatabaseSchema:database];
NSError *rollbackError = database.rollbackError;
if (rollbackError) {
os_log_error(ZENLogDatabase(), "Rolling back the schema update: %{public}@", rollbackError.localizedDescription);
NSError *updateError = database.rollbackError;
if (updateError) {
os_log_error(ZENLogDatabase(), "Rolling back the schema update: %{public}@", updateError.localizedDescription);
}
if (ownsTransaction) {
if (versionChanged && !rollbackError) {
[database commit];
if (versionChanged && !updateError) {
if (![database commit]) {
NSString *description =
[NSString stringWithFormat:@"Couldn't commit the schema update: %@", database.lastErrorMessage];
os_log_error(ZENLogDatabase(), "%{public}@, so rolling it back", description);
updateError = ZENDatabaseErrorMake(ZENDatabaseErrorCommitFailed, description);
// A failed commit can leave the transaction open, which would swallow
// the queue's next transaction
if (database.inTransaction) {
[database rollback];
}
}
} else {
[database rollback];
}
}
// Initialize the global identifiers from database tables
// Raise the global identifier counter past the tables' rows, which exist even
// when the update failed
[self initializeIdentifiers:database];

if (updateError) {
if (outError) {
*outError = updateError;
}
return NO;
}
return YES;
}

// Deprecated, but still part of the API
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
- (void)initializeDatabase:(ZENDatabase *)database {
[self initializeDatabase:database error:NULL];
}
#pragma clang diagnostic pop

- (NSArray *)tableNames {
NSMutableArray *tableNames = [NSMutableArray array];
Expand Down
7 changes: 7 additions & 0 deletions CoreZenTests/Classes/TestTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@

@end

// Its schema update to version 1 creates its table, then leaves a row whose
// deferred foreign key refers to no row. With foreign keys enforced, the update's
// statements succeed and its commit fails.
@interface ZENUncommittableMigrationTestTable : ZENTestTable

@end

// Reads test_objects like ZENTestTable, but its writes report failure without
// running a statement, as a table with checks of its own might
@interface ZENRefusingTestTable : ZENTestTable
Expand Down
20 changes: 20 additions & 0 deletions CoreZenTests/Classes/TestTable.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ + (BOOL)updateSchema:(ZENDatabase *)database version:(NSUInteger)version {

@end

@implementation ZENUncommittableMigrationTestTable

+ (NSString *)tableName {
return @"uncommittable_migration_objects";
}

+ (BOOL)updateSchema:(ZENDatabase *)database version:(NSUInteger)version {
BOOL updated = [super updateSchema:database version:version];
if (version == 1) {
// SQLite checks a deferred foreign key only at COMMIT
[database executeUpdate:@"CREATE TABLE parents (identifier INTEGER PRIMARY KEY);"];
[database executeUpdate:@"CREATE TABLE orphans "
@"(parent INTEGER REFERENCES parents (identifier) DEFERRABLE INITIALLY DEFERRED);"];
[database executeUpdate:@"INSERT INTO orphans VALUES (-1);"];
}
return updated;
}

@end

@implementation ZENRefusingTestTable

- (BOOL)insertDTO:(ZENDataTransferObject *)dto database:(ZENDatabase *)database {
Expand Down
Loading