Skip to content

Commit

Permalink
Merge pull request #47 from fengmk2/tag-module-id
Browse files Browse the repository at this point in the history
add module_id to tag table. #46
  • Loading branch information
dead-horse committed Dec 10, 2013
2 parents a995cd6 + 57f05d0 commit be4ffa4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
24 changes: 17 additions & 7 deletions controllers/registry/sync_module_worker.js
Expand Up @@ -91,7 +91,7 @@ SyncModuleWorker.prototype.next = function () {
return that.next();
}

that.log('[%s] Start...', pkg.name);
that.log('[%s] start...', pkg.name);
that._sync(pkg, function (err, versions) {
if (err) {
that.fails.push(pkg.name);
Expand Down Expand Up @@ -132,6 +132,10 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
var tags = {};
for (var i = 0; i < rows.length; i++) {
var r = rows[i];
if (!r.module_id) {
// no module_id, need to sync tags
continue;
}
tags[r.tag] = r.version;
}
ep.emit('existsTags', tags);
Expand All @@ -143,12 +147,18 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
var times = pkg.time || {};
var versions = [];
for (var v in times) {
var exists = map[v];
var version = pkg.versions[v];
if (!version || !version.dist) {
continue;
}
if (exists && exists.package.dist.shasum === version.dist.shasum) {
var exists = map[v] || {};
var sourceAuthor = exists.author;
if (Array.isArray(version.maintainers)) {
sourceAuthor = version.maintainers[0].name || exists.author;
}
if (exists.package && exists.package.dist.shasum === version.dist.shasum && exists.author === sourceAuthor) {
// * author make sure equal
// * shasum make sure equal
continue;
}
version.gmt_modified = Date.parse(times[v]);
Expand Down Expand Up @@ -204,7 +214,7 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
// sync tags
missingTags.forEach(function (item) {
Module.addTag(pkg.name, item[0], item[1], ep.done(function (result) {
that.log(' added tag %s:%s', item[0], item[1]);
that.log(' added tag %s:%s, module_id: %s', item[0], item[1], result && result.module_id);
ep.emit('addTag');
}));
});
Expand Down Expand Up @@ -298,7 +308,7 @@ SyncModuleWorker.prototype._syncOneVersion = function (versionIndex, sourcePacka
//make sure sync module have the correct author info
//only if can not get maintainers, use the username
var author = username;
if (Array.isArray(sourcePackage.maintainers)){
if (Array.isArray(sourcePackage.maintainers)) {
author = sourcePackage.maintainers[0].name || username;
}

Expand All @@ -316,8 +326,8 @@ SyncModuleWorker.prototype._syncOneVersion = function (versionIndex, sourcePacka
};
mod.package.dist = dist;

that.log(' [%s:%s] done, version: %s, size: %d',
sourcePackage.name, versionIndex, mod.version, dataSize);
that.log(' [%s:%s] done, author: %s, version: %s, size: %d',
sourcePackage.name, versionIndex, author, mod.version, dataSize);
Module.add(mod, ep.done(function (result) {
callback(null, result);
}));
Expand Down
2 changes: 2 additions & 0 deletions docs/db.sql
Expand Up @@ -49,9 +49,11 @@ CREATE TABLE `tag` (
`name` varchar(100) NOT NULL COMMENT 'module name',
`tag` varchar(30) NOT NULL COMMENT 'tag name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`module_id` bigint(20) unsigned NOT NULL COMMENT 'module id',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`, `tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module tag';
-- ALTER TABLE `tag` ADD `module_id` BIGINT( 20 ) UNSIGNED NOT NULL;

CREATE TABLE `total` (
`name` varchar(100) NOT NULL COMMENT 'total name',
Expand Down
22 changes: 15 additions & 7 deletions proxy/module.js
Expand Up @@ -109,21 +109,29 @@ exports.get = function (name, version, callback) {
};

var INSERT_TAG_SQL = 'INSERT INTO tag(gmt_create, gmt_modified, \
name, tag, version) \
VALUES(now(), now(), ?, ?, ?) \
ON DUPLICATE KEY UPDATE gmt_modified=now(), \
name, tag, version, module_id) \
VALUES(now(), now(), ?, ?, ?, ?) \
ON DUPLICATE KEY UPDATE gmt_modified=now(), module_id=VALUES(module_id), \
name=VALUES(name), tag=VALUES(tag), version=VALUES(version);';

var SELECT_MODULE_ID_SQL = 'SELECT id FROM module WHERE name=? AND version=?;';

exports.addTag = function (name, tag, version, callback) {
mysql.query(INSERT_TAG_SQL, [name, tag, version], function (err, result) {
mysql.queryOne(SELECT_MODULE_ID_SQL, [name, version], function (err, row) {
if (err) {
return callback(err);
}
callback(null, {id: result.insertId, gmt_modified: new Date()});
var module_id = row && row.id || 0;
mysql.query(INSERT_TAG_SQL, [name, tag, version, module_id], function (err, result) {
if (err) {
return callback(err);
}
callback(null, {id: result.insertId, gmt_modified: new Date(), module_id: module_id});
});
});
};

var SELECT_TAG_SQL = 'SELECT tag, version, gmt_modified FROM tag WHERE name=? AND tag=?;';
var SELECT_TAG_SQL = 'SELECT tag, version, gmt_modified, module_id FROM tag WHERE name=? AND tag=?;';

exports.getByTag = function (name, tag, callback) {
mysql.queryOne(SELECT_TAG_SQL, [name, tag], function (err, row) {
Expand All @@ -140,7 +148,7 @@ exports.removeTags = function (name, callback) {
mysql.query(DELETE_TAGS_SQL, [name], callback);
};

var SELECT_ALL_TAGS_SQL = 'SELECT tag, version, gmt_modified FROM tag WHERE name=?;';
var SELECT_ALL_TAGS_SQL = 'SELECT tag, version, gmt_modified, module_id FROM tag WHERE name=?;';

exports.listTags = function (name, callback) {
mysql.query(SELECT_ALL_TAGS_SQL, [name], callback);
Expand Down
34 changes: 34 additions & 0 deletions test/proxy/module.test.js
@@ -0,0 +1,34 @@
/**!
* cnpmjs.org - test/proxy/module.test.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

var should = require('should');
var mm = require('mm');
var mysql = require('../../common/mysql');
var Module = require('../../proxy/module');

describe('proxy/module.test.js', function () {
afterEach(mm.restore);

describe('addTag()', function () {
it('should add tag auto add module id', function (done) {
Module.addTag('mocha', 'test', '1.15.1', function (err, result) {
should.not.exist(err);
result.should.have.keys('id', 'gmt_modified', 'module_id');
done();
});
});
});
});

0 comments on commit be4ffa4

Please sign in to comment.