nodejs简单数据迁移demo

近期做数据迁移,采用nodejs框架,数据库为mysql。作为一枚菜鸟,在编码过程中,遇到众多奇葩问题,感谢民少给予的支持。

由于旧数据库中的数据,在之前设计中存在众多不合理的情况,因此在数据迁移中,需要对旧数据库中的相关数据做众多的规范性处理:

根据新系统的数据结构要求,存在如下问题:

1、进行无效数据筛选过滤,并进行记录。

2、同时存在外键关系也需要进行相应迁移。

3、实现数据库的自动化转移处理

4、记录无效及验证不通过数据,以及相关的原因。

5、旧数据中Id为不规范的ID,需要进行。

完成分析后,绝对按下面流程进行数据迁移:

1、在经历了对新旧数据库结构的分析后,进行相应的旧数据库中数据的导出。

2、根据具体新数据关系,先进行不存在外键关系的基础数据导入

3、完成基础数据的导入后,再进行相应的存在关联关系的数据进行导入处理。

在导入过程中,需要对基础数据进行导入成功后,方可进行关联数据的导入,满足外键关系的关联。

预先设计2套,具体方案如下:

一、先将所有数据导出为具体的data.js文件,存储在本地的文件中,读取这些数据文件进行迁移处理。

二、根据数据关系,通过将导出的数据进行缓存,读取缓存进行数据直接迁移处理。

采用nodegrass+mysql+underscore+eventproxy等相应组建进行整个数据导入的处理。

nodegrass进行数据模拟请求操作,

mysql进行数据库数据的读取处理,

underscore为集合处理工具类,

eventproxy进行嵌套回调函数工具。

1、主体程序代码如下:

function execExport(i) {
    var fileName = modules[i];
    var filePath = path.join(dir, fileName);
    require(filePath).export(connection, exportComplete(i, new Date().getTime()), ' limit 10;');
}


function exportComplete(i, start) {
    return function () {
        var fileName = modules[i];
        var filePath = path.join(dir, fileName);
        console.log('%s导出完成,行数: %s, 耗时: %s 秒', filePath, $.size(require(filePath).rows), parseInt((new Date().getTime() - start) / 1000, 10));
        if ((i + 1) < modules.length)
            return execExport(i + 1);
        console.log('______________完成所有导出____________');
        execImportBase();

    };
}

execExport(0);

通过调用execExport(0);,以递归的方式,进行旧数据库数据的导出,记录数据记录以及导出耗时情况。

在导出数据结束后,通过execImportBase()方法,进行数据的导入操作,由于外键关联的原因,所以先进行基础数据导入。

2、具体代码如下:

nodegrass模拟请求代码:

function execImportBase() {
    ajax.post('/user/login', {
        name: '××××××',
        pwd: '××××××',
        rememberPwd: true
    }, function (resp, headers) {
        if (resp.success) {
            console.log("登录成功,开始数据导入。");
            require('./server/dict').import();
        }
        else {
            console.log("登录失败。");
        }
    });
}

2、新旧数据库Id标识字段的关联处理代码:

exports.import = function () {
    var ep = new EP();
    ep.after('importAccount', $.size(exports.rows), function () {
        console.log("end Import Account");
        write.writeFile(fails, 'Account');
        require('./shop').import();
    });
    $.each(exports.rows, function (d) {
        ajax.post('/user/register', d, function (resp) {
            if (!resp.success) {
                d.resp = resp;
                fails.push(d);
            } else {
                d.newId = resp.data;
            }
            ep.emit('importAccount');
        });
    });
};

数据中保存成功后,将新数据的Id字段返回,让引用的外键关联表对应的引用字段,使用newId进行绑定。 由此完成数据外键关系的迁移。

3、mysql多语句执行的代码:

var connection = mysql.createConnection({
    host: '192.168.1.110',
    user: 'root',
    password: '123456',
    database: 'data',
    multipleStatements: true
});
multipleStatements: true:为对应的多语句同时执行开关。

4、eventproxy嵌套回调处理代码:

ep.after('importAccount', $.size(exports.rows), function () {
        console.log("end Import Account");
        write.writeFile(fails, 'Account');
        require('./shop').import();
    });
    $.each(exports.rows, function (d) {
        ajax.post('/user/register', d, function (resp) {
            if (!resp.success) {
                d.resp = resp;
                fails.push(d);
            } else {
                d.newId = resp.data;
            }
            ep.emit('importAccount');
        });
    });

使用eventproxy进行循环的请求发送,为外键表关联进行相应数据的处理准备。

在请求完成的同时,根据返回结果,通过fails.push()进行错误数据的记录,以及newId的处理。

将错误数据及结果进行保存,方便数据的筛选过滤,避免数据的非正常丢失。

5、输出相应错误数据代码:

var fs = require('fs');
var path = require('path');
var $ = require('underscore');

var dir = path.join(__dirname, 'failds');

//打印错误数据
exports.writeFile = function (data, name) {
    fs.writeFile(path.join(dir, name + '.js'), JSON.stringify(data), function (err) {
        if (err)
            return console.log(err);
        if ($.size(data) > 0)
            console.log('导入' + name + '出现 %s 条错误数据!', $.size(data));
    });
};

简单的小例子,本人菜鸟一枚,如果有不足之处,还请指正。

关于本例子的性能及bug情况,暂时未进行实际测试。待后期再相应的整理。