Compare commits

...

3 Commits

Author SHA1 Message Date
hackerESQ 6d6f968f42 Merge pull request #76 from investbrainapp/dividend-splits-should-be-unique
fix: add unique constraint to split and dividends
2025-03-19 16:17:01 -05:00
hackerESQ 261c848ffd fix: add unique constraint to split and dividends
to prevent duplicate records
2025-03-19 16:16:38 -05:00
hackerESQ 9bcc80078e Update 2021_09_06_014744_create_holdings_table.php 2025-03-19 15:32:38 -05:00
6 changed files with 15 additions and 19 deletions
+2 -2
View File
@@ -68,7 +68,7 @@ class Dividend extends Model
// nope, refresh forward looking only
if ($dividends_meta->total_dividends) {
$start_date = $dividends_meta->last_dividend_update->addHours(24);
$start_date = $dividends_meta->last_dividend_update;
}
// skip refresh if there's already recent data
@@ -90,7 +90,7 @@ class Dividend extends Model
}
// insert records
(new self)->insert($dividend_data->toArray());
(new self)->insertOrIgnore($dividend_data->toArray());
// sync to holdings
self::syncHoldings($symbol);
+1 -1
View File
@@ -73,7 +73,7 @@ class Split extends Model
if ($split_data->isNotEmpty()) {
// insert records
(new self)->insert($split_data->map(function ($split) {
(new self)->insertOrIgnore($split_data->map(function ($split) {
return [...$split, ...['id' => Str::uuid()->toString()]];
})->toArray());
@@ -21,6 +21,8 @@ class CreateDividendsTable extends Migration
$table->string('symbol', 25);
$table->float('dividend_amount', 12, 4);
$table->timestamps();
$table->unique(['date', 'symbol']);
});
}
@@ -21,6 +21,8 @@ class CreateSplitsTable extends Migration
$table->string('symbol', 25);
$table->float('split_amount', 12, 4);
$table->timestamps();
$table->unique(['date', 'symbol']);
});
}
@@ -25,7 +25,6 @@ class CreateHoldingsTable extends Migration
$table->float('total_cost_basis', 12, 4)->default(0);
$table->float('realized_gain_dollars', 12, 4)->default(0);
$table->float('dividends_earned', 12, 4)->default(0);
$table->boolean('reinvest_dividends')->default(false);
$table->timestamp('splits_synced_at')->nullable();
$table->timestamps();
});
+8 -15
View File
@@ -58,23 +58,16 @@ class DividendsTest extends TestCase
$this->assertEqualsWithDelta(4.95, $dividendsReinvested * $market_data->market_value, 0.01);
}
public function test_do_not_duplicate_recent_dividends(): void
public function test_cannot_insert_duplicate_dividends(): void
{
$this->actingAs($user = User::factory()->create());
$portfolio = Portfolio::factory()->create();
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolio->id)->symbol('ACME')->create();
$holding = Holding::query()->portfolio($portfolio->id)->symbol('ACME')->first();
Dividend::create([
'symbol' => 'ACME',
'date' => now()->subDay(2),
'dividend_amount' => .01,
]);
// first insert
Dividend::refreshDividendData('ACME');
$this->assertCount(1, $holding->dividends);
// try to duplicate
Dividend::refreshDividendData('ACME');
$dividend_count = Dividend::count();
$this->assertEquals(3, $dividend_count);
}
}