# Generated by Django 2.2.5 on 2019-10-16 13:18

import django.db.models.deletion
from django.contrib.auth import get_user_model
from django.db import connection, migrations, models


def populate_useraccounts(apps, schema_editor):
    User = get_user_model()
    users = User.objects.all()
    if not users:
        return
    values = [f'({user.id},\'[]\',\'{{}}\')' for user in users]
    sql_values = ",".join(values)
    sql_statement = f""" INSERT INTO gis_app_useraccount (user_ptr_id,vehicles, avg_coords)
    VALUES {sql_values};
    """
    with connection.cursor() as c:
        c.execute(sql_statement)


class Migration(migrations.Migration):

    dependencies = [
        ('gis_app', '0011_useraccount'),
    ]

    operations = [
        migrations.RunPython(populate_useraccounts),
        migrations.AlterField(
            model_name='userposition',
            name='user',
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                to='gis_app.UserAccount'),
        ),
        migrations.AlterField(
            model_name='uservehicle',
            name='user',
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                to='gis_app.UserAccount'),
        ),
        migrations.AlterField(
            model_name='vehicle',
            name='users',
            field=models.ManyToManyField(through='gis_app.UserVehicle',
                                         to='gis_app.UserAccount'),
        ),
    ]
