# Generated by Django 5.2 on 2024-08-12 07:46

from django.db import migrations

SQL_TEMPLATE = """\
-- First, set all is_latest to False
UPDATE {table_name} SET is_latest = false;

-- Then, set is_latest to True for the latest records
UPDATE {table_name}
SET is_latest = true
WHERE id IN (
    SELECT t.id
    FROM {table_name} t
    INNER JOIN (
        SELECT substr(uid, 1, {stem_uid_len}) AS stem_uid, MAX(created_at) AS max_created_at
        FROM {table_name}
        GROUP BY substr(uid, 1, {stem_uid_len})
    ) latest ON substr(t.uid, 1, {stem_uid_len}) = latest.stem_uid AND t.created_at = latest.max_created_at
);
"""


class Migration(migrations.Migration):
    dependencies = [
        (
            "lamindb",
            "0062_add_is_latest_field",
        ),
    ]

    operations = []  # type: ignore


# add data migration
for table_name, stem_uid_len in [
    ("lamindb_transform", 12),
    ("lamindb_artifact", 16),
    ("lamindb_collection", 16),
]:
    Migration.operations.append(
        migrations.RunSQL(
            sql=SQL_TEMPLATE.format(table_name=table_name, stem_uid_len=stem_uid_len)
        )
    )
