Navigation Menu

Skip to content

Commit

Permalink
Upgrade column from single to multiple values API
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 13, 2012
1 parent 3f4cabe commit 19eb75a
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 4 deletions.
5 changes: 5 additions & 0 deletions lib/batch/processor.js
Expand Up @@ -99,6 +99,11 @@ Processor.prototype = {
var record = { id: batch.id };
Object.keys(batch.fields).forEach(function(key) {
record[key] = batch.fields[key];
if (Array.isArray(batch.fields[key])) {
var field = this.domain.getIndexField(key);
if (!field.multipleValues)
field.upgradeToMultipleValuesSync();
}
});
this.domain.addRecordSync(record);
results.adds++;
Expand Down
79 changes: 75 additions & 4 deletions test/batch-processor.test.js
Expand Up @@ -48,10 +48,15 @@ suite('batch/processor/Processor (instance methods)', function() {
deletes: deletes
};
assert.deepEqual(result, expected);
var dump = context.commandSync('dump', {
tables: 'companies_00000000000000000000000000'
});
assert.equal(dump, expectedDump);
if (typeof expectedDump == 'string') { // grn dump
var actualGrnDump = context.commandSync('dump', {
tables: 'companies_00000000000000000000000000'
});
assert.equal(actualGrnDump, expectedDump);
} else {
var actualDump = processor.domain.dumpSync();
assert.deepEqual(actualDump, expectedDump);
}
}

test('load add-batches', function(done) {
Expand All @@ -74,6 +79,72 @@ suite('batch/processor/Processor (instance methods)', function() {
assertSuccess(result, 10, 0, schemeDump + '\n' + loadDump);
});

test('loadSync: auto migration to multiple values field', function() {
var batches = fs.readFileSync(__dirname + '/fixture/companies/add-multiple-values.sdf.json', 'UTF-8');
batches = JSON.parse(batches);
var result = processor.loadSync(batches);

var field = processor.domain.getIndexField('product');
assert.isTrue(field.multipleValues);

var dump = [
{ id: 'id1',
name: 'Brazil',
address: 'Shibuya, Tokyo, Japan',
email_address: 'info@razil.jp',
age: 1,
product: ['moritapo','groonga'] },
{ id: 'id2',
name: 'Enishi Tech Inc.',
address: 'Sapporo, Hokkaido, Japan',
email_address: 'info@enishi-tech.com',
age: 2,
product: ['nroonga','groonga'] },
{ id: 'id3'
name: 'ClearCode Inc.',
address: 'Hongo, Tokyo, Japan',
email_address: 'info@clear-code.com',
age: 3,
product: ['cutter','groonga'] },
{ id: 'id4',
name: 'Anaheim Electronics',
address: 'Granada, Moon',
age: 4,
product: ['hi-zack','gundam'] },
{ id: 'id5',
name: 'Shinsei Industry',
address: 'Earth',
age: 5,
product: ['valkyrie'] },
{ type: 'add',
name: 'Omni Consumer Products',
address: 'Detroit and Delta City, Michigan, United States',
age: 6,
product: ['robocop'] },
{ id: 'id7',
name: 'Capsule Corporation',
address: ['West City'],
age: 7,
product: '[time machine'] },
{ id: 'id8',
name: 'Stark Industries',
address: 'United States',
age: 8,
product: ['iron man'] },
{ id: 'id9',
name: 'Umbrella Corporation',
address: 'Tokyo, Japan',
age: 9,
product: ['tyrant'] },
{ id: 'id10',
name: 'U.S. Robots and Mechanical Men',
address: 'New York, United States',
age: 10,
product: ['ndr114'] }
];
assertSuccess(result, 10, 0, dump);
});

test('load delete-batches', function(done) {
var batches = fs.readFileSync(__dirname + '/fixture/companies/add.sdf.json', 'UTF-8');
batches = JSON.parse(batches);
Expand Down
125 changes: 125 additions & 0 deletions test/fixture/companies/add-multiple-values.sdf.json
@@ -0,0 +1,125 @@
[
{
"type": "add",
"id": "id1",
"version": 1,
"lang": "en",
"fields": {
"name": "Brazil",
"address": "Shibuya, Tokyo, Japan",
"email_address": "info@razil.jp",
"age": 1,
"product": ["moritapo","groonga"]
}
},
{
"type": "add",
"id": "id2",
"version": 1,
"lang": "en",
"fields": {
"name": "Enishi Tech Inc.",
"address": "Sapporo, Hokkaido, Japan",
"email_address": "info@enishi-tech.com",
"age": 2,
"product": ["nroonga","groonga"]
}
},
{
"type": "add",
"id": "id3",
"version": 1,
"lang": "en",
"fields": {
"name": "ClearCode Inc.",
"address": "Hongo, Tokyo, Japan",
"email_address": "info@clear-code.com",
"age": 3,
"product": ["cutter","groonga"]
}
},
{
"type": "add",
"id": "id4",
"version": 1,
"lang": "en",
"fields": {
"name": "Anaheim Electronics",
"address": "Granada, Moon",
"age": 4,
"product": ["hi-zack","gundam"]
}
},
{
"type": "add",
"id": "id5",
"version": 1,
"lang": "en",
"fields": {
"name": "Shinsei Industry",
"address": "Earth",
"age": 5,
"product": "valkyrie"
}
},
{
"type": "add",
"id": "id6",
"version": 1,
"lang": "en",
"fields": {
"name": "Omni Consumer Products",
"address": "Detroit and Delta City, Michigan, United States",
"age": 6,
"product": "robocop"
}
},
{
"type": "add",
"id": "id7",
"version": 1,
"lang": "en",
"fields": {
"name": "Capsule Corporation",
"address": "West City",
"age": 7,
"product": "time machine"
}
},
{
"type": "add",
"id": "id8",
"version": 1,
"lang": "en",
"fields": {
"name": "Stark Industries",
"address": "United States",
"age": 8,
"product": "iron man"
}
},
{
"type": "add",
"id": "id9",
"version": 1,
"lang": "en",
"fields": {
"name": "Umbrella Corporation",
"address": "Tokyo, Japan",
"age": 9,
"product": "tyrant"
}
},
{
"type": "add",
"id": "id10",
"version": 1,
"lang": "en",
"fields": {
"name": "U.S. Robots and Mechanical Men",
"address": "New York, United States",
"age": 10,
"product": ["ndr114","spd13"]
}
}
]

0 comments on commit 19eb75a

Please sign in to comment.