From f8c446ce74329fc5844e0fc1fd82e618242196f4 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 28 Mar 2019 03:33:14 -0500 Subject: migrate and revert executable, query insert and delete test --- src/bin/migrate.rs | 14 ++++++++++++++ src/bin/revert.rs | 10 ++++++++++ src/constants.rs | 5 +++++ src/lib.rs | 3 +++ src/mass.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/math.rs | 13 +++++++++++++ src/schema.rs | 13 +++++++++++++ 7 files changed, 99 insertions(+) create mode 100644 src/bin/migrate.rs create mode 100644 src/bin/revert.rs create mode 100644 src/schema.rs (limited to 'src') diff --git a/src/bin/migrate.rs b/src/bin/migrate.rs new file mode 100644 index 0000000..8d269a4 --- /dev/null +++ b/src/bin/migrate.rs @@ -0,0 +1,14 @@ +use diesel::pg::PgConnection; +use diesel::prelude::*; + +use space::math::get_db_url; + +fn main() { + let connection = PgConnection::establish(&get_db_url()).expect("Cannot connect"); + migrations_internals::run_pending_migrations(&connection).expect("Cannot run migrations"); + + //migrations_internals::revert_latest_migration(&connection) + // .expect("Cannot revert migrations"); + //migrations_internals::revert_latest_migration(&connection) + // .expect("Cannot revert migrations"); +} diff --git a/src/bin/revert.rs b/src/bin/revert.rs new file mode 100644 index 0000000..cd26968 --- /dev/null +++ b/src/bin/revert.rs @@ -0,0 +1,10 @@ +use diesel::pg::PgConnection; +use diesel::prelude::*; + +use space::math::get_db_url; + +fn main() { + let connection = PgConnection::establish(&get_db_url()).expect("Cannot connect"); + migrations_internals::revert_latest_migration(&connection).expect("Cannot revert migrations"); + migrations_internals::revert_latest_migration(&connection).expect("Cannot revert migrations"); +} diff --git a/src/constants.rs b/src/constants.rs index 6f2cf6f..ad73b19 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -29,3 +29,8 @@ pub const CRUDE_MINERALS_SIZE: usize = 10; pub const SLEEP_DURATION: u64 = 100; pub const FLOAT_PRECISION: f64 = 0.001; + +pub const POSTGRES_USERNAME: &str = "space"; +pub const POSTGRES_PASSWORD: &str = "space"; +pub const POSTGRES_IP: &str = "localhost"; +pub const POSTGRES_DB_NAME: &str = "space_db"; diff --git a/src/lib.rs b/src/lib.rs index e398dba..26dee5c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ #[macro_use] extern crate serde_derive; +#[macro_use] +extern crate diesel; pub mod client; pub mod constants; @@ -7,5 +9,6 @@ pub mod item; pub mod mass; pub mod math; pub mod modules; +pub mod schema; pub mod server_connection; pub mod storage; diff --git a/src/mass.rs b/src/mass.rs index 820173f..ed859a4 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -15,6 +15,7 @@ use crate::modules::navigation::Navigation; use crate::modules::refinery::Refinery; use crate::modules::tractorbeam::Tractorbeam; use crate::modules::types::ModuleType; +use crate::schema::masses as db_masses; use crate::storage::Storage; #[derive(Serialize, Deserialize, Debug, Clone)] @@ -25,6 +26,32 @@ pub struct Mass { pub effects: Effects, } +#[derive(Queryable)] +pub struct MassEntry { + pub id: i32, + pub name: String, + pub pos_x: f64, + pub pos_y: f64, + pub pos_z: f64, + pub vel_x: f64, + pub vel_y: f64, + pub vel_z: f64, + pub type_data: serde_json::Value, +} + +#[derive(Insertable)] +#[table_name = "db_masses"] +pub struct NewMassEntry { + pub name: String, + pub pos_x: f64, + pub pos_y: f64, + pub pos_z: f64, + pub vel_x: f64, + pub vel_y: f64, + pub vel_z: f64, + pub type_data: serde_json::Value, +} + #[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct Effects { acceleration: Vector, @@ -299,4 +326,18 @@ impl Mass { _ => 0, } } + + pub fn to_new_mass_entry(&self, name: String) -> NewMassEntry { + NewMassEntry { + name, + pos_x: self.position.x, + pos_y: self.position.y, + pos_z: self.position.z, + vel_x: self.velocity.x, + vel_y: self.velocity.y, + vel_z: self.velocity.z, + type_data: serde_json::from_str(&serde_json::to_string(&self.mass_type).unwrap()) + .unwrap(), + } + } } diff --git a/src/math.rs b/src/math.rs index 4f0558c..5ba7981 100644 --- a/src/math.rs +++ b/src/math.rs @@ -13,6 +13,19 @@ pub fn rand_name() -> String { .collect() } +pub fn get_db_url() -> String { + let mut db_url = String::new(); + db_url.push_str("postgres://"); + db_url.push_str(constants::POSTGRES_USERNAME); + db_url.push_str(":"); + db_url.push_str(constants::POSTGRES_PASSWORD); + db_url.push_str("@"); + db_url.push_str(constants::POSTGRES_IP); + db_url.push_str("/"); + db_url.push_str(constants::POSTGRES_DB_NAME); + db_url +} + #[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct ControlSystem { previous_error: f64, diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..2bb43ed --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,13 @@ +table! { + masses (id) { + id -> Integer, + name -> Varchar, + pos_x -> Double, + pos_y -> Double, + pos_z -> Double, + vel_x -> Double, + vel_y -> Double, + vel_z -> Double, + type_data -> Jsonb, + } +} -- cgit v1.2.3