#!/usr/bin/env python

import rethinkdb as r
from optparse import OptionParser
import sys


def msg(s):
    print s
    sys.stdout.flush()


def fix_users_table_type(conn):
    r.table('users').update({'otype': 'user'}).run(conn)
    r.table('users').filter(r.row.has_fields('_type')).replace(lambda doc: doc.without('_type')).run(conn)


def convert_scan_size(conn):
    print "Converting setup attribute scan_size to scan_size_width..."
    r.table('setupproperties').filter({'attribute': 'scan_size'}) \
        .update({
        'attribute': 'scan_size_width',
        'name': 'Scan Size Width'
    }).run(conn)
    print "Done."


def add_missing_note_field_to_processes(conn):
    print "Adding note field to processes that are missing it..."
    r.table('processes').filter(~r.row.has_fields('note')).update({'note': ''}).run(conn)
    print "Done."


def main():
    parser = OptionParser()
    parser.add_option("-P", "--port", dest="port", type="int", help="rethinkdb port", default=30815)
    (options, args) = parser.parse_args()
    conn = r.connect('localhost', options.port, db="materialscommons")

    # Is this needed?
    # fix_users_table_type(conn)

    convert_scan_size(conn)
    add_missing_note_field_to_processes(conn)


if __name__ == "__main__":
    main()

@@ -637,36 +637,43 @@
 def add_missing_
-note
+description
 _field_to_proces
@@ -705,144 +705,408 @@
 ing 
+description field and removing 
 note
+s
  field
- to processes that are missing it...%22%0A    r.table('processes').filter(~r.row.has_fields('note')).update(%7B'note': ''%7D).run(conn
+...%22%0A    r.table('processes').update(%7B'description': ''%7D).run(conn)%0A    all_processes = list(r.table('processes').run(conn))%0A    for p in all_processes:%0A        if 'note' in p:%0A            if p%5B'note'%5D != '':%0A                r.table('processes').get(p%5B'id'%5D).update(%7B'description': p%5B'note'%5D%7D).run(conn)%0A    r.table('processes').replace(r.row.without('note')
 )%0A  
@@ -1478,20 +1478,27 @@
 missing_
-note
+description
 _field_t

Remove note and add description field to process. Populate description field with the note field content.