fix(v2-import): namespace source identity for snapshot upserts
This commit is contained in:
@ -5,9 +5,9 @@ from .models import Competition, Season, TeamSeason
|
||||
|
||||
@admin.register(Competition)
|
||||
class CompetitionAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "source_uid", "competition_type", "gender", "country", "is_active")
|
||||
list_display = ("name", "source_name", "source_uid", "competition_type", "gender", "country", "is_active")
|
||||
list_filter = ("competition_type", "gender", "country", "is_active")
|
||||
search_fields = ("name", "slug", "source_uid")
|
||||
search_fields = ("name", "slug", "source_name", "source_uid")
|
||||
|
||||
|
||||
@admin.register(Season)
|
||||
|
||||
@ -0,0 +1,35 @@
|
||||
# Generated by Django 5.2.12 on 2026-03-13 15:08
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("competitions", "0003_competition_source_uid_season_source_uid_and_more"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="competition",
|
||||
name="source_name",
|
||||
field=models.CharField(blank=True, default="", max_length=120),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="competition",
|
||||
name="source_uid",
|
||||
field=models.CharField(blank=True, max_length=120, null=True),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name="competition",
|
||||
constraint=models.UniqueConstraint(
|
||||
condition=models.Q(source_uid__isnull=False) & ~models.Q(source_uid=""),
|
||||
fields=("source_name", "source_uid"),
|
||||
name="uq_competition_source_namespace_uid",
|
||||
),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name="competition",
|
||||
index=models.Index(fields=["source_name", "source_uid"], name="competition_source__4c5f3d_idx"),
|
||||
),
|
||||
]
|
||||
@ -14,7 +14,8 @@ class Competition(models.Model):
|
||||
|
||||
name = models.CharField(max_length=220)
|
||||
slug = models.SlugField(max_length=240, unique=True)
|
||||
source_uid = models.CharField(max_length=120, blank=True, null=True, unique=True)
|
||||
source_name = models.CharField(max_length=120, blank=True, default="")
|
||||
source_uid = models.CharField(max_length=120, blank=True, null=True)
|
||||
competition_type = models.CharField(max_length=24, choices=CompetitionType.choices)
|
||||
gender = models.CharField(max_length=16, choices=Gender.choices, default=Gender.MEN)
|
||||
level = models.PositiveSmallIntegerField(default=1)
|
||||
@ -32,10 +33,16 @@ class Competition(models.Model):
|
||||
class Meta:
|
||||
ordering = ["name"]
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=["name", "country"], name="uq_competition_name_country")
|
||||
models.UniqueConstraint(fields=["name", "country"], name="uq_competition_name_country"),
|
||||
models.UniqueConstraint(
|
||||
fields=["source_name", "source_uid"],
|
||||
condition=models.Q(source_uid__isnull=False) & ~models.Q(source_uid=""),
|
||||
name="uq_competition_source_namespace_uid",
|
||||
),
|
||||
]
|
||||
indexes = [
|
||||
models.Index(fields=["name"]),
|
||||
models.Index(fields=["source_name", "source_uid"]),
|
||||
models.Index(fields=["source_uid"]),
|
||||
models.Index(fields=["country"]),
|
||||
models.Index(fields=["competition_type"]),
|
||||
|
||||
@ -96,9 +96,26 @@ def _player_season_source_uid(record: dict[str, Any], source_name: str, snapshot
|
||||
)
|
||||
|
||||
|
||||
def _source_slug(*, source_name: str, base_name: str, fallback_prefix: str, fallback_external_id: str) -> str:
|
||||
base_slug = slugify(base_name) or f"{fallback_prefix}-{fallback_external_id}"
|
||||
source_slug = slugify(source_name) or "snapshot"
|
||||
return f"{source_slug}-{base_slug}"
|
||||
|
||||
|
||||
def _normalized_source_name(source_name: str) -> str:
|
||||
return source_name.strip().lower()
|
||||
|
||||
|
||||
def _upsert_record(record: dict[str, Any], *, source_name: str, snapshot_date: date) -> None:
|
||||
competition_slug = slugify(record["competition_name"]) or f"competition-{record['competition_external_id']}"
|
||||
source_key = _normalized_source_name(source_name)
|
||||
competition_slug = _source_slug(
|
||||
source_name=source_key,
|
||||
base_name=record["competition_name"],
|
||||
fallback_prefix="competition",
|
||||
fallback_external_id=record["competition_external_id"],
|
||||
)
|
||||
competition, _ = Competition.objects.update_or_create(
|
||||
source_name=source_key,
|
||||
source_uid=record["competition_external_id"],
|
||||
defaults={
|
||||
"name": record["competition_name"],
|
||||
@ -119,8 +136,14 @@ def _upsert_record(record: dict[str, Any], *, source_name: str, snapshot_date: d
|
||||
},
|
||||
)
|
||||
|
||||
team_slug = slugify(record["team_name"]) or f"team-{record['team_external_id']}"
|
||||
team_slug = _source_slug(
|
||||
source_name=source_key,
|
||||
base_name=record["team_name"],
|
||||
fallback_prefix="team",
|
||||
fallback_external_id=record["team_external_id"],
|
||||
)
|
||||
team, _ = Team.objects.update_or_create(
|
||||
source_name=source_key,
|
||||
source_uid=record["team_external_id"],
|
||||
defaults={
|
||||
"name": record["team_name"],
|
||||
@ -141,6 +164,7 @@ def _upsert_record(record: dict[str, Any], *, source_name: str, snapshot_date: d
|
||||
)
|
||||
|
||||
player, _ = Player.objects.update_or_create(
|
||||
source_name=source_key,
|
||||
source_uid=record["player_external_id"],
|
||||
defaults={
|
||||
"first_name": record["first_name"],
|
||||
@ -157,7 +181,7 @@ def _upsert_record(record: dict[str, Any], *, source_name: str, snapshot_date: d
|
||||
)
|
||||
|
||||
player_season, _ = PlayerSeason.objects.update_or_create(
|
||||
source_uid=_player_season_source_uid(record, source_name=source_name, snapshot_date=snapshot_date),
|
||||
source_uid=_player_season_source_uid(record, source_name=source_key, snapshot_date=snapshot_date),
|
||||
defaults={
|
||||
"player": player,
|
||||
"season": season,
|
||||
|
||||
@ -37,6 +37,7 @@ class PlayerCareerEntryInline(admin.TabularInline):
|
||||
class PlayerAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"full_name",
|
||||
"source_name",
|
||||
"source_uid",
|
||||
"birth_date",
|
||||
"nationality",
|
||||
@ -54,7 +55,7 @@ class PlayerAdmin(admin.ModelAdmin):
|
||||
"origin_competition",
|
||||
"origin_team",
|
||||
)
|
||||
search_fields = ("full_name", "first_name", "last_name", "source_uid")
|
||||
search_fields = ("full_name", "first_name", "last_name", "source_name", "source_uid")
|
||||
inlines = (PlayerAliasInline, PlayerCareerEntryInline)
|
||||
actions = ("recompute_origin_fields",)
|
||||
|
||||
|
||||
39
apps/players/migrations/0007_player_source_namespaced_uid.py
Normal file
39
apps/players/migrations/0007_player_source_namespaced_uid.py
Normal file
@ -0,0 +1,39 @@
|
||||
# Generated by Django 5.2.12 on 2026-03-13 15:08
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("players", "0006_player_source_uid_and_more"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="player",
|
||||
name="source_name",
|
||||
field=models.CharField(blank=True, default="", max_length=120),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="player",
|
||||
name="source_uid",
|
||||
field=models.CharField(blank=True, max_length=120, null=True),
|
||||
),
|
||||
migrations.RemoveConstraint(
|
||||
model_name="player",
|
||||
name="uq_player_full_name_birth_date",
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name="player",
|
||||
constraint=models.UniqueConstraint(
|
||||
condition=models.Q(source_uid__isnull=False) & ~models.Q(source_uid=""),
|
||||
fields=("source_name", "source_uid"),
|
||||
name="uq_player_source_namespace_uid",
|
||||
),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name="player",
|
||||
index=models.Index(fields=["source_name", "source_uid"], name="players_pla_source__73848c_idx"),
|
||||
),
|
||||
]
|
||||
@ -58,7 +58,8 @@ class Player(TimeStampedModel):
|
||||
first_name = models.CharField(max_length=120)
|
||||
last_name = models.CharField(max_length=120)
|
||||
full_name = models.CharField(max_length=260)
|
||||
source_uid = models.CharField(max_length=120, blank=True, null=True, unique=True)
|
||||
source_name = models.CharField(max_length=120, blank=True, default="")
|
||||
source_uid = models.CharField(max_length=120, blank=True, null=True)
|
||||
birth_date = models.DateField(blank=True, null=True)
|
||||
nationality = models.ForeignKey(
|
||||
"players.Nationality",
|
||||
@ -109,12 +110,14 @@ class Player(TimeStampedModel):
|
||||
ordering = ["full_name", "id"]
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["full_name", "birth_date"],
|
||||
name="uq_player_full_name_birth_date",
|
||||
fields=["source_name", "source_uid"],
|
||||
condition=models.Q(source_uid__isnull=False) & ~models.Q(source_uid=""),
|
||||
name="uq_player_source_namespace_uid",
|
||||
)
|
||||
]
|
||||
indexes = [
|
||||
models.Index(fields=["full_name"]),
|
||||
models.Index(fields=["source_name", "source_uid"]),
|
||||
models.Index(fields=["source_uid"]),
|
||||
models.Index(fields=["last_name", "first_name"]),
|
||||
models.Index(fields=["birth_date"]),
|
||||
|
||||
@ -5,6 +5,6 @@ from .models import Team
|
||||
|
||||
@admin.register(Team)
|
||||
class TeamAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "source_uid", "short_name", "country", "is_national_team")
|
||||
list_display = ("name", "source_name", "source_uid", "short_name", "country", "is_national_team")
|
||||
list_filter = ("is_national_team", "country")
|
||||
search_fields = ("name", "short_name", "slug", "source_uid")
|
||||
search_fields = ("name", "short_name", "slug", "source_name", "source_uid")
|
||||
|
||||
35
apps/teams/migrations/0003_team_source_namespaced_uid.py
Normal file
35
apps/teams/migrations/0003_team_source_namespaced_uid.py
Normal file
@ -0,0 +1,35 @@
|
||||
# Generated by Django 5.2.12 on 2026-03-13 15:08
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("teams", "0002_team_source_uid_team_teams_team_source__940258_idx"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="team",
|
||||
name="source_name",
|
||||
field=models.CharField(blank=True, default="", max_length=120),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="team",
|
||||
name="source_uid",
|
||||
field=models.CharField(blank=True, max_length=120, null=True),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name="team",
|
||||
constraint=models.UniqueConstraint(
|
||||
condition=models.Q(source_uid__isnull=False) & ~models.Q(source_uid=""),
|
||||
fields=("source_name", "source_uid"),
|
||||
name="uq_team_source_namespace_uid",
|
||||
),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name="team",
|
||||
index=models.Index(fields=["source_name", "source_uid"], name="teams_team_source__8035ae_idx"),
|
||||
),
|
||||
]
|
||||
@ -5,7 +5,8 @@ class Team(models.Model):
|
||||
name = models.CharField(max_length=200)
|
||||
short_name = models.CharField(max_length=80, blank=True)
|
||||
slug = models.SlugField(max_length=220, unique=True)
|
||||
source_uid = models.CharField(max_length=120, blank=True, null=True, unique=True)
|
||||
source_name = models.CharField(max_length=120, blank=True, default="")
|
||||
source_uid = models.CharField(max_length=120, blank=True, null=True)
|
||||
country = models.ForeignKey(
|
||||
"players.Nationality",
|
||||
on_delete=models.SET_NULL,
|
||||
@ -21,11 +22,17 @@ class Team(models.Model):
|
||||
class Meta:
|
||||
ordering = ["name"]
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=["name", "country"], name="uq_team_name_country")
|
||||
models.UniqueConstraint(fields=["name", "country"], name="uq_team_name_country"),
|
||||
models.UniqueConstraint(
|
||||
fields=["source_name", "source_uid"],
|
||||
condition=models.Q(source_uid__isnull=False) & ~models.Q(source_uid=""),
|
||||
name="uq_team_source_namespace_uid",
|
||||
),
|
||||
]
|
||||
indexes = [
|
||||
models.Index(fields=["name"]),
|
||||
models.Index(fields=["slug"]),
|
||||
models.Index(fields=["source_name", "source_uid"]),
|
||||
models.Index(fields=["source_uid"]),
|
||||
models.Index(fields=["country"]),
|
||||
models.Index(fields=["is_national_team"]),
|
||||
|
||||
Reference in New Issue
Block a user