-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add missing database constraint on TradeHistory - Cleanup of unnecessary parameters
- Loading branch information
Showing
2 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
base/migrations/0016_tradehistory_unique_user_bot_timestamp_start.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Generated by Django 4.2.4 on 2023-08-28 02:42 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
def remove_duplicates(apps, schema_editor): | ||
""" | ||
Remvos duplicates on the (user, bot, timestart_key) if | ||
they exists. | ||
""" | ||
TradeHistory = apps.get_model("base", "TradeHistory") | ||
dups = ( | ||
TradeHistory.objects.all() | ||
.order_by() | ||
.annotate( | ||
key=models.functions.Concat( | ||
models.F("user"), | ||
models.Value("--"), | ||
models.F("bot"), | ||
models.Value("--"), | ||
models.F("timestamp_start"), | ||
output_field=models.CharField(), | ||
) | ||
) | ||
.values("timestamp_start", "key") | ||
.annotate(dup_count=models.Count("key")) | ||
).filter(dup_count__gt=1) | ||
for ts in dups: | ||
records = TradeHistory.objects.filter( | ||
timestamp_start=ts["timestamp_start"] | ||
) | ||
for record in records[1:]: | ||
record.delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("base", "0015_alter_traderobot_fund_quote_asset_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
code=remove_duplicates, | ||
reverse_code=migrations.RunPython.noop, | ||
), | ||
migrations.AddConstraint( | ||
model_name="tradehistory", | ||
constraint=models.UniqueConstraint( | ||
fields=("user", "bot", "timestamp_start"), | ||
name="unique_user_bot_timestamp_start", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters