<commit_before>"""
Add _hour_start_at and _day_start_at field to all documents in all collections
"""
import logging

from backdrop.core.records import Record
from backdrop.core.timeutils import utc

log = logging.getLogger(__name__)


def up(db):
    for name in db.collection_names():
        log.info("Migrating collection: {0}".format(name))
        collection = db[name]
        query = {
            "_timestamp": {"$exists": True},
            "_day_start_at": {"$exists": False}
        }
        for document in collection.find(query):
            document['_timestamp'] = utc(document['_timestamp'])
            for attr in ['_updated_at', '_week_start_at', '_month_start_at']:
                if attr in document:
                    document.pop(attr)
            record = Record(document)

            collection.save(record.to_mongo())<commit_msg>Only run the mongo migration if the collection is not capped<commit_after>"""
Add _hour_start_at and _day_start_at field to all documents in all collections
"""
import logging

from backdrop.core.records import Record
from backdrop.core.timeutils import utc

log = logging.getLogger(__name__)


def up(db):
    for name in db.collection_names():
        log.info("Migrating collection: {0}".format(name))
        collection = db[name]
        query = {
            "_timestamp": {"$exists": True},
            "_day_start_at": {"$exists": False}
        }
        is_capped = collection.options()['capped']
        if not is_capped:
            for document in collection.find(query):
                document['_timestamp'] = utc(document['_timestamp'])
                for attr in ['_updated_at', '_week_start_at', '_month_start_at']:
                    if attr in document:
                        document.pop(attr)
                record = Record(document)

                collection.save(record.to_mongo())
{'fraction_non_alphanumeric': 0.1104589114194237, 'fraction_numerical': 0.0010672358591248667, 'mean_word_length': 2.4851301115241635, 'pattern_counts': {'":': 8, '<': 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': '399029', 'n_tokens_mistral': 525, 'n_tokens_neox': 507, 'n_words': 138}