Repository navigation
Persist independent member profiles and migrate SQLite to schema9 #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
efraespada
merged 3 commits into
develop
from
feature/21-complete-the-verified-household-administration-workspace-after-the-profile-model-foundation
Nov 2, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
a9c4b97
docs(sdd): specify issue contract in specs/household-administration-i…
github-actions[bot] e129e76
feat: Persist independent member profiles with schema9 migration
efraespada 4e59e04
docs: Correct the generated owner blob citation and record verified r…
efraespada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| """Scoped profile storage; application authorization precedes use of either repository.""" | ||
|
|
||
| from typing import Protocol | ||
|
|
||
| from domus.domain.member_profiles import MemberPreferences, MemberPresentation | ||
|
|
||
|
|
||
| class MemberPresentations(Protocol): | ||
| def get(self, household_id: str, member_id: str) -> MemberPresentation | None: ... | ||
| def save(self, presentation: MemberPresentation, expected_revision: int) -> None: ... | ||
|
|
||
|
|
||
| class MemberPreferencesRepository(Protocol): | ||
| def get(self, household_id: str, member_id: str) -> MemberPreferences | None: ... | ||
| def save(self, preferences: MemberPreferences, expected_revision: int) -> None: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
backend/src/domus/infrastructure/sqlite_member_profile_schema.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| """Schema9 adds separate public/private member records without rewriting historical facts.""" | ||
|
|
||
| PROFILE_TABLES = {"member_presentations", "member_preferences"} | ||
| PROFILE_STATEMENTS = ( | ||
| "CREATE TABLE member_presentations(member_id TEXT PRIMARY KEY NOT NULL, " | ||
| "household_id TEXT NOT NULL, alias TEXT, " | ||
| "revision INTEGER NOT NULL CHECK(typeof(revision)='integer' AND revision>=1), " | ||
| "FOREIGN KEY(household_id,member_id) REFERENCES members(household_id,member_id), " | ||
| "CHECK(alias IS NULL OR (typeof(alias)='text' AND length(alias) BETWEEN 1 AND 80)))", | ||
| "CREATE TABLE member_preferences(member_id TEXT PRIMARY KEY NOT NULL, " | ||
| "household_id TEXT NOT NULL, locale TEXT CHECK(locale IS NULL OR locale IN ('en','es')), " | ||
| "routine_participation_summaries INTEGER NOT NULL " | ||
| "CHECK(typeof(routine_participation_summaries)='integer' " | ||
| "AND routine_participation_summaries IN (0,1)), " | ||
| "revision INTEGER NOT NULL CHECK(typeof(revision)='integer' AND revision>=1), " | ||
| "FOREIGN KEY(household_id,member_id) REFERENCES members(household_id,member_id))", | ||
| "INSERT INTO member_presentations(member_id,household_id,alias,revision) " | ||
| "SELECT member_id,household_id,NULL,1 FROM members", | ||
| "INSERT INTO member_preferences(member_id,household_id,locale," | ||
| "routine_participation_summaries,revision) SELECT member_id,household_id,NULL,1,1 FROM members", | ||
| ) | ||
138 changes: 138 additions & 0 deletions
138
backend/src/domus/infrastructure/sqlite_member_profiles.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| """Independent scoped profile revisions, strict decoding and atomic member initialization.""" | ||
|
|
||
| import sqlite3 | ||
| from dataclasses import dataclass | ||
| from typing import Literal, cast | ||
|
|
||
| from domus.domain.errors import Conflict, DomusError, StorageFailure | ||
| from domus.domain.identity import Member, validate_revision | ||
| from domus.domain.member_profiles import MemberLocale, MemberPreferences, MemberPresentation | ||
|
|
||
|
|
||
| def initialize_member_profiles(connection: sqlite3.Connection, member: Member) -> None: | ||
| presentation = MemberPresentation(member.member_id, member.household_id) | ||
| preferences = MemberPreferences(member.member_id, member.household_id) | ||
| connection.execute( | ||
| "INSERT INTO member_presentations(member_id,household_id,alias,revision) VALUES (?,?,?,?)", | ||
| ( | ||
| presentation.member_id, | ||
| presentation.household_id, | ||
| presentation.alias, | ||
| presentation.revision, | ||
| ), | ||
| ) | ||
| connection.execute( | ||
| "INSERT INTO member_preferences(member_id,household_id,locale," | ||
| "routine_participation_summaries,revision) VALUES (?,?,?,?,?)", | ||
| ( | ||
| preferences.member_id, | ||
| preferences.household_id, | ||
| preferences.locale, | ||
| int(preferences.routine_participation_summaries), | ||
| preferences.revision, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def _row( | ||
| connection: sqlite3.Connection, | ||
| table: Literal["member_presentations", "member_preferences"], | ||
| household_id: str, | ||
| member_id: str, | ||
| ) -> sqlite3.Row | None: | ||
| # A missing member is absence; a missing profile for an existing member is corruption. | ||
| return cast( | ||
| sqlite3.Row | None, | ||
| connection.execute( | ||
| f"SELECT p.* FROM members m LEFT JOIN {table} p " | ||
| "ON p.household_id=m.household_id AND p.member_id=m.member_id " | ||
| "WHERE m.household_id=? AND m.member_id=?", | ||
| (household_id, member_id), | ||
| ).fetchone(), | ||
| ) | ||
|
|
||
|
|
||
| def _revision(revision: int, expected: int) -> None: | ||
| validate_revision(expected) | ||
| if revision != expected + 1: | ||
| raise Conflict("revision_conflict") | ||
|
|
||
|
|
||
| @dataclass | ||
| class SQLitePresentations: | ||
| connection: sqlite3.Connection | ||
|
|
||
| def get(self, household_id: str, member_id: str) -> MemberPresentation | None: | ||
| row = _row(self.connection, "member_presentations", household_id, member_id) | ||
| if row is None: | ||
| return None | ||
| try: | ||
| value = MemberPresentation( | ||
| row["member_id"], row["household_id"], row["alias"], row["revision"] | ||
| ) | ||
| if value.alias != row["alias"]: | ||
| raise ValueError("Noncanonical stored alias") | ||
| return value | ||
| except DomusError, ValueError: | ||
| raise StorageFailure("invalid_member_profile_record") from None | ||
|
|
||
| def save(self, presentation: MemberPresentation, expected_revision: int) -> None: | ||
| _revision(presentation.revision, expected_revision) | ||
| if ( | ||
| self.connection.execute( | ||
| "UPDATE member_presentations SET alias=?,revision=? " | ||
| "WHERE household_id=? AND member_id=? AND revision=?", | ||
| ( | ||
| presentation.alias, | ||
| presentation.revision, | ||
| presentation.household_id, | ||
| presentation.member_id, | ||
| expected_revision, | ||
| ), | ||
| ).rowcount | ||
| != 1 | ||
| ): | ||
| raise Conflict("revision_conflict") | ||
|
|
||
|
|
||
| @dataclass | ||
| class SQLitePreferences: | ||
| connection: sqlite3.Connection | ||
|
|
||
| def get(self, household_id: str, member_id: str) -> MemberPreferences | None: | ||
| row = _row(self.connection, "member_preferences", household_id, member_id) | ||
| if row is None: | ||
| return None | ||
| try: | ||
| flag = row["routine_participation_summaries"] | ||
| if type(flag) is not int or flag not in (0, 1): | ||
| raise ValueError("Invalid stored preference flag") | ||
| return MemberPreferences( | ||
| row["member_id"], | ||
| row["household_id"], | ||
| None if row["locale"] is None else MemberLocale(row["locale"]), | ||
| flag == 1, | ||
| row["revision"], | ||
| ) | ||
| except DomusError, ValueError: | ||
| raise StorageFailure("invalid_member_profile_record") from None | ||
|
|
||
| def save(self, preferences: MemberPreferences, expected_revision: int) -> None: | ||
| _revision(preferences.revision, expected_revision) | ||
| if ( | ||
| self.connection.execute( | ||
| "UPDATE member_preferences SET locale=?,routine_participation_summaries=?," | ||
| "revision=? " | ||
| "WHERE household_id=? AND member_id=? AND revision=?", | ||
| ( | ||
| preferences.locale, | ||
| int(preferences.routine_participation_summaries), | ||
| preferences.revision, | ||
| preferences.household_id, | ||
| preferences.member_id, | ||
| expected_revision, | ||
| ), | ||
| ).rowcount | ||
| != 1 | ||
| ): | ||
| raise Conflict("revision_conflict") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.