fix: add unique constraint to split and dividends
to prevent duplicate records
This commit is contained in:
@@ -68,7 +68,7 @@ class Dividend extends Model
|
|||||||
// nope, refresh forward looking only
|
// nope, refresh forward looking only
|
||||||
if ($dividends_meta->total_dividends) {
|
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
|
// skip refresh if there's already recent data
|
||||||
@@ -90,7 +90,7 @@ class Dividend extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
// insert records
|
// insert records
|
||||||
(new self)->insert($dividend_data->toArray());
|
(new self)->insertOrIgnore($dividend_data->toArray());
|
||||||
|
|
||||||
// sync to holdings
|
// sync to holdings
|
||||||
self::syncHoldings($symbol);
|
self::syncHoldings($symbol);
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ class Split extends Model
|
|||||||
if ($split_data->isNotEmpty()) {
|
if ($split_data->isNotEmpty()) {
|
||||||
|
|
||||||
// insert records
|
// insert records
|
||||||
(new self)->insert($split_data->map(function ($split) {
|
(new self)->insertOrIgnore($split_data->map(function ($split) {
|
||||||
|
|
||||||
return [...$split, ...['id' => Str::uuid()->toString()]];
|
return [...$split, ...['id' => Str::uuid()->toString()]];
|
||||||
})->toArray());
|
})->toArray());
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ class CreateDividendsTable extends Migration
|
|||||||
$table->string('symbol', 25);
|
$table->string('symbol', 25);
|
||||||
$table->float('dividend_amount', 12, 4);
|
$table->float('dividend_amount', 12, 4);
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['date', 'symbol']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ class CreateSplitsTable extends Migration
|
|||||||
$table->string('symbol', 25);
|
$table->string('symbol', 25);
|
||||||
$table->float('split_amount', 12, 4);
|
$table->float('split_amount', 12, 4);
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['date', 'symbol']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-15
@@ -58,23 +58,16 @@ class DividendsTest extends TestCase
|
|||||||
$this->assertEqualsWithDelta(4.95, $dividendsReinvested * $market_data->market_value, 0.01);
|
$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());
|
// first insert
|
||||||
|
|
||||||
$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,
|
|
||||||
]);
|
|
||||||
|
|
||||||
Dividend::refreshDividendData('ACME');
|
Dividend::refreshDividendData('ACME');
|
||||||
|
|
||||||
$this->assertCount(1, $holding->dividends);
|
// try to duplicate
|
||||||
|
Dividend::refreshDividendData('ACME');
|
||||||
|
|
||||||
|
$dividend_count = Dividend::count();
|
||||||
|
|
||||||
|
$this->assertEquals(3, $dividend_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user