Skip to content

Commit

Permalink
Migrate all id from Int to String
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Jun 3, 2024
1 parent 4da8529 commit a658d42
Show file tree
Hide file tree
Showing 23 changed files with 208 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.MenuOpen
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.MenuOpen
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Icon
Expand Down Expand Up @@ -148,7 +148,7 @@ private fun DrawerIcon(onDrawerMenuClick: () -> Unit, isDrawerOpen: Boolean) {
) {
Icon(
imageVector = if (isDrawerOpen) {
Icons.Default.MenuOpen
Icons.AutoMirrored.Filled.MenuOpen
} else {
Icons.Default.Menu
},
Expand All @@ -164,7 +164,7 @@ private fun HomeAppBarPreview() {
HomeAppBar(
currentFeedFilter = FeedFilter.Source(
feedSource = FeedSource(
id = 0,
id = "0",
url = "",
title = "A very very very very very very long title",
category = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ data class CategoriesState(
) {

data class CategoryItem(
val id: Long,
val id: String,
val name: String?,
val isSelected: Boolean,
val onClick: (CategoryId) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.prof18.feedflow.core.model

data class CategoryId(
val value: Long,
val value: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.compose.runtime.Immutable

@Immutable
data class FeedItem(
val id: Int,
val id: String,
val url: String,
val title: String?,
val subtitle: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.prof18.feedflow.core.model

data class FeedItemId(
val id: Int,
val id: String,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.prof18.feedflow.core.model

data class FeedItemUrlInfo(
val id: Int,
val id: String,
val url: String,
val title: String?,
val openOnlyOnBrowser: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.prof18.feedflow.core.model

data class FeedSource(
val id: Int,
val id: String,
val url: String,
val title: String,
val category: FeedSourceCategory?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.prof18.feedflow.core.model

data class FeedSourceCategory(
val id: Long,
val id: String,
val title: String,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.prof18.feedflow.core.model

data class ParsedFeedSource(
val id: String,
val url: String,
val title: String,
val categoryName: CategoryName?,
Expand Down Expand Up @@ -29,6 +30,7 @@ data class ParsedFeedSource(
return null
}
return ParsedFeedSource(
id = url.hashCode().toString(),
url = url!!,
title = title!!,
categoryName = if (category != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.prof18.feedflow.database

import app.cash.sqldelight.Transacter
import app.cash.sqldelight.TransactionWithoutReturn
import app.cash.sqldelight.adapter.primitive.IntColumnAdapter
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.coroutines.mapToOneOrDefault
Expand All @@ -16,8 +15,6 @@ import com.prof18.feedflow.core.model.FeedSource
import com.prof18.feedflow.core.model.FeedSourceCategory
import com.prof18.feedflow.core.model.ParsedFeedSource
import com.prof18.feedflow.db.FeedFlowDB
import com.prof18.feedflow.db.Feed_item
import com.prof18.feedflow.db.Feed_source
import com.prof18.feedflow.db.Search
import com.prof18.feedflow.db.SelectFeedUrls
import com.prof18.feedflow.db.SelectFeeds
Expand All @@ -34,16 +31,7 @@ class DatabaseHelper(
private val backgroundDispatcher: CoroutineDispatcher,
private val logger: Logger,
) {
private val dbRef: FeedFlowDB = FeedFlowDB(
sqlDriver,
feed_itemAdapter = Feed_item.Adapter(
url_hashAdapter = IntColumnAdapter,
feed_source_idAdapter = IntColumnAdapter,
),
feed_sourceAdapter = Feed_source.Adapter(
url_hashAdapter = IntColumnAdapter,
),
)
private val dbRef: FeedFlowDB = FeedFlowDB(sqlDriver)

suspend fun getFeedSources(): List<FeedSource> = withContext(backgroundDispatcher) {
dbRef.feedSourceQueries
Expand Down Expand Up @@ -86,7 +74,10 @@ class DatabaseHelper(
suspend fun insertCategories(categories: List<CategoryName>) =
dbRef.transactionWithContext(backgroundDispatcher) {
categories.forEach { category ->
dbRef.feedSourceCategoryQueries.insertFeedSourceCategory(category.name)
dbRef.feedSourceCategoryQueries.insertFeedSourceCategory(
id = category.name.hashCode().toString(),
title = category.name,
)
}
}

Expand All @@ -95,15 +86,15 @@ class DatabaseHelper(
feedSource.forEach { feedSource ->
if (feedSource.categoryName != null) {
dbRef.feedSourceQueries.insertFeedSource(
url_hash = feedSource.hashCode(),
url_hash = feedSource.hashCode().toString(),
url = feedSource.url,
title = feedSource.title,
title_ = feedSource.categoryName?.name.toString(),
logo_url = feedSource.logoUrl,
)
} else {
dbRef.feedSourceQueries.insertFeedSourceWithNoCategory(
url_hash = feedSource.hashCode(),
url_hash = feedSource.id,
url = feedSource.url,
title = feedSource.title,
logo_url = feedSource.logoUrl,
Expand Down Expand Up @@ -226,7 +217,7 @@ class DatabaseHelper(
.mapToOneOrDefault(0, backgroundDispatcher)
.flowOn(backgroundDispatcher)

suspend fun deleteCategory(id: Long) =
suspend fun deleteCategory(id: String) =
dbRef.transactionWithContext(backgroundDispatcher) {
dbRef.feedSourceQueries.resetCategory(categoryId = id)
dbRef.feedSourceCategoryQueries.delete(id = id)
Expand All @@ -240,7 +231,7 @@ class DatabaseHelper(
)
}

suspend fun updateFeedSourceName(feedSourceId: Int, newName: String) =
suspend fun updateFeedSourceName(feedSourceId: String, newName: String) =
dbRef.transactionWithContext(backgroundDispatcher) {
dbRef.feedSourceQueries.updateFeedSourceTitle(
title = newName,
Expand All @@ -267,7 +258,7 @@ class DatabaseHelper(
}
}

private fun FeedFilter.getFeedSourceId(): Int? {
private fun FeedFilter.getFeedSourceId(): String? {
return when (this) {
is FeedFilter.Source -> feedSource.id

Expand All @@ -279,7 +270,7 @@ class DatabaseHelper(
}
}

private fun FeedFilter.getCategoryId(): Long? {
private fun FeedFilter.getCategoryId(): String? {
return when (this) {
is FeedFilter.Category -> feedCategory.id

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import kotlin.Boolean;
import kotlin.Int;

CREATE TABLE feed_item (
url_hash INTEGER AS Int NOT NULL PRIMARY KEY,
url_hash TEXT NOT NULL PRIMARY KEY, -- New 'url_hash' as TEXT
url TEXT NOT NULL,
title TEXT,
subtitle TEXT,
content TEXT,
image_url TEXT,
feed_source_id INTEGER AS Int NOT NULL,
feed_source_id TEXT NOT NULL, -- Updated 'feed_source_id' to TEXT
is_read INTEGER AS Boolean NOT NULL DEFAULT 0,
is_bookmarked INTEGER AS Boolean NOT NULL DEFAULT 0,
pub_date INTEGER,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import kotlin.Int;

CREATE VIRTUAL TABLE IF NOT EXISTS feed_search USING FTS4(
url_hash INTEGER AS Int NOT NULL PRIMARY KEY,
url_hash TEXT NOT NULL PRIMARY KEY,
title TEXT,
subtitle TEXT,
tokenize="unicode61"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import kotlin.Int;

CREATE TABLE feed_source (
url_hash INTEGER AS Int NOT NULL PRIMARY KEY,
url_hash TEXT NOT NULL PRIMARY KEY,
url TEXT NOT NULL,
title TEXT NOT NULL,
category_id INTEGER,
category_id TEXT,
last_sync_timestamp INTEGER,
logo_url TEXT
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
CREATE TABLE feed_source_category (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL PRIMARY KEY,
title TEXT NOT NULL UNIQUE
);

insertFeedSourceCategory:
INSERT OR IGNORE INTO feed_source_category(title)
VALUES (?);
INSERT OR IGNORE INTO feed_source_category(id, title)
VALUES (?,?);

getCategoryByName:
SELECT * FROM feed_source_category WHERE title = ?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
-------- FEED SOURCE CATEGORY --------
-- Migration of category to change 'id' to a string type (without autoincrement)
CREATE TABLE new_feed_source_category (
id TEXT NOT NULL PRIMARY KEY,
title TEXT NOT NULL UNIQUE
);

-- Copy existing data
INSERT INTO new_feed_source_category (id, title)
SELECT
CAST(id AS TEXT),
title
FROM feed_source_category;

-- Drop old table
DROP TABLE feed_source_category;

-- Rename new table to original name
ALTER TABLE new_feed_source_category RENAME TO feed_source_category;

-------- FEED SOURCE --------
-- Drop existing triggers (as they reference the old table structure)
DROP TRIGGER IF EXISTS populate_feed_search;
DROP TRIGGER IF EXISTS delete_feed_search;

-- Migration to change 'url_hash' to a string type in 'feed_source'
CREATE TABLE new_feed_source (
url_hash TEXT NOT NULL PRIMARY KEY,
url TEXT NOT NULL,
title TEXT NOT NULL,
category_id TEXT, -- Assuming 'category_id' is now TEXT from previous migration
last_sync_timestamp INTEGER,
logo_url TEXT
);

-- Copy existing data, converting url_hash to TEXT
INSERT INTO new_feed_source (url_hash, url, title, category_id, last_sync_timestamp, logo_url)
SELECT
CAST(url_hash AS TEXT),
url,
title,
CAST(category_id AS TEXT),
last_sync_timestamp,
logo_url
FROM feed_source;

-- Drop old table
DROP TABLE feed_source;

-- Rename new table to original name
ALTER TABLE new_feed_source RENAME TO feed_source;

-------- FEED ITEM --------
-- Migration to change 'url_hash' to a string type
CREATE TABLE new_feed_item (
url_hash TEXT NOT NULL PRIMARY KEY, -- New 'url_hash' as TEXT
url TEXT NOT NULL,
title TEXT,
subtitle TEXT,
content TEXT,
image_url TEXT,
feed_source_id TEXT NOT NULL, -- Updated 'feed_source_id' to TEXT
is_read INTEGER AS Boolean NOT NULL DEFAULT 0,
is_bookmarked INTEGER AS Boolean NOT NULL DEFAULT 0,
pub_date INTEGER,
comments_url TEXT
);

-- Copy existing data, converting url_hash to TEXT
INSERT INTO new_feed_item (url_hash, url, title, subtitle, content, image_url, feed_source_id, is_read, is_bookmarked, pub_date, comments_url)
SELECT
CAST(url_hash AS TEXT), -- Directly convert url_hash to strings (no NULL check needed)
url,
title,
subtitle,
content,
image_url,
CAST(feed_source_id AS TEXT), -- Convert feed_source_id to TEXT
is_read,
is_bookmarked,
pub_date,
comments_url
FROM feed_item;

-- Drop old table
DROP TABLE feed_item;

-- Rename new table to original name
ALTER TABLE new_feed_item RENAME TO feed_item;


-------- FEED SEARCH --------
-- Version 1.3
-- Migration to change 'url_hash' to a string type in 'feed_search' and update triggers

DROP TABLE IF EXISTS feed_search;

-- Recreate the FTS4 table with TEXT url_hash and a temporary name
CREATE VIRTUAL TABLE IF NOT EXISTS feed_search USING FTS4(
url_hash TEXT NOT NULL PRIMARY KEY,
title TEXT,
subtitle TEXT,
tokenize="unicode61"
);

-- Populate the new table (converting url_hash to TEXT)
INSERT INTO feed_search (url_hash, title, subtitle)
SELECT
url_hash,
title,
subtitle
FROM feed_item;

-- Recreate triggers with updated references
CREATE TRIGGER populate_feed_search
AFTER INSERT ON feed_item
BEGIN
INSERT OR IGNORE INTO feed_search(url_hash, title, subtitle) VALUES (new.url_hash, new.title, new.subtitle);
END;

CREATE TRIGGER delete_feed_search
BEFORE DELETE ON feed_item
BEGIN
DELETE FROM feed_search WHERE url_hash = old.url_hash;
END;
Binary file not shown.
2 changes: 1 addition & 1 deletion iosApp/Source/Settings/AddFeed/AddFeedScreenContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct AddFeedScreenContent: View {

let showCloseButton: Bool
let updateFeedUrlTextFieldValue: (String) -> Void
let deleteCategory: (Int64) -> Void
let deleteCategory: (String) -> Void
let addNewCategory: (CategoryName) -> Void
let addFeed: () -> Void

Expand Down
Loading

0 comments on commit a658d42

Please sign in to comment.