#!/usr/bin/env node

var promise = require('bluebird');

promise.coroutine(function *() {
    'use strict';

    let ropts = {
        db: 'mctest',
        port: 30815
    };

    let r = require('rethinkdbdash')(ropts);

    try {
        yield r.dbDrop('mctest');
    } catch (err) {
        console.log(err);
    }

    console.log("Creating mctest database.");
    console.log("=======================");
    yield r.dbCreate('mctest');
    yield * createTable('processes', 'template_id');
    yield * createTable('project2process', 'project_id', 'process_id');
    yield * createTable('setups');
    yield * createTable('process2setup', 'process_id', 'setup_id');
    yield * createTable('measurements', 'process_id');
    yield * createTable('measure2datafile', 'measurement_id', 'datafile_id');
    yield * createTable('process2measurement', 'process_id', 'measurement_id');
    yield * createTable('setupproperties', 'setting_id');
    yield * createTable('attributes', 'parent_id');
    yield * createTable('attribute2measurement', 'attribute_id', 'measurement_id');
    yield * createTable('attributeset2attribute', 'attribute_set_id', 'attribute_id');
    yield * createTable('attribute2process', 'attribute_id', 'process_id');
    yield * createTable('samples');
    yield * createTable('sample2file', 'sample_id', 'datafile_id');
    yield * createTable('attributeset', 'parent_id');
    yield * createTable('sample2attributeset', 'sample_id', 'attribute_set_id');
    yield * createTable('process2sample', 'process_id', 'sample_id', 'attribute_set_id');
    yield * createTable('project2sample', 'project_id', 'sample_id');
    yield * createTable('best_measure_history', 'process_id', 'attribute_id', 'measurement_id');
    yield * createTable('process2setupfile', 'process_id', 'datafile_id');
    yield * createTable('process2output', 'process_id', 'datafile_id');
    yield * createTable('process2input', 'process_id', 'datafile_id');
    yield r.getPool().drain();

    /////////////////////////////

    function *createTable(table) {
        console.log('  create table...', table);
        yield r.tableCreate(table);
        if (arguments.length > 1) {
            for (let i = 1; i < arguments.length; i++) {
                console.log('     create index', arguments[i]);
                yield r.table(table).indexCreate(arguments[i]);
            }
        }
    }
})();

@@ -692,31 +692,34 @@
 le('
-measurements', 'process
+setupproperties', 'setting
 _id'
@@ -750,39 +750,36 @@
 le('
-measure2datafile', 'measurement
+process2setupfile', 'process
 _id'
@@ -828,50 +828,22 @@
 'pro
-cess2measurement', 'process_id', 'measurem
+perties', 'par
 ent_
@@ -877,40 +877,91 @@
 le('
+property
 set
-up
+2
 propert
-ies', 'setting_id');
+y', 'attribute_set_id', 'attribute_id'); // TODO: Review indices
 %0A   
@@ -986,28 +986,31 @@
 le('
-attributes', 'parent
+measurements', 'process
 _id'
@@ -1037,25 +1037,24 @@
 eTable('
-attribute
+property
 2measure
@@ -1098,269 +1098,106 @@
 d');
-%0A    yield * createTable('attributeset2attribute', 'attribute_set_id', 'attribute_id');%0A    yield * createTable('attribute2process', 'attribute_id', 'process_id');%0A    yield * createTable('samples');%0A    yield * createTable('sample2file', 'sample_id', 'datafile
+ // TODO: Review indices%0A    yield * createTable('process2measurement', 'process_id', 'measurement
 _id'
@@ -1228,33 +1228,15 @@
 le('
-attributeset', 'parent_id
+samples
 ');%0A
@@ -1267,25 +1267,24 @@
 'sample2
-attribute
+property
 set', 's
@@ -1306,32 +1306,56 @@
 ribute_set_id');
+ // TODO: Review indices
 %0A    yield * cre
@@ -1424,24 +1424,48 @@
 te_set_id');
+ // TODO: Review indices
 %0A    yield *
@@ -1623,226 +1623,97 @@
 d');
-%0A    yield * createTable('process2setupfile', 'process_id', 'datafile_id');%0A    yield * createTable('process2output', 'process_id', 'datafile_id');%0A    yield * createTable('process2input', 'process_id', 'datafile_id');
+ // TODO: Review indices%0A    yield * createTable('process2file'); // TODO: Review indices
 %0A   

Create tables.