Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wasm working with timestamps #67

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions rust/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ sha2 = { default-features = false, version = "0.10.8" }
data-encoding = "2.5.0"
tokio-util = "0.7.10"

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.69"

[dev-dependencies]
tokio = { version = "1.37.0", features = ["rt", "macros"], default-features = false }
13 changes: 13 additions & 0 deletions rust/core/src/database_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::databases::{
Timestamp,
};
use chrono::{DateTime, Utc};
#[cfg(target_arch = "wasm32")]
use js_sys::Date;
use quary_proto::snapshot::snapshot_strategy::StrategyType;
use sqlinference::dialect::Dialect;
use std::time::SystemTime;
Expand Down Expand Up @@ -81,6 +83,7 @@ impl DatabaseQueryGenerator for DatabaseQueryGeneratorDuckDB {
name.into()
}

#[cfg(not(target_arch = "wasm32"))]
fn get_current_timestamp(&self) -> Timestamp {
let datetime = self
.override_now
Expand All @@ -91,6 +94,16 @@ impl DatabaseQueryGenerator for DatabaseQueryGeneratorDuckDB {
datetime.format("%Y-%m-%dT%H:%M:%SZ")
)
}

#[cfg(target_arch = "wasm32")]
fn get_current_timestamp(&self) -> Timestamp {
let js_date = Date::new_0();
let datetime: DateTime<Utc> = js_date.into();
format!(
"CAST ('{}' AS TIMESTAMP WITH TIME ZONE)",
datetime.format("%Y-%m-%dT%H:%M:%SZ")
)
}
}

impl SnapshotGenerator for DatabaseQueryGeneratorDuckDB {
Expand Down
13 changes: 13 additions & 0 deletions rust/core/src/database_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use chrono::{DateTime, Utc};
use quary_proto::snapshot::snapshot_strategy::StrategyType;
use sqlinference::dialect::Dialect;
use std::time::SystemTime;
#[cfg(target_arch = "wasm32")]
use js_sys::Date;

#[derive(Debug, Clone)]
pub struct DatabaseQueryGeneratorPostgres {
Expand Down Expand Up @@ -151,6 +153,7 @@ impl DatabaseQueryGenerator for DatabaseQueryGeneratorPostgres {
name.to_string()
}

#[cfg(not(target_arch = "wasm32"))]
fn get_current_timestamp(&self) -> Timestamp {
let datetime = self
.override_now
Expand All @@ -159,6 +162,16 @@ impl DatabaseQueryGenerator for DatabaseQueryGeneratorPostgres {
.format("%Y-%m-%dT%H:%M:%SZ");
format!("CAST('{}' AS TIMESTAMP WITH TIME ZONE)", datetime)
}

#[cfg(target_arch = "wasm32")]
fn get_current_timestamp(&self) -> Timestamp {
let js_date = Date::new_0();
let datetime: DateTime<Utc> = js_date.into();
format!(
"CAST ('{}' AS TIMESTAMP WITH TIME ZONE)",
datetime.format("%Y-%m-%dT%H:%M:%SZ")
)
}
}

impl SnapshotGenerator for DatabaseQueryGeneratorPostgres {
Expand Down
13 changes: 13 additions & 0 deletions rust/core/src/database_redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use chrono::{DateTime, Utc};
use quary_proto::snapshot::snapshot_strategy::StrategyType;
use sqlinference::dialect::Dialect;
use std::time::SystemTime;
#[cfg(target_arch = "wasm32")]
use js_sys::Date;

#[derive(Debug, Clone)]
pub struct DatabaseQueryGeneratorRedshift {
Expand Down Expand Up @@ -151,6 +153,7 @@ impl DatabaseQueryGenerator for DatabaseQueryGeneratorRedshift {
name.to_string()
}

#[cfg(not(target_arch = "wasm32"))]
fn get_current_timestamp(&self) -> Timestamp {
let datetime = self
.override_now
Expand All @@ -159,6 +162,16 @@ impl DatabaseQueryGenerator for DatabaseQueryGeneratorRedshift {
.format("%Y-%m-%dT%H:%M:%SZ");
format!("CAST('{}' AS TIMESTAMP WITH TIME ZONE)", datetime)
}

#[cfg(target_arch = "wasm32")]
fn get_current_timestamp(&self) -> Timestamp {
let js_date = Date::new_0();
let datetime: DateTime<Utc> = js_date.into();
format!(
"CAST ('{}' AS TIMESTAMP WITH TIME ZONE)",
datetime.format("%Y-%m-%dT%H:%M:%SZ")
)
}
}

impl SnapshotGenerator for DatabaseQueryGeneratorRedshift {
Expand Down
4 changes: 4 additions & 0 deletions rust/core/src/databases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ impl DatabaseQueryGenerator for Box<dyn DatabaseQueryGenerator> {
fn database_name_wrapper(&self, name: &str) -> String {
self.as_ref().database_name_wrapper(name)
}

fn get_current_timestamp(&self) -> String {
self.as_ref().get_current_timestamp()
}
}

pub trait SnapshotGenerator {
Expand Down
Loading