Browse Source

migrations improved to make notes/shortcuts 1:1

master
Christian Müller 10 years ago
parent
commit
cfbc676427
  1. 57
      src/migrate.js

57
src/migrate.js

@ -13,19 +13,40 @@ var sequelize = new Sequelize('database', null, null, { @@ -13,19 +13,40 @@ var sequelize = new Sequelize('database', null, null, {
});
var Note = sequelize.define('Note', {
id: { type: Sequelize.INTEGER, autoIncrement: true, unique: true, primaryKey: true },
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
unique: true,
primaryKey: true
},
text: Sequelize.TEXT,
published: { type: Sequelize.DATE, defaultValue: Sequelize.NOW },
edited: { type: Sequelize.DATE, allowNull: true, defaultValue: null },
published: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
},
edited: {
type: Sequelize.DATE,
allowNull: true,
defaultValue: null
},
publisher: Sequelize.STRING(32),
password: Sequelize.STRING(16),
views: Sequelize.INTEGER,
});
var Link = sequelize.define('Link', {
id: { type: Sequelize.STRING, unique: true, primaryKey: true },
lastUsage: { type: Sequelize.DATE, allowNull: true, defaultValue: null },
params: Sequelize.STRING
id: {
type: Sequelize.STRING,
unique: true,
primaryKey: true
},
lastUsage: {
type: Sequelize.DATE,
allowNull: true,
defaultValue: null
},
params: Sequelize.STRING,
deprecatedId: Sequelize.STRING
});
Note.hasMany(Link);
@ -56,12 +77,13 @@ sequelize.sync().then(function () { @@ -56,12 +77,13 @@ sequelize.sync().then(function () {
views: views[id],
}).then(note => {
links.forEach(LinkId => {
var createLink = LinkId => {
client.hget("short-url", LinkId, function(err, result) {
result = result.replace(/:([\w_-]+)\s/g, '"$1":');
var obj = {};
if (result) {
result = result.replace(/:([\w_-]+)\s/g, '"$1":');
try {
obj = JSON.parse(result);
delete obj.title;
@ -71,9 +93,10 @@ sequelize.sync().then(function () { @@ -71,9 +93,10 @@ sequelize.sync().then(function () {
} catch (e) {
return console.log("PARSE ERROR FOR", result)
}
}
Link.create({
id: LinkId,
deprecatedId: id,
params: Object.keys(obj).length == 0 ? null : JSON.stringify(obj)
}).then(link => {
@ -82,12 +105,20 @@ sequelize.sync().then(function () { @@ -82,12 +105,20 @@ sequelize.sync().then(function () {
});
});
});
};
Link.create({ id: id }).then(link => {
link.setNote(note);
note.addLink(link);
if (links.length == 0) {
var tmp = id.split("/");
var paramString = '{:day "' + tmp[2] +
'", :month "' + tmp[1] + '", :title "' + tmp[3] + '", :year "' + tmp[0] + '"}';
client.hget("short-url", paramString, function(err, result) {
if (!result) throw("oops:" + paramString + ":" + id);
createLink(result);
});
} else createLink(links[links.length - 1]);
});
})

Loading…
Cancel
Save