Introduce automatic fields based on other fields

This commit is contained in:
kpcyrd
2018-12-04 00:52:16 +01:00
parent 52db46c340
commit 41270f6611
12 changed files with 158 additions and 6 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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,

View File

@@ -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)
}
}

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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::*;

View File

@@ -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 {

View File

@@ -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)
}
}

View File

@@ -3,6 +3,7 @@ use diesel;
use diesel::prelude::*;
use models::*;
use ser;
use url;
#[derive(Identifiable, Queryable, Associations, Serialize, Deserialize, PartialEq, Debug)]
@@ -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,
@@ -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>,
@@ -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>>,
@@ -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 {

View File

@@ -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)
@@ -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))?)
},
};

View File

@@ -56,6 +56,7 @@ table! {
id -> Integer,
subdomain_id -> Integer,
value -> Text,
path -> Text,
status -> Nullable<Integer>,
body -> Nullable<Binary>,
unscoped -> Bool,