Skip to content

Commit

Permalink
[chore] Minor fixes for server compatibility
Browse files Browse the repository at this point in the history
Also, fix the uniqueness constraint on notes
  • Loading branch information
zmbush committed Apr 26, 2019
1 parent 663c016 commit 0787ff0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions db/migrations/2019-04-26-020705_fix_note_constraint/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE notes
DROP CONSTRAINT notes_title_user_id_parent_note_id,
ADD CONSTRAINT notes_title_id_parent_note_id UNIQUE(id, title, parent_note_id);
3 changes: 3 additions & 0 deletions db/migrations/2019-04-26-020705_fix_note_constraint/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE notes
DROP CONSTRAINT notes_title_id_parent_note_id,
ADD CONSTRAINT notes_title_user_id_parent_note_id UNIQUE(title, user_id, parent_note_id);
34 changes: 31 additions & 3 deletions db/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,12 @@ impl User {
db.transaction::<(), diesel::result::Error, _>(|| {
use crate::schema::{note_tags_id::dsl::*, tags::dsl::*};

diesel::insert_into(tags)
// TODO: Server needs Postgres 9.5 to support ON CONFLICT DO NOTHING
// BODY: In the meantime, we will just ignore the result from the following execute.
let _ = diesel::insert_into(tags)
.values(&set_tags.iter().map(|t| tag.eq(t)).collect::<Vec<_>>())
.on_conflict_do_nothing()
.execute(db)?;
// .on_conflict_do_nothing()
.execute(db);

let all_tags = tags.filter(tag.eq_any(set_tags)).load::<Tag>(db)?;

Expand Down Expand Up @@ -343,6 +345,32 @@ mod test {
});
}

#[test]
fn test_modifying_tags() {
let db = db().unwrap();
db.test_transaction::<_, diesel::result::Error, _>(|| {
let user = test_user(&db);
let note = user
.new_note(
&NewNote {
title: "Title".to_owned(),
body: "Body".to_owned(),
parent_note_id: None,
},
&db,
)
.unwrap();

let note = user
.set_note_tags(note.id, &["Tag1".to_owned(), "Tag2".to_owned()], &db)
.unwrap();

assert_eq!(note.tags, vec!["Tag1".to_owned(), "Tag2".to_owned()]);

Ok(())
});
}

#[test]
fn test_creating_user() {
let db = db().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
module.exports = {
entry: './js/index.tsx',
output: {
filename: 'js/[name].bundle.js',
filename: 'js/[name].[chunkhash].js',
chunkFilename: 'js/[name].[chunkhash].js',

publicPath: '/dist/',
Expand Down

0 comments on commit 0787ff0

Please sign in to comment.