fix: postgres issues

ids are i64, converted to bigints. color should be stored as signed.
This makes migrating from SQLite to postgres kind of painful, but
doable.
This commit is contained in:
əlemi 2022-11-01 18:12:32 +01:00
parent 4466b792fd
commit adf2812dfc
Signed by: alemi
GPG key ID: A4895B84D311642C
11 changed files with 38 additions and 33 deletions

View file

@ -25,7 +25,7 @@ eframe = { version = "0.19", features = ["persistence"] }
tokio = { version = "1", features = ["full"] }
clap = { version = "4", features = ["derive"] }
futures = "0.3"
sea-orm = { version = "0.10", features = [ "runtime-tokio-rustls", "sqlx-sqlite", "macros" ] }
sea-orm = { version = "0.10", features = [ "runtime-tokio-rustls", "sqlx-sqlite", "sqlx-postgres", "macros" ] }
reqwest = { version = "0.11", features = ["json"] }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]

View file

@ -20,4 +20,5 @@ features = [
# e.g.
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
"sqlx-sqlite", # `DATABASE_DRIVER` feature
"sqlx-postgres",
]

View file

@ -17,7 +17,7 @@ impl MigrationTrait for Migration {
.if_not_exists()
.col(
ColumnDef::new(Panels::Id)
.integer()
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
@ -26,6 +26,7 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(Panels::Position).integer().not_null())
.col(ColumnDef::new(Panels::Timeserie).boolean().not_null())
.col(ColumnDef::new(Panels::Height).integer().not_null())
.col(ColumnDef::new(Panels::Width).integer().not_null())
.col(ColumnDef::new(Panels::ViewScroll).boolean().not_null())
.col(ColumnDef::new(Panels::LimitView).boolean().not_null())
.col(ColumnDef::new(Panels::ViewSize).integer().not_null())
@ -43,7 +44,7 @@ impl MigrationTrait for Migration {
.if_not_exists()
.col(
ColumnDef::new(Sources::Id)
.integer()
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
@ -62,15 +63,15 @@ impl MigrationTrait for Migration {
.if_not_exists()
.col(
ColumnDef::new(Metrics::Id)
.integer()
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Metrics::Name).string().not_null())
.col(ColumnDef::new(Metrics::Position).integer().not_null())
.col(ColumnDef::new(Metrics::PanelId).integer().not_null())
.col(ColumnDef::new(Metrics::SourceId).integer().not_null())
.col(ColumnDef::new(Metrics::PanelId).big_integer().not_null())
.col(ColumnDef::new(Metrics::SourceId).big_integer().not_null())
.col(ColumnDef::new(Metrics::QueryX).string().not_null())
.col(ColumnDef::new(Metrics::QueryY).string().not_null())
.col(ColumnDef::new(Metrics::Color).integer().not_null())
@ -83,14 +84,14 @@ impl MigrationTrait for Migration {
.if_not_exists()
.col(
ColumnDef::new(Points::Id)
.integer()
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Points::MetricId).integer().not_null())
.col(ColumnDef::new(Points::X).float().not_null())
.col(ColumnDef::new(Points::Y).float().not_null())
.col(ColumnDef::new(Points::MetricId).big_integer().not_null())
.col(ColumnDef::new(Points::X).double().not_null())
.col(ColumnDef::new(Points::Y).double().not_null())
.to_owned(),
).await?;
Ok(())
@ -121,6 +122,7 @@ enum Panels {
Position,
Timeserie,
Height,
Width,
ViewScroll,
LimitView,
ViewSize,

View file

@ -10,13 +10,13 @@ use crate::data::FetchError;
#[sea_orm(table_name = "metrics")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: i32,
pub id: i64,
pub name: String,
pub source_id: i32,
pub source_id: i64,
pub query_x: Option<String>,
pub query_y: String,
pub panel_id: i32,
pub color: u32,
pub panel_id: i64,
pub color: i32,
pub position: i32,
}

View file

@ -6,7 +6,7 @@ use sea_orm::entity::prelude::*;
#[sea_orm(table_name = "panels")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = true)]
pub id: i32,
pub id: i64,
pub name: String,
pub view_scroll: bool,
pub view_size: i32,

View file

@ -6,8 +6,8 @@ use eframe::egui::plot::PlotPoint;
#[sea_orm(table_name = "points")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: i32,
pub metric_id: i32,
pub id: i64,
pub metric_id: i64,
pub x: f64,
pub y: f64,
}

View file

@ -5,7 +5,7 @@ use chrono::Utc;
#[sea_orm(table_name = "sources")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: i32,
pub id: i64,
pub name: String,
pub enabled: bool,
pub url: String,

View file

@ -12,7 +12,7 @@ use crate::data::entities;
use super::metric::metric_edit_ui;
pub fn source_panel(app: &mut App, ui: &mut Ui) {
let mut source_to_put_metric_on : Option<i32> = None;
let mut source_to_put_metric_on : Option<i64> = None;
let mut to_swap: Option<usize> = None;
let _to_insert: Vec<entities::metrics::Model> = Vec::new();
// let mut to_delete: Option<usize> = None;

View file

@ -49,6 +49,14 @@ struct CliArgs {
log_size: u64,
}
// When compiling for web:
#[cfg(target_arch = "wasm32")]
fn setup_tracing(_layer: InternalLoggerLayer) {
// Make sure panics are logged using `console.error`.
console_error_panic_hook::set_once();
// Redirect tracing to console.log and friends:
tracing_wasm::set_as_global_default();
}
// When compiling natively:
#[cfg(not(target_arch = "wasm32"))]
@ -61,15 +69,6 @@ fn setup_tracing(layer: InternalLoggerLayer) {
.init();
}
// When compiling for web:
#[cfg(target_arch = "wasm32")]
fn setup_tracing(_layer: InternalLoggerLayer) {
// Make sure panics are logged using `console.error`.
console_error_panic_hook::set_once();
// Redirect tracing to console.log and friends:
tracing_wasm::set_as_global_default();
}
fn main() {
let args = CliArgs::parse();

View file

@ -83,7 +83,7 @@ pub fn timestamp_to_str(t: i64, date: bool, time: bool) -> String {
)
}
pub fn unpack_color(c: u32) -> Color32 {
pub fn unpack_color(c: i32) -> Color32 {
let r: u8 = (c >> 0) as u8;
let g: u8 = (c >> 8) as u8;
let b: u8 = (c >> 16) as u8;
@ -92,11 +92,11 @@ pub fn unpack_color(c: u32) -> Color32 {
}
#[allow(dead_code)]
pub fn repack_color(c: Color32) -> u32 {
let mut out: u32 = 0;
pub fn repack_color(c: Color32) -> i32 {
let mut out: i32 = 0;
let mut offset = 0;
for el in c.to_array() {
out |= ((el & 0xFF) as u32) << offset;
out |= ((el & 0xFF) as i32) << offset;
offset += 8;
}
return out;

View file

@ -1,5 +1,5 @@
use chrono::Utc;
use sea_orm::{DatabaseConnection, EntityTrait, Condition, ColumnTrait, QueryFilter, Set};
use sea_orm::{DatabaseConnection, EntityTrait, Condition, ColumnTrait, QueryFilter, Set, QueryOrder, Order};
use tokio::sync::{watch, mpsc};
use tracing::info;
use std::collections::VecDeque;
@ -174,6 +174,7 @@ impl AppState {
.add(entities::points::Column::X.gte((now - new_width) as f64))
.add(entities::points::Column::X.lte(now as f64))
)
.order_by(entities::points::Column::X, Order::Asc)
.all(&db)
.await.unwrap().into();
self.tx.points.send(self.points.clone().into()).unwrap();
@ -194,6 +195,7 @@ impl AppState {
.add(entities::points::Column::X.gte(now - new_width))
.add(entities::points::Column::X.lte(now - width))
)
.order_by(entities::points::Column::X, Order::Asc)
.all(&db)
.await.unwrap();
info!(target: "worker", "Fetched {} previous points", previous_points.len());
@ -211,6 +213,7 @@ impl AppState {
.add(entities::points::Column::X.gte(last as f64))
.add(entities::points::Column::X.lte(now as f64))
)
.order_by(entities::points::Column::X, Order::Asc)
.all(&db)
.await.unwrap();
info!(target: "worker", "Fetched {} new points", new_points.len());