<commit_before>from django.db import models
import json

#TODO: less arbitrary name length

#### LOGGER DB ####

def toJSON(dbname, tablename):
    """
    Creates a JSON representation of a table schema. 
    Doesn't include the database name, because that's not guaranteed
    to be the same on the destination server anyway.
    """

    db = Database.objects.get(name=dbname)
    table = Table.objects.get(db=db, name=tablename)

    schema = {tablename:[]}
    for column in Column.objects.filter(table=table):
        schema[tablename].append((column.name, column.type))

    return json.dumps(schema, sort_keys=True)

def fromJSON(dbname, schema_json):
    """
    Creates Database, Table, and Column objects as needed to satisfy the incoming schema.
    If the table is already present, assume we are updating: delete all columns and recreate from the schema.
    """
    
    schema = json.loads(schema_json)
    try:
        db = Database.objects.get(name=dbname)
    except Database.DoesNotExist:
        db = Database(name=dbname)
        db.save()

    for (tablename, columns) in schema.iteritems():
        try:
            table = Table.objects.get(db=db, name=tablename)
            for column in Columns.objects.filter(table=table):
                column.delete()
        except Table.DoesNotExist:
            table = Table(db=db, name=tablename)<commit_msg>Adding optional destination database to schema<commit_after>from django.db import models
import json

#TODO: less arbitrary name length

#### LOGGER DB ####

def toJSON(dbname, tablenames, destdb = None):
    """
    Creates a JSON representation of a table schema. 
    Doesn't include the database name, because that's not guaranteed
    to be the same on the destination server anyway.
    """

    db = Database.objects.get(name=dbname)
    
    schema = {'tables':{}}

    for tablename in tablenames:
        table = Table.objects.get(db=db, name=tablename)
        schema['tables'][tablename] = []

        for column in Column.objects.filter(table=table):
            schema[tablename].append((column.name, column.type))

    if destdb:
        schema['database'] = destdb

    return json.dumps(schema, sort_keys=True)

def fromJSON(dbname, schema_json):
    """
    Creates Database, Table, and Column objects as needed to satisfy the incoming schema.
    If the table is already present, assume we are updating: delete all columns and recreate from the schema.
    """
    
    schema = json.loads(schema_json)
    try:
        db = Database.objects.get(name=dbname)
    except Database.DoesNotExist:
        db = Database(name=dbname)
        db.save()

    for (tablename, columns) in schema.iteritems():
        try:
            table = Table.objects.get(db=db, name=tablename)
            for column in Columns.objects.filter(table=table):
                column.delete()
        except Table.DoesNotExist:
            table = Table(db=db, name=tablename)
{'fraction_non_alphanumeric': 0.1010854816824966, 'fraction_numerical': 0.0, 'mean_word_length': 2.890501319261214, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '943193', 'n_tokens_mistral': 858, 'n_tokens_neox': 848, 'n_words': 257}