refactor: add a migration to introduce SavedPost#userIsAuthor

This commit is contained in:
Harsh Shandilya 2025-07-22 00:03:14 +05:30
parent b0432b848d
commit ef0b06cbd7
6 changed files with 36 additions and 2 deletions

View file

@ -34,6 +34,7 @@ object SavedPostSerializer : KSerializer<SavedPost> {
element<String>("submitterName")
element<List<String>>("tags")
element<String>("description")
element<Boolean>("userIsAuthor")
}
override fun deserialize(decoder: Decoder): SavedPost {
@ -47,6 +48,7 @@ object SavedPostSerializer : KSerializer<SavedPost> {
var submitterName = ""
var tags = emptyList<String>()
var description = ""
var userIsAuthor = false
while (true) {
when (val index = decodeElementIndex(descriptor)) {
0 -> shortId = decodeStringElement(descriptor, 0)
@ -58,6 +60,7 @@ object SavedPostSerializer : KSerializer<SavedPost> {
6 -> submitterName = decodeStringElement(descriptor, 6)
7 -> tags = decodeSerializableElement(descriptor, 7, delegateSerializer)
8 -> description = decodeStringElement(descriptor, 8)
9 -> userIsAuthor = decodeBooleanElement(descriptor, 9)
CompositeDecoder.DECODE_DONE -> break
else -> error("Unexpected index: $index")
}
@ -72,6 +75,7 @@ object SavedPostSerializer : KSerializer<SavedPost> {
submitterName = submitterName,
tags = tags,
description = description,
userIsAuthor = userIsAuthor,
)
}
}
@ -87,6 +91,7 @@ object SavedPostSerializer : KSerializer<SavedPost> {
encodeStringElement(descriptor, 6, value.submitterName)
encodeSerializableElement(descriptor, 7, delegateSerializer, value.tags)
encodeStringElement(descriptor, 8, value.description)
encodeBooleanElement(descriptor, 9, value.userIsAuthor)
}
}
}

View file

@ -1,6 +1,7 @@
import kotlin.Int;
import kotlin.String;
import kotlin.collections.List;
import kotlin.Boolean;
CREATE TABLE IF NOT EXISTS SavedPost(
shortId TEXT NOT NULL PRIMARY KEY,
@ -11,7 +12,8 @@ CREATE TABLE IF NOT EXISTS SavedPost(
commentsUrl TEXT NOT NULL,
submitterName TEXT NOT NULL,
tags TEXT AS List<String> NOT NULL,
description TEXT NOT NULL DEFAULT ""
description TEXT NOT NULL DEFAULT "",
userIsAuthor INTEGER AS Boolean NOT NULL DEFAULT 0
);
insertOrReplacePost:

View file

@ -0,0 +1,25 @@
import kotlin.Int;
import kotlin.String;
import kotlin.collections.List;
import kotlin.Boolean;
ALTER TABLE SavedPost RENAME TO SavedPost_Old;
CREATE TABLE IF NOT EXISTS SavedPost(
shortId TEXT NOT NULL PRIMARY KEY,
title TEXT NOT NULL,
url TEXT NOT NULL,
createdAt TEXT NOT NULL,
commentCount INTEGER AS Int,
commentsUrl TEXT NOT NULL,
submitterName TEXT NOT NULL,
tags TEXT AS List<String> NOT NULL,
description TEXT NOT NULL DEFAULT "",
userIsAuthor INTEGER AS Boolean NOT NULL DEFAULT 0
);
INSERT INTO SavedPost(shortId, title, url, createdAt, commentCount, commentsUrl, submitterName, tags, description, userIsAuthor)
SELECT shortId, title, url, createdAt, commentCount, commentsUrl, submitterName, tags, description, 0
FROM SavedPost_Old;
DROP TABLE SavedPost_Old;

View file

@ -55,6 +55,7 @@ class SavedPostSerializerTest {
tags = listOf("ask", "programming"),
description =
"<p>You suddenly have in your possession a super computer. What comes next? What projects are suddenly possible for you? What performance tests can you now explore?</p>\n",
userIsAuthor = false,
)
}
}

View file

@ -1 +1 @@
{"short_id":"nbigsf","title":"Fun Format Friday: You now have a super computer. What next?","url":"","created_at":"2023-05-04T23:43:50.000-05:00","comment_count":13,"comments_url":"https://lobste.rs/s/nbigsf/fun_format_friday_you_now_have_super","submitter_name":"LenFalken","tags":["ask","programming"],"description":"<p>You suddenly have in your possession a super computer. What comes next? What projects are suddenly possible for you? What performance tests can you now explore?</p>\n"}
{"short_id":"nbigsf","title":"Fun Format Friday: You now have a super computer. What next?","url":"","created_at":"2023-05-04T23:43:50.000-05:00","comment_count":13,"comments_url":"https://lobste.rs/s/nbigsf/fun_format_friday_you_now_have_super","submitter_name":"LenFalken","tags":["ask","programming"],"description":"<p>You suddenly have in your possession a super computer. What comes next? What projects are suddenly possible for you? What performance tests can you now explore?</p>\n","user_is_author":false}

View file

@ -124,6 +124,7 @@ class SavedPostQueriesTest {
submitterName = "test_user_$i",
tags = listOf(),
description = "",
userIsAuthor = i % 2 == 0,
)
posts.add(post)