@sanity/migrate
Version:
Tooling for running data migrations on Sanity.io projects
887 lines (886 loc) • 33.4 kB
JavaScript
function _async_generator(gen) {
var front, back;
function send(key, arg) {
return new Promise(function(resolve, reject) {
var request = {
key: key,
arg: arg,
resolve: resolve,
reject: reject,
next: null
};
if (back) back = back.next = request;
else {
front = back = request;
resume(key, arg);
}
});
}
function resume(key, arg) {
try {
var result = gen[key](arg);
var value = result.value;
var overloaded = value instanceof _overload_yield;
Promise.resolve(overloaded ? value.v : value).then(function(arg) {
if (overloaded) {
var nextKey = key === "return" ? "return" : "next";
if (!value.k || arg.done) return resume(nextKey, arg);
else arg = gen[nextKey](arg).value;
}
settle(result.done ? "return" : "normal", arg);
}, function(err) {
resume("throw", err);
});
} catch (err) {
settle("throw", err);
}
}
function settle(type, value) {
switch(type){
case "return":
front.resolve({
value: value,
done: true
});
break;
case "throw":
front.reject(value);
break;
default:
front.resolve({
value: value,
done: false
});
break;
}
front = front.next;
if (front) resume(front.key, front.arg);
else back = null;
}
this._invoke = send;
if (typeof gen.return !== "function") this.return = undefined;
}
_async_generator.prototype[typeof Symbol === "function" && Symbol.asyncIterator || "@@asyncIterator"] = function() {
return this;
};
_async_generator.prototype.next = function(arg) {
return this._invoke("next", arg);
};
_async_generator.prototype.throw = function(arg) {
return this._invoke("throw", arg);
};
_async_generator.prototype.return = function(arg) {
return this._invoke("return", arg);
};
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _async_to_generator(fn) {
return function() {
var self = this, args = arguments;
return new Promise(function(resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
function _overload_yield(value, kind) {
this.v = value;
this.k = kind;
}
function _wrap_async_generator(fn) {
return function() {
return new _async_generator(fn.apply(this, arguments));
};
}
function _ts_generator(thisArg, body) {
var f, y, t, _ = {
label: 0,
sent: function() {
if (t[0] & 1) throw t[1];
return t[1];
},
trys: [],
ops: []
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
return d(g, "next", {
value: verb(0)
}), d(g, "throw", {
value: verb(1)
}), d(g, "return", {
value: verb(2)
}), typeof Symbol === "function" && d(g, Symbol.iterator, {
value: function() {
return this;
}
}), g;
function verb(n) {
return function(v) {
return step([
n,
v
]);
};
}
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while(g && (g = 0, op[0] && (_ = 0)), _)try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [
op[0] & 2,
t.value
];
switch(op[0]){
case 0:
case 1:
t = op;
break;
case 4:
_.label++;
return {
value: op[1],
done: false
};
case 5:
_.label++;
y = op[1];
op = [
0
];
continue;
case 7:
op = _.ops.pop();
_.trys.pop();
continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
_ = 0;
continue;
}
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
_.label = op[1];
break;
}
if (op[0] === 6 && _.label < t[1]) {
_.label = t[1];
t = op;
break;
}
if (t && _.label < t[2]) {
_.label = t[2];
_.ops.push(op);
break;
}
if (t[2]) _.ops.pop();
_.trys.pop();
continue;
}
op = body.call(thisArg, _);
} catch (e) {
op = [
6,
e
];
y = 0;
} finally{
f = t = 0;
}
if (op[0] & 5) throw op[1];
return {
value: op[0] ? op[1] : void 0,
done: true
};
}
}
import { describe, expect, test } from 'vitest';
import { toArray } from '../../../it-utils/index.js';
import { batchMutations } from '../batchMutations.js';
function byteLength(obj) {
return JSON.stringify(obj).length;
}
describe('mutation payload batching', function() {
test('when everything fits into a single mutation request', function() {
return _async_to_generator(function() {
var first, second, gen, firstSize, secondSize, it;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
first = {
createIfNotExists: {
_id: 'foo',
_type: 'something',
bar: 'baz'
}
};
second = {
patch: {
id: 'foo',
set: {
bar: 'baz'
}
}
};
gen = function gen() {
return _wrap_async_generator(function() {
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
first
];
case 1:
_state.sent();
return [
4,
second
];
case 2:
_state.sent();
return [
2
];
}
});
})();
};
firstSize = JSON.stringify(first).length;
secondSize = JSON.stringify(second).length;
it = batchMutations(gen(), firstSize + secondSize);
return [
4,
it.next()
];
case 1:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: false,
value: {
mutations: [
first,
second
]
}
});
return [
4,
it.next()
];
case 2:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: true,
value: undefined
});
return [
2
];
}
});
})();
});
test('when max batch is not big enough to fit all values', function() {
return _async_to_generator(function() {
var first, second, gen, firstSize, it;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
first = {
createIfNotExists: {
_id: 'foo',
_type: 'something',
bar: 'baz'
}
};
second = {
patch: {
id: 'foo',
set: {
bar: 'baz'
}
}
};
gen = function gen() {
return _wrap_async_generator(function() {
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
first
];
case 1:
_state.sent();
return [
4,
second
];
case 2:
_state.sent();
return [
2
];
}
});
})();
};
firstSize = JSON.stringify(first).length;
it = batchMutations(gen(), firstSize);
return [
4,
it.next()
];
case 1:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: false,
value: {
mutations: [
first
]
}
});
return [
4,
it.next()
];
case 2:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: false,
value: {
mutations: [
second
]
}
});
return [
4,
it.next()
];
case 3:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: true,
value: undefined
});
return [
2
];
}
});
})();
});
test('when each mutation is bigger than max batch size', function() {
return _async_to_generator(function() {
var first, second, gen, it;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
first = {
createIfNotExists: {
_id: 'foo',
_type: 'something',
bar: 'baz'
}
};
second = {
patch: {
id: 'foo',
set: {
bar: 'baz'
}
}
};
gen = function gen() {
return _wrap_async_generator(function() {
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
first
];
case 1:
_state.sent();
return [
4,
second
];
case 2:
_state.sent();
return [
2
];
}
});
})();
};
it = batchMutations(gen(), 1);
return [
4,
it.next()
];
case 1:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: false,
value: {
mutations: [
first
]
}
});
return [
4,
it.next()
];
case 2:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: false,
value: {
mutations: [
second
]
}
});
return [
4,
it.next()
];
case 3:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: true,
value: undefined
});
return [
2
];
}
});
})();
});
test('when some mutations can be chunked, others not', function() {
return _async_to_generator(function() {
var first, second, third, gen, it;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
first = {
createIfNotExists: {
_id: 'foo',
_type: 'something',
bar: 'baz'
}
};
second = {
patch: {
id: 'foo',
set: {
bar: 'baz'
}
}
};
// Note: this is an array of mutations and should not be split up as it may be intentional to keep it in a transaction
// todo: is it ok to include other mutations in the same batch as a transaction?
third = [
{
createOrReplace: {
_id: 'foo',
_type: 'something',
bar: 'baz',
baz: 'qux'
}
},
{
patch: {
id: 'foo',
set: {
bar: 'baz'
}
}
}
];
gen = function gen() {
return _wrap_async_generator(function() {
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
first
];
case 1:
_state.sent();
return [
4,
second
];
case 2:
_state.sent();
return [
4,
third
];
case 3:
_state.sent();
return [
2
];
}
});
})();
};
it = batchMutations(gen(), byteLength(first) + byteLength(second));
return [
4,
it.next()
];
case 1:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: false,
value: {
mutations: [
first,
second
]
}
});
return [
4,
it.next()
];
case 2:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: false,
value: {
mutations: third
}
});
return [
2
];
}
});
})();
});
});
//todo: verify if this is the default behavior we want
test('transactions are always in the same batch, but might include other mutations if they fit', function() {
return _async_to_generator(function() {
var first, second, transaction, fourth, gen, it;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
first = {
createIfNotExists: {
_id: 'foo',
_type: 'something',
bar: 'baz'
}
};
second = {
patch: {
id: 'foo',
set: {
bar: 'baz'
}
}
};
// Note: this is an array of mutations and should not be split up as it may be intentional to keep it in a transaction
transaction = {
mutations: [
{
createOrReplace: {
_id: 'foo',
_type: 'something',
bar: 'baz',
baz: 'qux'
}
},
{
patch: {
id: 'foo',
set: {
bar: 'baz'
}
}
}
]
};
fourth = {
patch: {
id: 'another',
set: {
this: 'that'
}
}
};
gen = function gen() {
return _wrap_async_generator(function() {
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
first
];
case 1:
_state.sent();
return [
4,
second
];
case 2:
_state.sent();
return [
4,
transaction
];
case 3:
_state.sent();
return [
4,
fourth
];
case 4:
_state.sent();
return [
2
];
}
});
})();
};
it = batchMutations(gen(), [
first,
second,
transaction,
fourth
].reduce(function(l, m) {
return l + byteLength(m);
}, 0));
return [
4,
toArray(it)
];
case 1:
expect.apply(void 0, [
_state.sent()
]).toEqual([
{
mutations: [
first,
second
]
},
transaction,
{
mutations: [
fourth
]
}
]);
return [
4,
it.next()
];
case 2:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: true,
value: undefined
});
return [
2
];
}
});
})();
});
test('transactions are always batched as-is if preserveTransactions: true', function() {
return _async_to_generator(function() {
var first, second, transaction, fourth, gen, it;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
first = {
createIfNotExists: {
_id: 'foo',
_type: 'something',
bar: 'baz'
}
};
second = {
patch: {
id: 'foo',
set: {
bar: 'baz'
}
}
};
// Note: this is an array of mutations and should not be split up as it may be intentional to keep it in a transaction
transaction = {
mutations: [
{
createOrReplace: {
_id: 'foo',
_type: 'something',
bar: 'baz',
baz: 'qux'
}
},
{
patch: {
id: 'foo',
set: {
bar: 'baz'
}
}
}
]
};
fourth = {
patch: {
id: 'another',
set: {
this: 'that'
}
}
};
gen = function gen() {
return _wrap_async_generator(function() {
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
first
];
case 1:
_state.sent();
return [
4,
second
];
case 2:
_state.sent();
return [
4,
transaction
];
case 3:
_state.sent();
return [
4,
fourth
];
case 4:
_state.sent();
return [
2
];
}
});
})();
};
it = batchMutations(gen(), [
first,
second,
transaction,
fourth
].reduce(function(l, m) {
return l + byteLength(m);
}, 0));
return [
4,
it.next()
];
case 1:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: false,
value: {
mutations: [
first,
second
]
}
});
return [
4,
it.next()
];
case 2:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: false,
value: transaction
});
return [
4,
it.next()
];
case 3:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: false,
value: {
mutations: [
fourth
]
}
});
return [
4,
it.next()
];
case 4:
expect.apply(void 0, [
_state.sent()
]).toEqual({
done: true,
value: undefined
});
return [
2
];
}
});
})();
});