use on conflict

master
Vitaliy Filippov 2016-06-28 01:07:35 +03:00
parent c152ffa122
commit a5c87c61ea
2 changed files with 7 additions and 2 deletions

3
db.sql
View File

@ -23,10 +23,10 @@ create unique index folders_name on folders (name);
create table messages (
id serial not null primary key,
folder_id int not null,
uid int not null,
messageid varchar(1000) not null,
inreplyto varchar(1000) not null,
folder_id int not null,
subject text not null,
from_email varchar(255) not null,
from_name varchar(255) not null,
@ -41,4 +41,5 @@ create table messages (
flags smallint not null,
foreign key (folder_id) references folders (id) on delete cascade on update cascade
);
create unique index messages_unique on messages (folder_id, uid);
create index messages_messageid on messages (messageid);

View File

@ -62,7 +62,11 @@ function* main(thread, account)
.where({ account_id: accountId, name: box.name }).returning('id').rows(thread);
if (err) throw err;
if (rows[0] && rows[0].id)
{
// IMAP sync: http://tools.ietf.org/html/rfc4549
// TODO: check old uidvalidity
boxId = rows[0].id;
}
else
{
var [ err, row ] = yield pg.insert('folders', {
@ -126,7 +130,7 @@ function* main(thread, account)
msgrow.flags = (msgrow.flags & ~8) | (msgrow.flags & 8 ? 0 : 8); // invert "\seen" (unread) flag
yield msg.once('end', thread);
console.log(msgrow.time+' '+msgrow.from_email+' '+msgrow.subject);
var [ err, row ] = yield pg.insert('messages', msgrow).run(thread);
var [ err, row ] = yield pg.raw(pg.insert('messages', msgrow)+' ON CONFLICT (folder_id, uid) DO UPDATE SET flags=excluded.flags').run(thread);
if (err) throw err;
});
});