# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-05-24 19:33
from __future__ import unicode_literals

import logging

from django.db import migrations

from osf.models import MetaSchema
from website.project.metadata.schemas import OSF_META_SCHEMAS


logger = logging.getLogger(__file__)


def add_schemas(*args):
    """Import meta-data schemas from JSON to database if not already loaded
    """
    schema_count = 0
    for schema in OSF_META_SCHEMAS:
        schema_obj, created = MetaSchema.objects.get_or_create(
            name=schema['name'],
            schema_version=schema.get('version', 1)
        )
        schema_obj.schema = schema
        schema_obj.save()
        schema_count += 1

        if created:
            logger.info('Added schema {} to the database'.format(schema['name']))

    logger.info('Ensured {} schemas are in the database'.format(schema_count))


def remove_schemas(*args):
    removed_schemas = 0
    for schema in OSF_META_SCHEMAS:
        schema_obj = MetaSchema.objects.get(
            schema=schema,
            name=schema['name'],
            schema_version=schema.get('version', 1)
        )
        schema_obj.delete()
        removed_schemas += 1

    logger.info('Removed {} schemas from the database'.format(removed_schemas))

class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0035_ensure_licenses'),
    ]

    operations = [
        migrations.RunPython(add_schemas, remove_schemas),
    ]

Add migration for ensure schemas