Skip to content
This repository has been archived by the owner on Sep 2, 2020. It is now read-only.

Commit

Permalink
Support stupid mysql 5.5, fix copying published badges
Browse files Browse the repository at this point in the history
  • Loading branch information
christensenep committed Feb 20, 2014
1 parent 5d264b5 commit ba290b1
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 35 deletions.
3 changes: 1 addition & 2 deletions app/models/badge.js
Expand Up @@ -60,7 +60,7 @@ module.exports = function getBadgeModel (key) {
});

delete badge.criteria;

badge.created = new Date();
Badge.put(badge, function (err, result) {
if (err)
return callback(err);
Expand All @@ -85,7 +85,6 @@ module.exports = function getBadgeModel (key) {
'badgeId',
'required',
'note',
'created',
'lastUpdated']
});

Expand Down
2 changes: 1 addition & 1 deletion app/models/image.js
Expand Up @@ -9,7 +9,7 @@ module.exports = function getImageModel (key) {
['id',
'mimetype',
'data',
'created',
'url',
'lastUpdated']
});

Expand Down
61 changes: 41 additions & 20 deletions app/views/badge.js
Expand Up @@ -177,18 +177,22 @@ function saveBadge(req, callback) {

async.parallel([
function(innerCallback) {
const criteria = req.body.criteria.slice(0,numCriteria).map(function(criterion) {
return {
id: criterion.id || null,
description: criterion.description,
required: criterion.required == 'on' ? 1 : 0,
note: criterion.note
};
});

badgeRow.setCriteria(criteria, function(err) {
return innerCallback(err);
});
if (criteria in req.body) {
const criteria = req.body.criteria.slice(0,numCriteria).map(function(criterion) {
return {
id: criterion.id || null,
description: criterion.description,
required: criterion.required == 'on' ? 1 : 0,
note: criterion.note
};
});

badgeRow.setCriteria(criteria, function(err) {
return innerCallback(err);
});
}

return innerCallback();
},
function(innerCallback) {
if (req.files) {
Expand All @@ -208,7 +212,8 @@ function saveBadge(req, callback) {
const imageQuery = {
id: badgeRow.imageId,
mimetype: type,
data: data
data: data,
url: null
};

Image.put(imageQuery, function(err, imageResult) {
Expand Down Expand Up @@ -297,13 +302,22 @@ exports.copy = function copy (req, res, next) {
if (err)
return res.send(500, err);

badge = openbadger.toBadgekitBadge(badge);
delete badge.id;
Badge.put(badge, function (err, result) {
Image.put({ url: badge.imageUrl }, function(err, imageResult) {
if (err)
return res.send(500, err);
return res.send(500,err);

return res.send(200, { location: res.locals.url('directory') + '?category=draft' })
badge = openbadger.toBadgekitBadge(badge);
delete badge.id;
badge.created = new Date();
delete badge.image;
badge.imageId = imageResult.insertId;

Badge.put(badge, function (err, result) {
if (err)
return res.send(500, err);

return res.send(200, { location: res.locals.url('directory') + '?category=draft' })
});
});
});
};
Expand All @@ -317,8 +331,15 @@ exports.image = function image (req, res, next) {

if (row) {
if (row.image.id !== null) {
res.type(row.image.mimetype);
return res.send(row.image.data);
if (row.image.url === null) {
res.type(row.image.mimetype);
return res.send(row.image.data);
}
else {
var location = row.image.url.toString('ascii');
res.header('Location', location);
return res.send(301, {location: location});
}
}
else {
res.sendfile(path.join(__dirname, '../static/images/default-badge.png'));
Expand Down
2 changes: 1 addition & 1 deletion app/views/directory.js
Expand Up @@ -138,7 +138,7 @@ exports.home = function home (req, res, next) {
exports.addBadge = function addBadge (req, res, next) {
const category = req.query.category || 'draft';

Badge.put({ name: 'New Badge', status: category }, function (err, result) {
Badge.put({ name: 'New Badge', status: category, created: new Date() }, function (err, result) {
if (err)
return res.send(500, err);

Expand Down
24 changes: 17 additions & 7 deletions migrations/20140220161432-timestamps.js
Expand Up @@ -6,17 +6,18 @@ exports.up = function(db, callback) {
async.series([
db.runSql.bind(db,
"ALTER TABLE `badge` "
+ "ADD `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,"
+ "ADD `created` TIMESTAMP DEFAULT '2000-01-01 00:00:00',"
+ "ADD `lastUpdated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
),
db.runSql.bind(db,
"ALTER TABLE `image` "
+ "ADD `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,"
+ "ADD `lastUpdated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
+ "ADD `lastUpdated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,"
+ "ADD `url` VARCHAR(255),"
+ "MODIFY `mimetype` VARCHAR(255) NULL,"
+ "MODIFY `data` LONGBLOB NULL"
),
db.runSql.bind(db,
"ALTER TABLE `criteria` "
+ "ADD `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,"
+ "ADD `lastUpdated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
)], callback);
};
Expand All @@ -28,14 +29,23 @@ async.series([
+ "DROP COLUMN `created`,"
+ "DROP COLUMN `lastUpdated`"
),
db.runSql.bind(db,
"UPDATE `image` "
+ "SET `mimetype`='' WHERE `mimetype` IS NULL"
),
db.runSql.bind(db,
"UPDATE `image` "
+ "SET `data`='' WHERE `data` IS NULL"
),
db.runSql.bind(db,
"ALTER TABLE `image` "
+ "DROP COLUMN `created`,"
+ "DROP COLUMN `lastUpdated`"
+ "DROP COLUMN `lastUpdated`,"
+ "DROP COLUMN `url`,"
+ "MODIFY `mimetype` VARCHAR(255) NOT NULL,"
+ "MODIFY `data` LONGBLOB NOT NULL"
),
db.runSql.bind(db,
"ALTER TABLE `criteria` "
+ "DROP COLUMN `created`,"
+ "DROP COLUMN `lastUpdated`"
)], callback);
};
6 changes: 2 additions & 4 deletions test/migration.test.js
Expand Up @@ -158,11 +158,11 @@ describe('Migrations', function () {
results.should.be.ok;
results.length.should.equal(0);
}),
$.sql("SELECT created,lastUpdated FROM image", function (results) {
$.sql("SELECT lastUpdated FROM image", function (results) {
results.should.be.ok;
results.length.should.equal(0);
}),
$.sql("SELECT created,lastUpdated FROM criteria", function (results) {
$.sql("SELECT lastUpdated FROM criteria", function (results) {
results.should.be.ok;
results.length.should.equal(0);
}),
Expand All @@ -172,9 +172,7 @@ describe('Migrations', function () {
}),
$.sqlError("SELECT created FROM badge", "ER_BAD_FIELD_ERROR"),
$.sqlError("SELECT lastUpdated FROM badge", "ER_BAD_FIELD_ERROR"),
$.sqlError("SELECT created FROM image", "ER_BAD_FIELD_ERROR"),
$.sqlError("SELECT lastUpdated FROM image", "ER_BAD_FIELD_ERROR"),
$.sqlError("SELECT created FROM criteria", "ER_BAD_FIELD_ERROR"),
$.sqlError("SELECT lastUpdated FROM criteria", "ER_BAD_FIELD_ERROR")
];
});
Expand Down

0 comments on commit ba290b1

Please sign in to comment.