Skip to content

Commit e817ff0

Browse files
committed
fix: keep the sign-out watermark monotonic and drop the session foreign key
Backing the watermark off by 26 hours to stay clear of time-zone skew also moved it backwards, which would re-validate tokens somebody had deliberately invalidated in that window by changing or resetting their password. It now takes the later of the two values, so it can only ever move forward. The foreign key on user_session.user_account_id is gone. Hibernate cannot see it - the column is plain by design, because the row is written by native upserts on the auth hot path - so the migration check kept generating a changeset to drop it. It also turned emitting a token into something that requires the account row to be visible to the current transaction, which broke TranslationsControllerHistoryTest. Orphans age out with the purge, the same way activity_revision.author_id is handled.
1 parent 37fa414 commit e817ff0

2 files changed

Lines changed: 5 additions & 8 deletions

File tree

backend/data/src/main/kotlin/io/tolgee/model/UserSession.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ class UserSession : StandardAuditModel() {
3535
lateinit var deviceId: String
3636

3737
/**
38-
* Plain column rather than a relation - the row is written by native upserts on the auth hot path.
38+
* Plain column rather than a relation - the row is written by native upserts on the auth hot path,
39+
* and no foreign key backs it: emitting a token must not require the account row to be visible to
40+
* this transaction yet. Orphans age out with the purge, the same way `activity_revision.author_id`
41+
* is handled.
3942
*/
4043
@Column(name = "user_account_id", nullable = false)
4144
var userAccountId: Long = 0

backend/data/src/main/resources/db/changelog/schema.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5883,12 +5883,6 @@
58835883
<column name="expires_at"/>
58845884
</createIndex>
58855885
</changeSet>
5886-
<changeSet author="jancizmar" id="1785400000000-5">
5887-
<addForeignKeyConstraint baseColumnNames="user_account_id" baseTableName="user_session"
5888-
constraintName="fk_user_session_user_account" deferrable="false"
5889-
initiallyDeferred="false" onDelete="CASCADE" referencedColumnNames="id"
5890-
referencedTableName="user_account" validate="true"/>
5891-
</changeSet>
58925886
<changeSet author="jancizmar" id="1785400000000-6">
58935887
<createTable tableName="auth_audit_event">
58945888
<column name="id" type="BIGINT">
@@ -5941,7 +5935,7 @@
59415935
until real time caught up. Erring the other way only leaves the newest pre-upgrade
59425936
tokens valid, and those get a session row from the backfill path on first use anyway.
59435937
</comment>
5944-
<sql>UPDATE user_account SET tokens_valid_not_before = now() - interval '26 hours' WHERE deleted_at IS NULL;</sql>
5938+
<sql>UPDATE user_account SET tokens_valid_not_before = greatest(coalesce(tokens_valid_not_before, to_timestamp(0)), now() - interval '26 hours') WHERE deleted_at IS NULL;</sql>
59455939
<rollback/>
59465940
</changeSet>
59475941
<changeSet author="jancizmar" id="1785400000000-11">

0 commit comments

Comments
 (0)