近期作數據遷移,採用nodejs框架,數據庫爲mysql。做爲一枚菜鳥,在編碼過程當中,遇到衆多奇葩問題,感謝民少給予的支持。node
因爲舊數據庫中的數據,在以前設計中存在衆多不合理的狀況,所以在數據遷移中,須要對舊數據庫中的相關數據作衆多的規範性處理:mysql
根據新系統的數據結構要求,存在以下問題:ajax
一、進行無效數據篩選過濾,並進行記錄。sql
二、同時存在外鍵關係也須要進行相應遷移。數據庫
三、實現數據庫的自動化轉移處理緩存
四、記錄無效及驗證不經過數據,以及相關的緣由。數據結構
五、舊數據中Id爲不規範的ID,須要進行。框架
完成分析後,絕對按下面流程進行數據遷移:函數
一、在經歷了對新舊數據庫結構的分析後,進行相應的舊數據庫中數據的導出。工具
二、根據具體新數據關係,先進行不存在外鍵關係的基礎數據導入
三、完成基礎數據的導入後,再進行相應的存在關聯關係的數據進行導入處理。
在導入過程當中,須要對基礎數據進行導入成功後,方可進行關聯數據的導入,知足外鍵關係的關聯。
預先設計2套,具體方案以下:
1、先將全部數據導出爲具體的data.js文件,存儲在本地的文件中,讀取這些數據文件進行遷移處理。
2、根據數據關係,經過將導出的數據進行緩存,讀取緩存進行數據直接遷移處理。
採用nodegrass+mysql+underscore+eventproxy等相應組建進行整個數據導入的處理。
nodegrass進行數據模擬請求操做,
mysql進行數據庫數據的讀取處理,
underscore爲集合處理工具類,
eventproxy進行嵌套回調函數工具。
一、主體程序代碼以下:
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()方法,進行數據的導入操做,因爲外鍵關聯的緣由,因此先進行基礎數據導入。
二、具體代碼以下:
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("登陸失敗。"); } }); }
二、新舊數據庫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進行綁定。 由此完成數據外鍵關係的遷移。
三、mysql多語句執行的代碼:
var connection = mysql.createConnection({ host: '192.168.1.110', user: 'root', password: '123456', database: 'data', multipleStatements: true });
multipleStatements: true:爲對應的多語句同時執行開關。
四、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的處理。
將錯誤數據及結果進行保存,方便數據的篩選過濾,避免數據的非正常丟失。
五、輸出相應錯誤數據代碼:
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狀況,暫時未進行實際測試。待後期再相應的整理。