-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathpostgres.rs
187 lines (165 loc) · 5.36 KB
/
postgres.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use dozer_ingestion_connector::{async_trait, dozer_types::types::Field};
use dozer_ingestion_postgres::{
connection::{client::Client, helper::connect},
connector::{PostgresConfig, PostgresConnector},
tokio_postgres,
};
use dozer_utils::{process::run_docker_compose, Cleanup};
use tempfile::TempDir;
use crate::test_suite::{
records::Operation, CudConnectorTest, DataReadyConnectorTest, FieldsAndPk,
InsertOnlyConnectorTest,
};
use super::sql::{
create_schema, create_table, create_table_with_all_supported_data_types, insert_record,
operation_to_sql, schema_to_sql,
};
pub struct PostgresConnectorTest {
config: tokio_postgres::Config,
schema_name: Option<String>,
table_name: String,
schema: FieldsAndPk,
_cleanup: Cleanup,
_temp_dir: TempDir,
}
#[async_trait]
impl DataReadyConnectorTest for PostgresConnectorTest {
type Connector = PostgresConnector;
async fn new() -> (Self, Self::Connector) {
let (mut client, connector_test, connector) = create_postgres_server().await;
client
.batch_execute(&create_table_with_all_supported_data_types("test_table"))
.await
.unwrap();
(connector_test, connector)
}
}
#[async_trait]
impl InsertOnlyConnectorTest for PostgresConnectorTest {
type Connector = PostgresConnector;
async fn new(
schema_name: Option<String>,
table_name: String,
schema: FieldsAndPk,
records: Vec<Vec<Field>>,
) -> Option<(Self, Self::Connector, FieldsAndPk)> {
let (mut client, mut connector_test, connector) = create_postgres_server().await;
let (actual_schema, _) = schema_to_sql(schema.clone());
if let Some(schema_name) = &schema_name {
client
.batch_execute(&create_schema(schema_name))
.await
.expect("Failed to create schema");
}
let query = create_table(schema_name.as_deref(), &table_name, &actual_schema);
client
.batch_execute(&query)
.await
.expect("Failed to create table");
for record in records {
let query = insert_record(schema_name.as_deref(), &table_name, &record, &schema.0);
client
.batch_execute(&query)
.await
.expect("Failed to insert record");
}
connector_test.schema_name = schema_name;
connector_test.table_name = table_name;
connector_test.schema = schema;
Some((connector_test, connector, actual_schema))
}
}
#[async_trait]
impl CudConnectorTest for PostgresConnectorTest {
async fn start_cud(&self, operations: Vec<Operation>) {
let mut client = connect(self.config.clone()).await.unwrap();
let schema_name = self.schema_name.clone();
let table_name = self.table_name.clone();
let schema = self.schema.clone();
tokio::spawn(async move {
for operation in operations {
client
.batch_execute(&operation_to_sql(
schema_name.as_deref(),
&table_name,
&operation,
&schema,
))
.await
.unwrap();
}
});
}
}
async fn create_postgres_server() -> (Client, PostgresConnectorTest, PostgresConnector) {
let host = "localhost";
let port = 5432;
let user = "postgres";
let password = "postgres";
let dbname = "dozer-test";
let temp_dir = tempfile::Builder::new()
.prefix("postgres")
.tempdir()
.expect("Failed to create temp dir");
let docker_compose_path = temp_dir.path().join("docker-compose.yaml");
std::fs::write(&docker_compose_path, DOCKER_COMPOSE_YAML)
.expect("Failed to write docker compose file");
let cleanup = run_docker_compose(&docker_compose_path, "dozer-wait-for-connections-healthy");
let mut config = tokio_postgres::Config::default();
config
.host(host)
.port(port)
.user(user)
.password(password)
.dbname(dbname);
let connector = PostgresConnector::new(
PostgresConfig {
name: "postgres_connector_test".to_string(),
config: config.clone(),
schema: None,
batch_size: 1000,
},
None,
)
.unwrap();
let client = connect(config.clone()).await.unwrap();
(
client,
PostgresConnectorTest {
config,
schema_name: Default::default(),
table_name: Default::default(),
schema: Default::default(),
_cleanup: cleanup,
_temp_dir: temp_dir,
},
connector,
)
}
const DOCKER_COMPOSE_YAML: &str = r#"version: '2.4'
services:
postgres:
container_name: postgres
image: debezium/postgres:13
ports:
- target: 5432
published: 5432
environment:
- POSTGRES_DB=dozer-test
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- ALLOW_IP_RANGE=0.0.0.0/0
healthcheck:
test:
- CMD-SHELL
- pg_isready -U postgres -h 0.0.0.0 -d dozer-test
interval: 5s
timeout: 5s
retries: 5
dozer-wait-for-connections-healthy:
image: alpine
command: echo 'All connections are healthy'
depends_on:
postgres:
condition: service_healthy
"#;