Skip to content

Commit

Permalink
Add database code
Browse files Browse the repository at this point in the history
  • Loading branch information
kpcyrd committed Sep 12, 2018
1 parent f4b551c commit 42ca33f
Show file tree
Hide file tree
Showing 13 changed files with 376 additions and 5 deletions.
137 changes: 137 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ colored = "1.6"
lazy_static = "1.0"
shellwords = "0.1"
publicsuffix = { version="1.5", default-features=false }
diesel = { version = "1.0.0", features = ["sqlite"] }
diesel_migrations = { version = "1.3.0", features = ["sqlite"] }

# https://crates.io/crates/tor_control
5 changes: 5 additions & 0 deletions diesel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# For documentation on how to configure this file,
# see diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/schema.rs"
Empty file added migrations/.gitkeep
Empty file.
5 changes: 5 additions & 0 deletions migrations/2018-09-12-102647_create_domains/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP TABLE urls;
DROP TABLE subdomain_ipaddrs;
DROP TABLE subdomains;
DROP TABLE ipaddrs;
DROP TABLE domains;
40 changes: 40 additions & 0 deletions migrations/2018-09-12-102647_create_domains/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
CREATE TABLE domains (
id INTEGER PRIMARY KEY AUTOINCREMENT,
value VARCHAR NOT NULL,
CONSTRAINT domain_unique UNIQUE (value)
);

CREATE TABLE subdomains (
id INTEGER PRIMARY KEY AUTOINCREMENT,
domain_id INTEGER NOT NULL,
value VARCHAR NOT NULL,
FOREIGN KEY(domain_id) REFERENCES domains(id),
CONSTRAINT subdomain_unique UNIQUE (value)
);

/* family maybe not needed */
CREATE TABLE ipaddrs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
family VARCHAR NOT NULL,
value VARCHAR NOT NULL,
CONSTRAINT ipaddr_unique UNIQUE (value)
);

CREATE TABLE subdomain_ipaddrs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
subdomain_id INTEGER NOT NULL,
ip_addr_id INTEGER NOT NULL,
FOREIGN KEY(subdomain_id) REFERENCES domains(id),
FOREIGN KEY(ip_addr_id) REFERENCES ipaddrs(id),
CONSTRAINT subdomain_ipaddr_unique UNIQUE (subdomain_id, ip_addr_id)
);

CREATE TABLE urls (
id INTEGER PRIMARY KEY AUTOINCREMENT,
subdomain_id INTEGER NOT NULL,
value VARCHAR NOT NULL,
status INTEGER,
body BLOB,
FOREIGN KEY(subdomain_id) REFERENCES domains(id),
CONSTRAINT url_unique UNIQUE (value)
);
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub use failure::{Error, ResultExt};
pub type Result<T> = ::std::result::Result<T, Error>;
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ extern crate colored;
extern crate failure;
extern crate shellwords;
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate diesel;
#[macro_use] extern crate diesel_migrations;

pub mod errors;
pub mod engine;
pub mod migrations;
pub mod models;
pub mod schema;
pub mod shell;
pub mod term;
pub mod worker;
Expand All @@ -15,5 +21,11 @@ pub mod worker;
fn main() {
env_logger::init();

shell::run();
if let Err(err) = shell::run() {
eprintln!("Error: {}", err);
for cause in err.iter_chain().skip(1) {
eprintln!("Because: {}", cause);
}
std::process::exit(1);
}
}
11 changes: 11 additions & 0 deletions src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![allow(unused_imports)]
use errors::*;

use diesel::sqlite::*;

embed_migrations!();

pub fn run(conn: &SqliteConnection) -> Result<()> {
embedded_migrations::run(conn)?;
Ok(())
}
Loading

0 comments on commit 42ca33f

Please sign in to comment.