@@ -20,6 +20,22 @@ def initialize(host, port, dbname, user, password)
2020 password : password
2121 )
2222 @connected = true
23+ version = get_latest_schema_migration
24+ if version >= 20250413141446
25+ Rails . logger . info "Detected schema migration version #{ version } ; Using new simplified schema!"
26+ @entries_table = "public.entries"
27+ @valuations_table = "public.valuations"
28+ @valuation_key = "Valuation"
29+ @transactions_table = "public.transactions"
30+ @transaction_key = "Transaction"
31+ else
32+ Rails . logger . info "Detected schema migration version #{ version } ; Using old Account:: schema!"
33+ @entries_table = "public.account_entries"
34+ @valuations_table = "public.account_valuations"
35+ @valuation_key = "Account::Valuation"
36+ @transactions_table = "public.account_transactions"
37+ @transaction_key = "Account::Transaction"
38+ end
2339 rescue PG ::Error => e
2440 Rails . logger . error "Connection error: #{ e . message } "
2541 @connected = false
@@ -31,6 +47,10 @@ def connected?
3147 @connected
3248 end
3349
50+ def get_latest_schema_migration
51+ execute ( "SELECT version FROM public.schema_migrations ORDER BY version DESC LIMIT 1" ) &.first &.dig ( "version" ) &.to_i
52+ end
53+
3454 def get_families
3555 execute ( "SELECT id, name FROM public.families" )
3656 end
@@ -55,8 +75,9 @@ def get_accounts(family_id = nil)
5575 end
5676
5777 def get_simplefin_transactions ( account_id , start_date )
78+ entries_table = @entries_table
5879 query = <<-SQL
59- SELECT plaid_id FROM public.account_entries
80+ SELECT plaid_id FROM #{ entries_table }
6081 WHERE account_id = $1
6182 AND plaid_id IS NOT NULL
6283 AND date >= (TO_TIMESTAMP($2)::DATE)
@@ -66,17 +87,20 @@ def get_simplefin_transactions(account_id, start_date)
6687 end
6788
6889 def upsert_account_valuation ( account_id , simplefin_account )
90+ entries_table = @entries_table
91+ valuations_table = @valuations_table
92+
6993 valuation_uuid = SecureRandom . uuid
7094 amount = simplefin_account . dig ( "balance" )
7195 currency = simplefin_account . dig ( "currency" )
7296 date = simplefin_account . dig ( "balance-date" )
7397
7498 # Check if a row exists with the same account_id and date
7599 select_query = <<-SQL
76- SELECT id FROM public.account_entries
77- WHERE account_id = $1 AND date = (TO_TIMESTAMP($2)::DATE) AND entryable_type = 'Account::Valuation' LIMIT 1;
100+ SELECT id FROM #{ entries_table }
101+ WHERE account_id = $1 AND date = (TO_TIMESTAMP($2)::DATE) AND entryable_type = $3 LIMIT 1;
78102 SQL
79- existing_entry = execute ( select_query , [ account_id , date ] ) . first
103+ existing_entry = execute ( select_query , [ account_id , date , @valuation_key ] ) . first
80104
81105 if existing_entry
82106 # Update existing row
@@ -85,15 +109,15 @@ def upsert_account_valuation(account_id, simplefin_account)
85109
86110 valuation_uuid = existing_entry [ "id" ]
87111 update_query = <<-SQL
88- UPDATE public.account_entries
112+ UPDATE #{ entries_table }
89113 SET amount = $1, updated_at = NOW()
90114 WHERE id = $2;
91115 SQL
92116 execute ( update_query , [ amount , valuation_uuid ] )
93117
94- # also update account_valuations timestamp
118+ # also update valuations timestamp
95119 valuation_update_query = <<-SQL
96- UPDATE public.account_valuations
120+ UPDATE #{ valuations_table }
97121 SET updated_at = NOW()
98122 WHERE id = $1;
99123 SQL
@@ -104,16 +128,16 @@ def upsert_account_valuation(account_id, simplefin_account)
104128 Rails . logger . info "Adding a Balance Update..."
105129
106130 insert_query = <<-SQL
107- INSERT INTO public.account_entries (
131+ INSERT INTO #{ entries_table } (
108132 account_id, entryable_type, entryable_id, amount, currency, date, name, created_at, updated_at
109133 ) VALUES (
110- $1, 'Account::Valuation', $2, $3, $4, (TO_TIMESTAMP($5 )::DATE), 'Balance Update', NOW(), NOW()
134+ $1, $2, $3, $4, $5, (TO_TIMESTAMP($6 )::DATE), 'Balance Update', NOW(), NOW()
111135 );
112136 SQL
113- execute ( insert_query , [ account_id , valuation_uuid , amount , currency , date ] )
137+ execute ( insert_query , [ account_id , @valuation_key , valuation_uuid , amount , currency , date ] )
114138
115139 insert_valuation_query = <<-SQL
116- INSERT INTO public.account_valuations (
140+ INSERT INTO #{ valuations_table } (
117141 id, created_at, updated_at
118142 ) VALUES (
119143 $1, NOW(), NOW()
@@ -124,6 +148,9 @@ def upsert_account_valuation(account_id, simplefin_account)
124148 end
125149
126150 def new_transaction ( account_id , simplefin_transaction_record , currency )
151+ entries_table = @entries_table
152+ transactions_table = @transactions_table
153+
127154 amount = simplefin_transaction_record . dig ( "amount" )
128155 short_date = simplefin_transaction_record . dig ( "posted" )
129156 display_name = simplefin_transaction_record . dig ( "description" )
@@ -132,19 +159,19 @@ def new_transaction(account_id, simplefin_transaction_record, currency)
132159 transaction_uuid = SecureRandom . uuid
133160 adjusted_amount = BigDecimal ( amount . to_s ) * -1
134161
135- # Insert the account_entries entry
162+ # Insert the entries entry
136163 query = <<-SQL
137- INSERT INTO public.account_entries (
164+ INSERT INTO #{ entries_table } (
138165 account_id, entryable_type, entryable_id, amount, currency, date, name, created_at, updated_at, plaid_id
139166 ) VALUES (
140- $1, 'Account::Transaction', $2, $3, $4, (TO_TIMESTAMP($5 )::DATE), $6 , NOW(), NOW(), $7
167+ $1, $2, $3, $4, $5, (TO_TIMESTAMP($6 )::DATE), $7 , NOW(), NOW(), $8
141168 );
142169 SQL
143- execute ( query , [ account_id , transaction_uuid , adjusted_amount , currency , short_date , display_name , simplefin_txn_id ] )
170+ execute ( query , [ account_id , @transaction_key , transaction_uuid , adjusted_amount , currency , short_date , display_name , simplefin_txn_id ] )
144171
145- # Insert the account_transaction entry
172+ # Insert the transaction entry
146173 query = <<-SQL
147- INSERT INTO public.account_transactions (
174+ INSERT INTO #{ transactions_table } (
148175 id, created_at, updated_at
149176 ) VALUES (
150177 $1, NOW(), NOW()
0 commit comments