Skip to content

Commit

Permalink
Merge pull request #44 from kpcyrd/automatic-fields
Browse files Browse the repository at this point in the history
Introduce automatic fields based on other fields
  • Loading branch information
kpcyrd committed Dec 4, 2018
2 parents 52db46c + 41270f6 commit 7235406
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 6 deletions.
25 changes: 25 additions & 0 deletions migrations/2018-12-03-143558_url_path/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
PRAGMA foreign_keys=off;

ALTER TABLE urls RENAME TO _urls_old;

CREATE TABLE urls (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
subdomain_id INTEGER NOT NULL,
value VARCHAR NOT NULL,
status INTEGER,
body BLOB,
unscoped BOOLEAN DEFAULT 0 NOT NULL,
online BOOLEAN,
title VARCHAR,
redirect VARCHAR,
FOREIGN KEY(subdomain_id) REFERENCES subdomains(id) ON DELETE CASCADE,
CONSTRAINT url_unique UNIQUE (value)
);

INSERT INTO urls (id, subdomain_id, value, status, body, unscoped, online, title, redirect)
SELECT id, subdomain_id, value, status, body, unscoped, online, title, redirect
FROM _urls_old;

DROP TABLE _urls_old;

PRAGMA foreign_keys=on;
26 changes: 26 additions & 0 deletions migrations/2018-12-03-143558_url_path/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
PRAGMA foreign_keys=off;

ALTER TABLE urls RENAME TO _urls_old;

CREATE TABLE urls (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
subdomain_id INTEGER NOT NULL,
value VARCHAR NOT NULL,
path VARCHAR NOT NULL,
status INTEGER,
body BLOB,
unscoped BOOLEAN DEFAULT 0 NOT NULL,
online BOOLEAN,
title VARCHAR,
redirect VARCHAR,
FOREIGN KEY(subdomain_id) REFERENCES subdomains(id) ON DELETE CASCADE,
CONSTRAINT url_unique UNIQUE (value)
);

INSERT INTO urls (id, subdomain_id, value, path, status, body, unscoped, online, title, redirect)
SELECT id, subdomain_id, value, '/', status, body, unscoped, online, title, redirect
FROM _urls_old;

DROP TABLE _urls_old;

PRAGMA foreign_keys=on;
1 change: 1 addition & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl Database {
Insert::Url(object) => self.insert_struct(NewUrl {
subdomain_id: object.subdomain_id,
value: &object.value,
path: &object.path,
status: object.status,
body: object.body.as_ref(),
online: object.online,
Expand Down
10 changes: 10 additions & 0 deletions src/models/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,13 @@ impl Printable<PrintableDomain> for NewDomainOwned {
})
}
}

pub type InsertDomain = NewDomainOwned;

impl LuaInsertToNewOwned for InsertDomain {
type Target = NewDomainOwned;

fn try_into_new(self) -> Result<NewDomainOwned> {
Ok(self)
}
}
10 changes: 10 additions & 0 deletions src/models/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ impl Printable<PrintableEmail> for NewEmailOwned {
}
}

pub type InsertEmail = NewEmailOwned;

impl LuaInsertToNewOwned for InsertEmail {
type Target = NewEmailOwned;

fn try_into_new(self) -> Result<NewEmailOwned> {
Ok(self)
}
}

#[derive(Identifiable, AsChangeset, Serialize, Deserialize, Debug)]
#[table_name="emails"]
pub struct EmailUpdate {
Expand Down
10 changes: 10 additions & 0 deletions src/models/ipaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,16 @@ impl Printable<PrintableIpAddr> for NewIpAddrOwned {
}
}

pub type InsertIpAddr = NewIpAddrOwned;

impl LuaInsertToNewOwned for InsertIpAddr {
type Target = NewIpAddrOwned;

fn try_into_new(self) -> Result<NewIpAddrOwned> {
Ok(self)
}
}

#[derive(Identifiable, AsChangeset, Serialize, Deserialize, Debug)]
#[table_name="ipaddrs"]
pub struct IpAddrUpdate {
Expand Down
6 changes: 6 additions & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ macro_rules! display_detailed {
};
}

pub trait LuaInsertToNewOwned {
type Target;

fn try_into_new(self) -> Result<Self::Target>;
}

mod domain;
pub use self::domain::*;

Expand Down
10 changes: 10 additions & 0 deletions src/models/subdomain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ impl Printable<PrintableSubdomain> for NewSubdomainOwned {
}
}

pub type InsertSubdomain = NewSubdomainOwned;

impl LuaInsertToNewOwned for InsertSubdomain {
type Target = NewSubdomainOwned;

fn try_into_new(self) -> Result<NewSubdomainOwned> {
Ok(self)
}
}

#[derive(Identifiable, AsChangeset, Serialize, Deserialize, Debug)]
#[table_name="subdomains"]
pub struct SubdomainUpdate {
Expand Down
10 changes: 10 additions & 0 deletions src/models/subdomain_ipaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,13 @@ impl Printable<PrintableSubdomainIpAddr> for NewSubdomainIpAddr {
})
}
}

pub type InsertSubdomainIpAddr = NewSubdomainIpAddr;

impl LuaInsertToNewOwned for InsertSubdomainIpAddr {
type Target = NewSubdomainIpAddr;

fn try_into_new(self) -> Result<NewSubdomainIpAddr> {
Ok(self)
}
}
36 changes: 36 additions & 0 deletions src/models/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use diesel;
use diesel::prelude::*;
use models::*;
use ser;
use url;


#[derive(Identifiable, Queryable, Associations, Serialize, Deserialize, PartialEq, Debug)]
Expand All @@ -12,6 +13,7 @@ pub struct Url {
pub id: i32,
pub subdomain_id: i32,
pub value: String,
pub path: String,
pub status: Option<i32>,
pub body: Option<Vec<u8>>,
pub unscoped: bool,
Expand Down Expand Up @@ -214,6 +216,7 @@ impl Detailed for Url {
pub struct NewUrl<'a> {
pub subdomain_id: i32,
pub value: &'a str,
pub path: &'a str,
pub status: Option<i32>,
pub body: Option<&'a Vec<u8>>,
pub online: Option<bool>,
Expand Down Expand Up @@ -254,6 +257,7 @@ impl<'a> Upsertable<Url> for NewUrl<'a> {
pub struct NewUrlOwned {
pub subdomain_id: i32,
pub value: String,
pub path: String,
pub status: Option<i32>,
#[serde(deserialize_with="ser::opt_string_or_bytes")]
pub body: Option<Vec<u8>>,
Expand All @@ -272,6 +276,38 @@ impl Printable<PrintableUrl> for NewUrlOwned {
}
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InsertUrl {
pub subdomain_id: i32,
pub value: String,
pub status: Option<i32>,
#[serde(deserialize_with="ser::opt_string_or_bytes")]
pub body: Option<Vec<u8>>,
pub online: Option<bool>,
pub title: Option<String>,
pub redirect: Option<String>,
}

impl LuaInsertToNewOwned for InsertUrl {
type Target = NewUrlOwned;

fn try_into_new(self) -> Result<NewUrlOwned> {
let url = url::Url::parse(&self.value)?;
let path = url.path().to_string();

Ok(NewUrlOwned {
subdomain_id: self.subdomain_id,
value: self.value,
path,
status: self.status,
body: self.body,
online: self.online,
title: self.title,
redirect: self.redirect,
})
}
}

#[derive(Identifiable, AsChangeset, Serialize, Deserialize, Debug)]
#[table_name="urls"]
pub struct UrlUpdate {
Expand Down
19 changes: 13 additions & 6 deletions src/runtime/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ use models::*;
use json::LuaJsonValue;


pub fn try_into_new<T: LuaInsertToNewOwned>(x: LuaJsonValue) -> Result<T::Target>
where for<'de> T: serde::Deserialize<'de>
{
structs::from_lua::<T>(x)?
.try_into_new()
}

pub fn db_add(lua: &mut hlua::Lua, state: Arc<State>) {
lua.set("db_add", hlua::function2(move |family: String, object: AnyLuaValue| -> Result<Option<i32>> {
let family = Family::from_str(&family)
Expand All @@ -19,27 +26,27 @@ pub fn db_add(lua: &mut hlua::Lua, state: Arc<State>) {

let object = match family {
Family::Domain => {
Insert::Domain(structs::from_lua::<NewDomainOwned>(object)
Insert::Domain(try_into_new::<InsertDomain>(object)
.map_err(|e| state.set_error(e))?)
},
Family::Subdomain => {
Insert::Subdomain(structs::from_lua::<NewSubdomainOwned>(object)
Insert::Subdomain(try_into_new::<InsertSubdomain>(object)
.map_err(|e| state.set_error(e))?)
},
Family::IpAddr => {
Insert::IpAddr(structs::from_lua::<NewIpAddrOwned>(object)
Insert::IpAddr(try_into_new::<InsertIpAddr>(object)
.map_err(|e| state.set_error(e))?)
},
Family::SubdomainIpAddr => {
Insert::SubdomainIpAddr(structs::from_lua::<NewSubdomainIpAddr>(object)
Insert::SubdomainIpAddr(try_into_new::<InsertSubdomainIpAddr>(object)
.map_err(|e| state.set_error(e))?)
},
Family::Url => {
Insert::Url(structs::from_lua::<NewUrlOwned>(object)
Insert::Url(try_into_new::<InsertUrl>(object)
.map_err(|e| state.set_error(e))?)
},
Family::Email => {
Insert::Email(structs::from_lua::<NewEmailOwned>(object)
Insert::Email(try_into_new::<InsertEmail>(object)
.map_err(|e| state.set_error(e))?)
},
};
Expand Down
1 change: 1 addition & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ table! {
id -> Integer,
subdomain_id -> Integer,
value -> Text,
path -> Text,
status -> Nullable<Integer>,
body -> Nullable<Binary>,
unscoped -> Bool,
Expand Down

0 comments on commit 7235406

Please sign in to comment.