Skip to content

Commit

Permalink
saving my activity into sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasa committed Dec 28, 2020
1 parent a1ef25c commit 5c3f532
Show file tree
Hide file tree
Showing 13 changed files with 457 additions and 2 deletions.
182 changes: 182 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ clap = "3.0.0-beta.2"
structopt = "0.3.13"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
uuid = { version = "0.8", features = ["serde", "v4"] }
diesel = { version = "1.4.5", features = ["sqlite", "chrono"] }




30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# excavator
![CI](https://github.com/ichnion/excavator/workflows/CI/badge.svg)


#### Diesel with SQLite setup

* Diesel-cli install

```
cargo install diesel_cli
```

* Create SQLite Database file

```
diesel setup --database-url=ichneos.db
```

* Create Migration scripts

```
diesel migration generate google_my_activity
```

* Populate the migration scripts


* Run the migration

```
diesel --database-url=ichneos.db migration run
```
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/db/schema.rs"
Binary file added ichneos.db
Binary file not shown.
Empty file added migrations/.gitkeep
Empty file.
8 changes: 8 additions & 0 deletions migrations/2020-12-24-070800_google_my_activity/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--
-- Tables for Google derrived data
--
drop table google_my_activity;
drop table activity_location_info;
drop table activity_sub_title;
drop table activity_details;
drop table activity_products;
46 changes: 46 additions & 0 deletions migrations/2020-12-24-070800_google_my_activity/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--
-- Tables for Google derrived data
--
CREATE TABLE google_my_activity (
uuid TEXT NOT NULL PRIMARY KEY,
header TEXT NOT NULL,
title TEXT NOT NULL,
title_url TEXT,
time TEXT NOT NULL,
UNIQUE(header,title,time)
);

CREATE TABLE activity_location_info (
id INTEGER PRIMARY KEY,
a_uuid TEXT NOT NULL ,
name TEXT,
url TEXT,
source TEXT,
FOREIGN KEY (a_uuid) REFERENCES google_my_activity(uuid) ON DELETE CASCADE
);
CREATE INDEX gact1 on activity_location_info(a_uuid);

CREATE TABLE activity_sub_title (
id INTEGER PRIMARY KEY,
a_uuid TEXT NOT NULL,
name TEXT,
url TEXT,
FOREIGN KEY (a_uuid) REFERENCES google_my_activity(uuid) ON DELETE CASCADE
);
CREATE INDEX gact2 on activity_sub_title(a_uuid);

CREATE TABLE activity_details (
id INTEGER PRIMARY KEY,
a_uuid TEXT NOT NULL,
name TEXT,
FOREIGN KEY (a_uuid) REFERENCES google_my_activity(uuid) ON DELETE CASCADE
);
CREATE INDEX gact3 on activity_details(a_uuid);

CREATE TABLE activity_products (
id INTEGER PRIMARY KEY,
a_uuid TEXT NOT NULL,
name TEXT,
FOREIGN KEY (a_uuid) REFERENCES google_my_activity(uuid) ON DELETE CASCADE
);
CREATE INDEX gact4 on activity_products(a_uuid);
43 changes: 43 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use diesel::{prelude::*, sqlite::SqliteConnection};

pub mod models;
pub mod schema;

pub fn establish_connection() -> SqliteConnection {
let db = "./ichneos.db";
SqliteConnection::establish(db)
.unwrap_or_else(|_| panic!("Error connecting to {}", db))
}

pub fn save_activity(connection: &SqliteConnection,
uuid: &str, header: &str, title: &str,
title_url: &str, time: &str) {

let task = models::MyActivityEntity {
uuid : uuid.to_string(),
header : header.to_string(),
title : title.to_string(),
title_url : title_url.to_string(),
time : time.to_string()
};

diesel::insert_into(schema::google_my_activity::table)
.values(&task)
.execute(connection)
.expect("Error inserting new task");
}

pub fn save_sub_title(connection: &SqliteConnection,
a_uuid: &str, name: &str, url: &str ) {

let task = models::SubTitlesEntity {
a_uuid : a_uuid.to_string(),
name : name.to_string(),
url : url.to_string()
};

diesel::insert_into(schema::activity_sub_title::table)
.values(&task)
.execute(connection)
.expect("Error inserting new task");
}
Loading

0 comments on commit 5c3f532

Please sign in to comment.