/
index.js
108 lines (92 loc) · 3.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { readFileSync } from 'fs';
import { resolve, dirname } from 'path';
import { expect } from 'chai';
import Sequelize from 'sequelize';
import typescript from 'typescript';
import coffeescript from 'coffee-script';
import helper from '../helper';
import Umzug from '../../src';
describe('custom resolver', () => {
beforeEach(function () {
helper.clearTmp();
this.storagePath = resolve(__dirname, '../tmp/storage.sqlite');
this.sequelize = new Sequelize('database', 'username', 'password', {
dialect: 'sqlite',
storage: this.storagePath,
logging: false,
});
this.umzug = () => {
if (!this.path || !this.pattern) {
throw new Error('path and pattern must be defined');
}
return new Umzug({
migrations: {
path: this.path,
params: [
this.sequelize.getQueryInterface(),
this.sequelize.constructor,
],
pattern: this.pattern,
customResolver: this.customResolver,
},
storage: 'sequelize',
storageOptions: {
path: this.storagePath,
sequelize: this.sequelize,
},
});
};
this.verifyTables = async () => {
const tables = await this.sequelize.query('select * from sqlite_master where type=\'table\'');
expect(tables.sort()).to.deep.equal(['SequelizeMeta', 'thing', 'user']);
};
});
it('resolves javascript files if no custom resolver is defined', async function () {
this.pattern = /\.js$/;
this.path = resolve(__dirname, 'javascript');
this.customResolver = undefined;
await this.umzug().up();
await this.verifyTables();
});
it('can resolve sql files', async function () {
this.pattern = /\.sql$/;
this.path = resolve(__dirname, 'sql');
this.customResolver = path => ({
up: () => this.sequelize.query(readFileSync(path, 'utf8')),
});
await this.umzug().up();
await this.verifyTables();
});
it('can resolve typescript files', async function () {
this.pattern = /\.ts$/;
this.path = resolve(__dirname, 'typescript');
this.customResolver = path => {
const typescriptSrc = readFileSync(path, 'utf8');
const transpiled = typescript.transpileModule(typescriptSrc, {});
const Module = module.constructor;
const m = new Module(path, module.parent);
m.filename = path;
m.paths = Module._nodeModulePaths(dirname(path));
m._compile(transpiled.outputText, path);
return m.exports;
};
await this.umzug().up();
await this.verifyTables();
});
it('can resolve coffeescript files', async function () {
this.pattern = /\.coffee$/;
this.path = resolve(__dirname, 'coffeescript');
this.customResolver = path => {
const coffeescriptSrc = readFileSync(path, 'utf8');
const javascriptSrc = coffeescript.compile(coffeescriptSrc);
const Module = module.constructor;
const m = new Module(path, module.parent);
m.filename = path;
m.paths = Module._nodeModulePaths(dirname(path));
m._compile(javascriptSrc, path);
return m.exports;
};
await this.umzug().up();
await this.verifyTables();
});
});