Skip to content

Commit

Permalink
Merge pull request #114 from afadil/portfolio-refactoring
Browse files Browse the repository at this point in the history
Portfolio refactoring
  • Loading branch information
afadil committed Sep 23, 2024
2 parents f771dff + 484362a commit 1c1ed34
Show file tree
Hide file tree
Showing 117 changed files with 4,208 additions and 3,260 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wealthfolio-app",
"private": true,
"version": "1.0.13",
"version": "1.0.14",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
41 changes: 39 additions & 2 deletions src-core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "wealthfolio_core"
version = "1.0.11"
description = "Portfolio tracker"
authors = ["Aziz Fadil"]
license = "MIT"
license = "LGPL-3.0"
repository = ""
edition = "2021"

Expand All @@ -12,7 +12,7 @@ edition = "2021"
[dependencies]
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
diesel = { version = "2.2.4", features = ["sqlite", "chrono", "numeric", "returning_clauses_for_sqlite_3_35"] }
diesel = { version = "2.2.4", features = ["sqlite", "chrono", "r2d2", "numeric", "returning_clauses_for_sqlite_3_35"] }
chrono = { version = "0.4.38", features = ["serde"] }
uuid = { version = "1.10.0", features = ["v4"] }
rusqlite = { version = "0.32.1", features = ["bundled"] }
Expand All @@ -24,3 +24,5 @@ thiserror = "1.0.63"
lazy_static = "1.5.0"
diesel_migrations = { version = "2.2.0", features = ["sqlite" ] }
rayon = "1.10.0"
r2d2 = "0.8.10"
dashmap = "6.1.0"
14 changes: 13 additions & 1 deletion src-core/migrations/2023-11-08-162221_init_db/down.sql
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
-- This file should undo anything in `up.sql`

DROP TABLE IF EXISTS goals_allocation;
DROP TABLE IF EXISTS goals;
DROP TABLE IF EXISTS settings;
DROP TABLE IF EXISTS quotes;
DROP TABLE IF EXISTS activities;
DROP TABLE IF EXISTS assets;
DROP TABLE IF EXISTS accounts;
DROP TABLE IF EXISTS platforms;

DROP INDEX IF EXISTS market_data_data_source_date_symbol_key;
DROP INDEX IF EXISTS market_data_symbol_idx;
DROP INDEX IF EXISTS assets_data_source_symbol_key;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP TABLE IF EXISTS portfolio_history;
DROP INDEX IF EXISTS idx_portfolio_history_account_date;

DROP TABLE IF EXISTS exchange_rates;
DROP INDEX IF EXISTS idx_exchange_rates_currencies;
44 changes: 44 additions & 0 deletions src-core/migrations/2024-09-16-023604_portfolio_history/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CREATE TABLE portfolio_history (
id TEXT NOT NULL PRIMARY KEY,
account_id TEXT NOT NULL,
date DATE NOT NULL,
total_value NUMERIC NOT NULL DEFAULT 0,
market_value NUMERIC NOT NULL DEFAULT 0,
book_cost NUMERIC NOT NULL DEFAULT 0,
available_cash NUMERIC NOT NULL DEFAULT 0,
net_deposit NUMERIC NOT NULL DEFAULT 0,
currency TEXT NOT NULL,
base_currency TEXT NOT NULL,
total_gain_value NUMERIC NOT NULL DEFAULT 0,
total_gain_percentage NUMERIC NOT NULL DEFAULT 0,
day_gain_percentage NUMERIC NOT NULL DEFAULT 0,
day_gain_value NUMERIC NOT NULL DEFAULT 0,
allocation_percentage NUMERIC NOT NULL DEFAULT 0,
exchange_rate NUMERIC NOT NULL DEFAULT 0,
holdings TEXT,
UNIQUE(account_id, date)
);
CREATE INDEX idx_portfolio_history_account_date ON portfolio_history(account_id, date);

-- change goals table column types
ALTER TABLE "goals" ADD COLUMN "target_amount_new" NUMERIC NOT NULL DEFAULT 0;
UPDATE "goals" SET "target_amount_new" = "target_amount";
ALTER TABLE "goals" DROP COLUMN "target_amount";
ALTER TABLE "goals" RENAME COLUMN "target_amount_new" TO "target_amount";
ALTER TABLE "goals" ADD COLUMN "is_achieved_new" BOOLEAN NOT NULL DEFAULT false;
UPDATE "goals" SET "is_achieved_new" = COALESCE("is_achieved", false);
ALTER TABLE "goals" DROP COLUMN "is_achieved";
ALTER TABLE "goals" RENAME COLUMN "is_achieved_new" TO "is_achieved";

CREATE TABLE exchange_rates (
id TEXT NOT NULL PRIMARY KEY,
from_currency TEXT NOT NULL,
to_currency TEXT NOT NULL,
rate NUMERIC NOT NULL,
source TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE(from_currency, to_currency)
);

CREATE INDEX idx_exchange_rates_currencies ON exchange_rates(from_currency, to_currency);
17 changes: 17 additions & 0 deletions src-core/migrations/2024-09-21-023605_settings_to_kv/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Create a temporary table with the original structure
CREATE TABLE "settings" (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
theme TEXT NOT NULL DEFAULT 'light',
font TEXT NOT NULL,
base_currency TEXT NOT NULL
);

-- Migrate data back from app_settings to settings
INSERT INTO settings (theme, font, base_currency)
SELECT
(SELECT setting_value FROM app_settings WHERE setting_key = 'theme'),
(SELECT setting_value FROM app_settings WHERE setting_key = 'font'),
(SELECT setting_value FROM app_settings WHERE setting_key = 'base_currency');

-- Drop the new app_settings table
DROP TABLE "app_settings";
16 changes: 16 additions & 0 deletions src-core/migrations/2024-09-21-023605_settings_to_kv/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Create the new app_settings table with key-value structure
CREATE TABLE "app_settings" (
"setting_key" TEXT NOT NULL PRIMARY KEY,
"setting_value" TEXT NOT NULL
);

-- Migrate existing settings to the new table
INSERT INTO "app_settings" ("setting_key", "setting_value")
SELECT 'theme', theme FROM settings
UNION ALL
SELECT 'font', font FROM settings
UNION ALL
SELECT 'base_currency', base_currency FROM settings;

-- Drop the old settings table
DROP TABLE "settings";
Empty file.
44 changes: 44 additions & 0 deletions src-core/migrations/2024-09-22-012202_init_exchange_rates/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-- Get the base currency from app_setting
WITH base_currency AS (
SELECT setting_value AS currency
FROM app_settings
WHERE setting_key = 'base_currency'
)

-- Insert exchange rates for accounts
INSERT OR IGNORE INTO exchange_rates (id, from_currency, to_currency, rate, source)
SELECT
base_currency.currency || accounts.currency || '=X' AS id,
base_currency.currency,
accounts.currency,
1.0, -- Default rate, to be updated later
'MANUAL'
FROM accounts
CROSS JOIN base_currency
WHERE accounts.currency != base_currency.currency

UNION

-- Insert exchange rates for activities
SELECT DISTINCT
accounts.currency || activities.currency || '=X' AS id,
accounts.currency,
activities.currency,
1.0, -- Default rate, to be updated later
'MANUAL'
FROM activities
JOIN accounts ON activities.account_id = accounts.id
WHERE activities.currency != accounts.currency

UNION

-- Insert exchange rates from base currency to activity currency
SELECT DISTINCT
base_currency.currency || activities.currency || '=X' AS id,
base_currency.currency,
activities.currency,
1.0, -- Default rate, to be updated later
'MANUAL'
FROM activities
CROSS JOIN base_currency
WHERE activities.currency != base_currency.currency;
12 changes: 12 additions & 0 deletions src-core/src/account/account_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ impl AccountRepository {

diesel::delete(accounts.filter(id.eq(account_id))).execute(conn)
}

pub fn load_accounts_by_ids(
&self,
conn: &mut SqliteConnection,
account_ids: &[String],
) -> Result<Vec<Account>, diesel::result::Error> {
accounts
.filter(id.eq_any(account_ids))
.filter(is_active.eq(true))
.order(created_at.desc())
.load::<Account>(conn)
}
}
Loading

0 comments on commit 1c1ed34

Please sign in to comment.