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

feat/connect snapshot query generator #55

Merged
merged 2 commits into from
Apr 29, 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
29 changes: 15 additions & 14 deletions rust/core/src/database_duckdb.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::databases::{
base_for_seeds_create_table_specifying_text_type, DatabaseQueryGenerator, SnapshotGenerator,
Timestamp,
};
use chrono::{DateTime, Utc};
use quary_proto::snapshot::snapshot_strategy::StrategyType;
Expand All @@ -24,17 +25,6 @@ impl DatabaseQueryGeneratorDuckDB {
override_now,
}
}

fn get_now(&self) -> String {
let datetime = self
.override_now
.map(|time| -> DateTime<Utc> { time.into() })
.unwrap_or(SystemTime::now().into());
format!(
"CAST ('{}' AS TIMESTAMP WITH TIME ZONE)",
datetime.format("%Y-%m-%dT%H:%M:%SZ")
)
}
}

impl DatabaseQueryGenerator for DatabaseQueryGeneratorDuckDB {
Expand Down Expand Up @@ -90,6 +80,17 @@ impl DatabaseQueryGenerator for DatabaseQueryGeneratorDuckDB {
fn database_name_wrapper(&self, name: &str) -> String {
name.into()
}

fn get_current_timestamp(&self) -> Timestamp {
let datetime = self
.override_now
.map(|time| -> DateTime<Utc> { time.into() })
.unwrap_or(SystemTime::now().into());
format!(
"CAST ('{}' AS TIMESTAMP WITH TIME ZONE)",
datetime.format("%Y-%m-%dT%H:%M:%SZ")
)
}
}

impl SnapshotGenerator for DatabaseQueryGeneratorDuckDB {
Expand All @@ -106,7 +107,7 @@ impl SnapshotGenerator for DatabaseQueryGeneratorDuckDB {
"table_exists is not necessary for DuckDB snapshots."
);

let now = self.get_now();
let now = self.get_current_timestamp();
let snapshot_query =
self.generate_snapshot_query(templated_select, unique_key, strategy, now.as_str())?;

Expand Down Expand Up @@ -211,12 +212,12 @@ mod tests {
}

#[test]
fn test_get_now() {
fn test_get_current_timestamp() {
let override_now = SystemTime::now();
let database = DatabaseQueryGeneratorDuckDB::new(None, Some(override_now));

// TODO Improve test
let result = database.get_now();
let result = database.get_current_timestamp();
let expected_datetime: DateTime<Utc> = override_now.into();
let expected_result = format!(
"CAST ('{}' AS TIMESTAMP WITH TIME ZONE)",
Expand Down
31 changes: 17 additions & 14 deletions rust/core/src/database_postgres.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::databases::{
base_for_seeds_create_table_specifying_text_type, DatabaseQueryGenerator, SnapshotGenerator,
Timestamp,
};
use chrono::{DateTime, Utc};
use quary_proto::snapshot::snapshot_strategy::StrategyType;
Expand All @@ -21,15 +22,6 @@ impl DatabaseQueryGeneratorPostgres {
override_now,
}
}

fn get_now(&self) -> String {
let datetime = self
.override_now
.map(|time| -> DateTime<Utc> { time.into() })
.unwrap_or(SystemTime::now().into())
.format("%Y-%m-%dT%H:%M:%SZ");
format!("CAST('{}' AS TIMESTAMP WITH TIME ZONE)", datetime)
}
}

impl DatabaseQueryGenerator for DatabaseQueryGeneratorPostgres {
Expand Down Expand Up @@ -158,6 +150,15 @@ impl DatabaseQueryGenerator for DatabaseQueryGeneratorPostgres {
fn database_name_wrapper(&self, name: &str) -> String {
name.to_string()
}

fn get_current_timestamp(&self) -> Timestamp {
let datetime = self
.override_now
.map(|time| -> DateTime<Utc> { time.into() })
.unwrap_or(SystemTime::now().into())
.format("%Y-%m-%dT%H:%M:%SZ");
format!("CAST('{}' AS TIMESTAMP WITH TIME ZONE)", datetime)
}
}

impl SnapshotGenerator for DatabaseQueryGeneratorPostgres {
Expand All @@ -173,7 +174,7 @@ impl SnapshotGenerator for DatabaseQueryGeneratorPostgres {
table_exists, None,
"table_exists is not necessary for Postgres snapshots."
);
let now = self.get_now();
let now = self.get_current_timestamp();
let snapshot_query =
self.generate_snapshot_query(templated_select, unique_key, strategy, now.as_str())?;
match strategy {
Expand Down Expand Up @@ -246,21 +247,23 @@ impl SnapshotGenerator for DatabaseQueryGeneratorPostgres {

#[cfg(test)]
mod tests {
use crate::databases::DatabaseQueryGenerator;

#[test]
fn get_now() {
fn test_get_current_timestamp() {
let generator = super::DatabaseQueryGeneratorPostgres::new("schema".to_string(), None);
let now = generator.get_now();
let now = generator.get_current_timestamp();

assert!(now.starts_with("CAST('20"));
}

#[test]
fn get_now_override() {
fn get_current_timestamp_override() {
let generator = super::DatabaseQueryGeneratorPostgres::new(
"schema".to_string(),
Some(std::time::SystemTime::UNIX_EPOCH),
);
let now = generator.get_now();
let now = generator.get_current_timestamp();
assert_eq!(
now,
"CAST('1970-01-01T00:00:00Z' AS TIMESTAMP WITH TIME ZONE)".to_string()
Expand Down
31 changes: 17 additions & 14 deletions rust/core/src/database_redshift.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::databases::{
base_for_seeds_create_table_specifying_text_type, DatabaseQueryGenerator, SnapshotGenerator,
Timestamp,
};
use chrono::{DateTime, Utc};
use quary_proto::snapshot::snapshot_strategy::StrategyType;
Expand All @@ -21,15 +22,6 @@ impl DatabaseQueryGeneratorRedshift {
override_now,
}
}

fn get_now(&self) -> String {
let datetime = self
.override_now
.map(|time| -> DateTime<Utc> { time.into() })
.unwrap_or(SystemTime::now().into())
.format("%Y-%m-%dT%H:%M:%SZ");
format!("CAST('{}' AS TIMESTAMP WITH TIME ZONE)", datetime)
}
}

impl DatabaseQueryGenerator for DatabaseQueryGeneratorRedshift {
Expand Down Expand Up @@ -158,6 +150,15 @@ impl DatabaseQueryGenerator for DatabaseQueryGeneratorRedshift {
fn database_name_wrapper(&self, name: &str) -> String {
name.to_string()
}

fn get_current_timestamp(&self) -> Timestamp {
let datetime = self
.override_now
.map(|time| -> DateTime<Utc> { time.into() })
.unwrap_or(SystemTime::now().into())
.format("%Y-%m-%dT%H:%M:%SZ");
format!("CAST('{}' AS TIMESTAMP WITH TIME ZONE)", datetime)
}
}

impl SnapshotGenerator for DatabaseQueryGeneratorRedshift {
Expand All @@ -172,7 +173,7 @@ impl SnapshotGenerator for DatabaseQueryGeneratorRedshift {
match strategy {
StrategyType::Timestamp(timestamp) => {
let updated_at = &timestamp.updated_at;
let now = self.get_now();
let now = self.get_current_timestamp();

// Redshift does not support CREATE TABLE IF NOT EXISTS (w/ AS (...))
let create_table_sql = format!(
Expand Down Expand Up @@ -256,21 +257,23 @@ impl SnapshotGenerator for DatabaseQueryGeneratorRedshift {

#[cfg(test)]
mod test {
use crate::databases::DatabaseQueryGenerator;

#[test]
fn get_now() {
fn test_get_current_timestamp() {
let generator = super::DatabaseQueryGeneratorRedshift::new("schema".to_string(), None);
let now = generator.get_now();
let now = generator.get_current_timestamp();

assert!(now.starts_with("CAST('20"));
}

#[test]
fn get_now_override() {
fn get_current_timestamp_override() {
let generator = super::DatabaseQueryGeneratorRedshift::new(
"schema".to_string(),
Some(std::time::SystemTime::UNIX_EPOCH),
);
let now = generator.get_now();
let now = generator.get_current_timestamp();
assert_eq!(
now,
"CAST('1970-01-01T00:00:00Z' AS TIMESTAMP WITH TIME ZONE)".to_string()
Expand Down
8 changes: 8 additions & 0 deletions rust/core/src/databases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ pub trait DatabaseQueryGenerator: SnapshotGenerator + Debug + Sync {

/// database_name_wrapper returns a full path or name wrapped in quotes that work for the specific database
fn database_name_wrapper(&self, name: &str) -> String;

louisjoecodes marked this conversation as resolved.
Show resolved Hide resolved
/// get_current_timestamp returns the current timestamp (with TZ + hour in database format)
fn get_current_timestamp(&self) -> Timestamp {
panic!("get_current_timestamp not implemented for this database")
}
}

pub trait SnapshotGenerator {
Expand Down Expand Up @@ -302,6 +307,9 @@ impl QueryResult {
}
}

// Timestamp is a type alias for a String that represents a formatted database timestamp.
pub type Timestamp = String;

#[cfg(test)]
mod tests {
use crate::database_sqlite::DatabaseQueryGeneratorSqlite;
Expand Down
Loading
Loading