Skip to content

Conversation

@groue
Copy link
Owner

@groue groue commented Sep 8, 2025

This pull request enhances support for record types that target a database view. It is now possible to instruct GRDB about the primary key of a view, and this unlocks both reads and writes that used to throw a database error.

See View Records.

// A record that targets the "captain" database view:
struct Captain: Codable, Identifiable, FetchableRecord, PersistableRecord {
    var id: String
    var name: String
    var teamId: String
}

// Now possible:
var bob = try Captain.find(db, id: "bob")
try captain.updateChanges(db) {
    $0.name = "Bobby"
    $0.teamId = "red"
}

Original discussion: #1809

@groue groue force-pushed the dev/persistable-views branch 2 times, most recently from 56741ee to 3e1a711 Compare September 10, 2025 18:54
@stevensJourney
Copy link

Thanks for the awesome work @groue! I've tested this for PowerSync on our end with some basic queries. We're still ironing out some unrelated kinks in our PowerSync integration, but so far this works perfectly for us.

@groue
Copy link
Owner Author

groue commented Sep 22, 2025

Thank you for the good news, @stevensJourney! 👍 Unless you tell me otherwise, I'll merge this PR shortly!

This is also a big cleanup opportunity.

We need public schema identifiers in order to support the customization of schema introspection.
This method is necessary for the customization of schema introspection: it must be possible to fallback on the primitive SQLite schema introspection when needed.
Database.changesCount calls sqlite3_changes(), whose documentation says:

> https://sqlite.org/c3ref/changes.html
> Changes to a view that are intercepted by INSTEAD OF triggers are not counted.

Since we want to support INSTEAD OF triggers well, so that record types can be based on views, we switch to sqlite3_total_changes().
When GRDB eventually adds methods to the DatabaseSchemaSource protocol, our chained schema source will implement chaining on those new methods.

When users write their own custom chaining schema source, the compiler will not tell them that they have to chain the new methods when they upgrade GRDB. The built-in (and correct) implementation of chaining helps avoiding this scenario.
@groue groue force-pushed the dev/persistable-views branch from 3e1a711 to 9ef5e2c Compare September 23, 2025 15:19
@groue groue merged commit a0e101e into development Sep 23, 2025
8 checks passed
@groue groue deleted the dev/persistable-views branch September 23, 2025 15:38
@groue
Copy link
Owner Author

groue commented Sep 23, 2025

⛵ Shipped in v7.7.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants