Skip to content

Commit a504803

Browse files
Rewrite WCCustomerModel to use composite primary key
1 parent 763ea79 commit a504803

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

libs/fluxc-plugin/schemas/org.wordpress.android.fluxc.persistence.WCAndroidDatabase/72.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"formatVersion": 1,
33
"database": {
44
"version": 72,
5-
"identityHash": "ac32e20c89d1549b8a1f06ac715177e0",
5+
"identityHash": "e94f8b70f90756ca710f86422b5924d6",
66
"entities": [
77
{
88
"tableName": "AddonEntity",
@@ -3022,7 +3022,7 @@
30223022
},
30233023
{
30243024
"tableName": "CustomerEntity",
3025-
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`stableId` TEXT NOT NULL, `localSiteId` INTEGER NOT NULL, `remoteCustomerId` INTEGER NOT NULL, `avatarUrl` TEXT NOT NULL, `dateCreated` TEXT NOT NULL, `dateCreatedGmt` TEXT NOT NULL, `dateModified` TEXT NOT NULL, `dateModifiedGmt` TEXT NOT NULL, `email` TEXT NOT NULL, `firstName` TEXT NOT NULL, `isPayingCustomer` INTEGER NOT NULL, `lastName` TEXT NOT NULL, `role` TEXT NOT NULL, `username` TEXT NOT NULL, `billingAddress1` TEXT NOT NULL, `billingAddress2` TEXT NOT NULL, `billingCity` TEXT NOT NULL, `billingCompany` TEXT NOT NULL, `billingCountry` TEXT NOT NULL, `billingEmail` TEXT NOT NULL, `billingFirstName` TEXT NOT NULL, `billingLastName` TEXT NOT NULL, `billingPhone` TEXT NOT NULL, `billingPostcode` TEXT NOT NULL, `billingState` TEXT NOT NULL, `shippingAddress1` TEXT NOT NULL, `shippingAddress2` TEXT NOT NULL, `shippingCity` TEXT NOT NULL, `shippingCompany` TEXT NOT NULL, `shippingCountry` TEXT NOT NULL, `shippingFirstName` TEXT NOT NULL, `shippingLastName` TEXT NOT NULL, `shippingPostcode` TEXT NOT NULL, `shippingState` TEXT NOT NULL, `analyticsCustomerId` INTEGER, PRIMARY KEY(`stableId`))",
3025+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`stableId` TEXT NOT NULL, `localSiteId` INTEGER NOT NULL, `remoteCustomerId` INTEGER NOT NULL, `avatarUrl` TEXT NOT NULL, `dateCreated` TEXT NOT NULL, `dateCreatedGmt` TEXT NOT NULL, `dateModified` TEXT NOT NULL, `dateModifiedGmt` TEXT NOT NULL, `email` TEXT NOT NULL, `firstName` TEXT NOT NULL, `isPayingCustomer` INTEGER NOT NULL, `lastName` TEXT NOT NULL, `role` TEXT NOT NULL, `username` TEXT NOT NULL, `billingAddress1` TEXT NOT NULL, `billingAddress2` TEXT NOT NULL, `billingCity` TEXT NOT NULL, `billingCompany` TEXT NOT NULL, `billingCountry` TEXT NOT NULL, `billingEmail` TEXT NOT NULL, `billingFirstName` TEXT NOT NULL, `billingLastName` TEXT NOT NULL, `billingPhone` TEXT NOT NULL, `billingPostcode` TEXT NOT NULL, `billingState` TEXT NOT NULL, `shippingAddress1` TEXT NOT NULL, `shippingAddress2` TEXT NOT NULL, `shippingCity` TEXT NOT NULL, `shippingCompany` TEXT NOT NULL, `shippingCountry` TEXT NOT NULL, `shippingFirstName` TEXT NOT NULL, `shippingLastName` TEXT NOT NULL, `shippingPostcode` TEXT NOT NULL, `shippingState` TEXT NOT NULL, `analyticsCustomerId` INTEGER, PRIMARY KEY(`localSiteId`, `stableId`))",
30263026
"fields": [
30273027
{
30283028
"fieldPath": "stableId",
@@ -3237,6 +3237,7 @@
32373237
"primaryKey": {
32383238
"autoGenerate": false,
32393239
"columnNames": [
3240+
"localSiteId",
32403241
"stableId"
32413242
]
32423243
}
@@ -4310,7 +4311,7 @@
43104311
],
43114312
"setupQueries": [
43124313
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
4313-
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'ac32e20c89d1549b8a1f06ac715177e0')"
4314+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'e94f8b70f90756ca710f86422b5924d6')"
43144315
]
43154316
}
43164317
}

libs/fluxc-plugin/src/androidTest/java/org/wordpress/android/fluxc/persistence/MigrationTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class MigrationTests {
440440
val remoteId1 = cursor.getLong(2)
441441
val analyticsId1 = if (!cursor.isNull(3)) cursor.getLong(3) else null
442442
val username1 = cursor.getString(4)
443-
assertThat(stableId1).isEqualTo("site:10|analytics:555")
443+
assertThat(stableId1).isEqualTo("analytics:555")
444444
assertThat(siteId1).isEqualTo(10)
445445
assertThat(remoteId1).isEqualTo(0)
446446
assertThat(analyticsId1).isEqualTo(555)
@@ -453,7 +453,7 @@ class MigrationTests {
453453
val remoteId2 = cursor.getLong(2)
454454
val analyticsId2 = if (!cursor.isNull(3)) cursor.getLong(3) else null
455455
val username2 = cursor.getString(4)
456-
assertThat(stableId2).isEqualTo("site:10|wp:123")
456+
assertThat(stableId2).isEqualTo("wp:123")
457457
assertThat(siteId2).isEqualTo(10)
458458
assertThat(remoteId2).isEqualTo(123)
459459
assertThat(analyticsId2).isNull()

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerModel.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package org.wordpress.android.fluxc.model.customer
22

33
import androidx.room.Entity
4-
import androidx.room.PrimaryKey
54
import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId
65
import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId
76

87
/**
98
* Single Woo customer - see https://woocommerce.github.io/woocommerce-rest-api-docs/#customer-properties
109
*/
1110
@ConsistentCopyVisibility
12-
@Entity(tableName = "CustomerEntity")
11+
@Entity(
12+
tableName = "CustomerEntity",
13+
primaryKeys = ["localSiteId", "stableId"]
14+
)
1315
data class WCCustomerModel internal constructor(
1416
/**
1517
* Deterministic primary key used to uniquely identify a customer across refreshes.
1618
*
1719
* Format examples:
18-
* - "site:<localSiteId>|wp:<remoteCustomerId>" for registered users (remote > 0)
19-
* - "site:<localSiteId>|analytics:<analyticsCustomerId>" for analytics guests
20+
* - "wp:<remoteCustomerId>" for registered users (remote > 0)
21+
* - "analytics:<analyticsCustomerId>" for analytics guests
2022
*/
21-
@PrimaryKey val stableId: String = "",
23+
val stableId: String = "",
2224
val localSiteId: LocalId = LocalId(0),
2325
val remoteCustomerId: RemoteId = RemoteId(0),
2426
val avatarUrl: String = "",
@@ -93,7 +95,7 @@ data class WCCustomerModel internal constructor(
9395
shippingState: String = "",
9496
analyticsCustomerId: Long? = 0,
9597
) : this(
96-
stableId = buildStableId(localSiteId.value, remoteCustomerId.value, analyticsCustomerId),
98+
stableId = buildStableId(remoteCustomerId.value, analyticsCustomerId),
9799
localSiteId = localSiteId,
98100
remoteCustomerId = remoteCustomerId,
99101
avatarUrl = avatarUrl,
@@ -135,21 +137,21 @@ data class WCCustomerModel internal constructor(
135137
* Builds deterministic primary key used to uniquely identify a customer across refreshes.
136138
*
137139
* Format examples:
138-
* - "site:<localSiteId>|wp:<remoteCustomerId>" for registered users (remote > 0)
139-
* - "site:<localSiteId>|analytics:<analyticsCustomerId>" for analytics guests
140+
* - "wp:<remoteCustomerId>" for registered users (remote > 0)
141+
* - "analytics:<analyticsCustomerId>" for analytics guests
140142
*
141143
* @param siteId local site id
142144
* @param remoteId remote customer id
143145
* @param analyticsId analytics customer id
144146
* @return stable id
145147
*/
146-
private fun buildStableId(siteId: Int, remoteId: Long?, analyticsId: Long?): String {
148+
private fun buildStableId(remoteId: Long?, analyticsId: Long?): String {
147149
val rid = remoteId ?: 0L
148150
return if (rid > 0L) {
149-
"site:$siteId|wp:$rid"
151+
"wp:$rid"
150152
} else {
151153
val aid = analyticsId ?: 0L
152-
if (aid > 0L) "site:$siteId|analytics:$aid" else ""
154+
if (aid > 0L) "analytics:$aid" else ""
153155
}
154156
}
155157
}

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/persistence/migrations/Migrations.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ internal val MIGRATION_71_72 = object : Migration(71, 72) {
11281128
`shippingPostcode` TEXT NOT NULL,
11291129
`shippingState` TEXT NOT NULL,
11301130
`analyticsCustomerId` INTEGER,
1131-
PRIMARY KEY(`stableId`)
1131+
PRIMARY KEY(`localSiteId`, `stableId`)
11321132
)
11331133
""".trimIndent()
11341134
)
@@ -1149,8 +1149,8 @@ internal val MIGRATION_71_72 = object : Migration(71, 72) {
11491149
SELECT stableId, MAX(id) AS keepId FROM (
11501150
SELECT `id`,
11511151
CASE
1152-
WHEN `remoteCustomerId` > 0 THEN 'site:' || `localSiteId` || '|wp:' || `remoteCustomerId`
1153-
WHEN `analyticsCustomerId` IS NOT NULL AND `analyticsCustomerId` > 0 THEN 'site:' || `localSiteId` || '|analytics:' || `analyticsCustomerId`
1152+
WHEN `remoteCustomerId` > 0 THEN 'wp:' || `remoteCustomerId`
1153+
WHEN `analyticsCustomerId` IS NOT NULL AND `analyticsCustomerId` > 0 THEN 'analytics:' || `analyticsCustomerId`
11541154
END AS `stableId`
11551155
FROM `CustomerEntity`
11561156
WHERE `remoteCustomerId` > 0 OR (`analyticsCustomerId` IS NOT NULL AND `analyticsCustomerId` > 0)

0 commit comments

Comments
 (0)