elefant-tools 0.0.2

A library for doing things like pg_dump and pg_restore, with extra features, and probably more bugs.
Documentation
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use crate::helpers::StringExt;
use crate::models::column::PostgresColumn;
use crate::models::constraint::PostgresConstraint;
use crate::models::hypertable_retention::HypertableRetention;
use crate::models::index::PostgresIndex;
use crate::models::schema::PostgresSchema;
use crate::object_id::ObjectId;
use crate::pg_interval::Interval;
use crate::postgres_client_wrapper::FromPgChar;
use crate::quoting::AttemptedKeywordUsage::ColumnName;
use crate::quoting::{
    quote_value_string, AttemptedKeywordUsage, IdentifierQuoter, Quotable, QuotableIter,
};
use crate::storage::DataFormat;
use crate::{default, ElefantToolsError, HypertableCompression, PostgresIndexType};
use itertools::Itertools;
use serde::{Deserialize, Serialize};

#[derive(Debug, Eq, PartialEq, Default, Clone, Serialize, Deserialize)]
pub struct PostgresTable {
    pub name: String,
    pub columns: Vec<PostgresColumn>,
    pub constraints: Vec<PostgresConstraint>,
    pub indices: Vec<PostgresIndex>,
    pub comment: Option<String>,
    pub storage_parameters: Vec<String>,
    pub table_type: TableTypeDetails,
    pub object_id: ObjectId,
    pub depends_on: Vec<ObjectId>,
}

impl PostgresTable {
    pub fn new(name: &str) -> Self {
        PostgresTable {
            name: name.to_string(),
            ..default()
        }
    }

    pub fn get_create_statement(
        &self,
        schema: &PostgresSchema,
        identifier_quoter: &IdentifierQuoter,
    ) -> String {
        let escaped_relation_name = format!(
            "{}.{}",
            schema.name.quote(identifier_quoter, ColumnName),
            self.name.quote(identifier_quoter, ColumnName)
        );
        let mut sql = "create table ".to_string();
        sql.push_str(&escaped_relation_name);

        if let TableTypeDetails::PartitionedChildTable {
            partition_expression,
            parent_table,
        } = &self.table_type
        {
            sql.push_str(" partition of ");
            sql.push_str(&parent_table.quote(identifier_quoter, ColumnName));
            sql.push(' ');
            sql.push_str(partition_expression);
        } else {
            sql.push_str(" (");

            let mut text_row_count = 0;

            for column in &self.columns {
                if text_row_count > 0 {
                    sql.push(',');
                }
                sql.push_str("\n    ");
                sql.push_str(&column.name.quote(identifier_quoter, ColumnName));
                sql.push(' ');
                sql.push_str(&column.data_type.quote(identifier_quoter, ColumnName));

                if let Some(length) = column.data_type_length {
                    sql.push_str(&format!("({})", length));
                }

                for _ in 0..column.array_dimensions {
                    sql.push_str("[]");
                }

                if !column.is_nullable {
                    sql.push_str(" not null");
                }

                if let Some(generated) = &column.generated {
                    sql.push_str(" generated always as (");
                    sql.push_str(generated);
                    sql.push_str(") stored");
                }

                text_row_count += 1;
            }

            for index in &self.indices {
                if index.index_constraint_type == PostgresIndexType::PrimaryKey {
                    if text_row_count > 0 {
                        sql.push(',');
                    }

                    sql.push_str("\n    constraint ");
                    sql.push_str(&index.name.quote(identifier_quoter, ColumnName));
                    sql.push_str(" primary key (");

                    // We don't need to escape the column names here as they are already escaped in the index definition.
                    sql.push_join(", ", index.key_columns.iter().map(|c| &c.name));
                    sql.push(')');
                    text_row_count += 1;
                }
            }

            for constraint in &self.constraints {
                if let PostgresConstraint::Check(check) = constraint {
                    if text_row_count > 0 {
                        sql.push(',');
                    }
                    sql.push_str("\n    constraint ");
                    sql.push_str(&check.name.quote(identifier_quoter, ColumnName));
                    sql.push_str(" check ");
                    sql.push_str(&check.check_clause);
                    text_row_count += 1;
                }
            }

            if let TableTypeDetails::PartitionedParentTable {
                partition_strategy,
                partition_columns,
                ..
            } = &self.table_type
            {
                sql.push_str("\n) partition by ");
                sql.push_str(match partition_strategy {
                    TablePartitionStrategy::Hash => "hash",
                    TablePartitionStrategy::List => "list",
                    TablePartitionStrategy::Range => "range",
                });
                sql.push_str(" (");

                match partition_columns {
                    PartitionedTableColumns::Columns(columns) => {
                        sql.push_join(
                            ", ",
                            columns
                                .iter()
                                .map(|c| c.quote(identifier_quoter, ColumnName)),
                        );
                    }
                    PartitionedTableColumns::Expression(expr) => {
                        sql.push_str(expr);
                    }
                }

                sql.push(')');
            } else if let TableTypeDetails::InheritedTable { parent_tables } = &self.table_type {
                sql.push_str("\n) inherits (");
                sql.push_join(
                    ", ",
                    parent_tables.iter().map(|c| {
                        c.quote(identifier_quoter, AttemptedKeywordUsage::TypeOrFunctionName)
                    }),
                );
                sql.push(')');
            } else {
                sql.push_str("\n)");
            }
        }

        if !self.storage_parameters.is_empty() {
            sql.push_str("\nwith (");
            sql.push_join(", ", self.storage_parameters.iter());
            sql.push(')');
        }

        sql.push(';');

        if let Some(c) = &self.comment {
            sql.push_str(&format!(
                "\ncomment on table {} is {};",
                escaped_relation_name,
                quote_value_string(c)
            ));
        }

        for col in &self.columns {
            if let Some(c) = &col.comment {
                sql.push_str(&format!(
                    "\ncomment on column {}.{} is {};",
                    escaped_relation_name,
                    col.name.quote(identifier_quoter, ColumnName),
                    quote_value_string(c)
                ));
            }
        }

        for constraint in &self.constraints {
            if let PostgresConstraint::Check(constraint) = constraint {
                if let Some(c) = &constraint.comment {
                    sql.push_str(&format!(
                        "\ncomment on constraint {} on {} is {};",
                        constraint.name.quote(identifier_quoter, ColumnName),
                        escaped_relation_name,
                        quote_value_string(c)
                    ));
                }
            }
        }

        if let TableTypeDetails::TimescaleHypertable {
            dimensions,
            compression: _,
            retention: _,
        } = &self.table_type
        {
            // We don't need timescale to create the indices as we do it later on again based on what was exported.
            for (idx, dim) in dimensions.iter().enumerate() {
                match dim {
                    HypertableDimension::Time {
                        column_name,
                        time_interval,
                    } => {
                        if idx == 0 {
                            sql.push_str(&format!("\nselect public.create_hypertable('{}', by_range('{}', INTERVAL '{}'), create_default_indexes => false);", escaped_relation_name, column_name.quote(identifier_quoter, ColumnName), time_interval.to_postgres()));
                        } else {
                            sql.push_str(&format!("\nselect public.add_dimension('{}', by_range('{}', INTERVAL '{}'));", escaped_relation_name, column_name.quote(identifier_quoter, ColumnName), time_interval.to_postgres()));
                        }
                    }
                    HypertableDimension::SpaceInterval {
                        column_name,
                        integer_interval,
                    } => {
                        if idx == 0 {
                            sql.push_str(&format!("\nselect public.create_hypertable('{}', by_range('{}', {}), create_default_indexes => false);", escaped_relation_name, column_name.quote(identifier_quoter, ColumnName), integer_interval));
                        } else {
                            sql.push_str(&format!(
                                "\nselect public.add_dimension('{}', by_range('{}', {}));",
                                escaped_relation_name,
                                column_name.quote(identifier_quoter, ColumnName),
                                integer_interval
                            ));
                        }
                    }
                    HypertableDimension::SpacePartitions {
                        column_name,
                        num_partitions,
                    } => {
                        if idx == 0 {
                            sql.push_str(&format!("\nselect public.create_hypertable('{}', by_hash('{}', {}), create_default_indexes => false);", escaped_relation_name, column_name.quote(identifier_quoter, ColumnName), num_partitions));
                        } else {
                            sql.push_str(&format!(
                                "\nselect public.add_dimension('{}', by_hash('{}', {}));",
                                escaped_relation_name,
                                column_name.quote(identifier_quoter, ColumnName),
                                num_partitions
                            ));
                        }
                    }
                }
            }
        }

        sql
    }

    pub fn get_copy_in_command(
        &self,
        schema: &PostgresSchema,
        data_format: &DataFormat,
        identifier_quoter: &IdentifierQuoter,
    ) -> String {
        let mut s = "copy ".to_string();

        s.push_str(&schema.name.quote(identifier_quoter, ColumnName));
        s.push('.');
        s.push_str(&self.name.quote(identifier_quoter, ColumnName));

        s.push_str(" (");

        let cols = self.get_copy_columns_expression(identifier_quoter);

        s.push_str(&cols);

        s.push_str(") from stdin with (format ");
        match data_format {
            DataFormat::Text => {
                s.push_str("text");
            }
            DataFormat::PostgresBinary { .. } => {
                s.push_str("binary");
            }
        }
        s.push_str(", header false);");

        s
    }

    pub fn get_copy_out_command(
        &self,
        schema: &PostgresSchema,
        data_format: &DataFormat,
        identifier_quoter: &IdentifierQuoter,
    ) -> String {
        let mut s = "copy ".to_string();

        if let TableTypeDetails::TimescaleHypertable { .. } = self.table_type {
            s.push_str("(select ");
            let cols = self.get_copy_columns_expression(identifier_quoter);

            s.push_str(&cols);
            s.push_str(" from ");

            s.push_str(&schema.name.quote(identifier_quoter, ColumnName));
            s.push('.');
            s.push_str(&self.name.quote(identifier_quoter, ColumnName));
            s.push_str(") ");
        } else {
            s.push_str(&schema.name.quote(identifier_quoter, ColumnName));
            s.push('.');
            s.push_str(&self.name.quote(identifier_quoter, ColumnName));

            s.push_str(" (");

            let cols = self.get_copy_columns_expression(identifier_quoter);

            s.push_str(&cols);
            s.push_str(") ");
        }

        s.push_str(" to stdout with (format ");
        match data_format {
            DataFormat::Text => {
                s.push_str("text");
            }
            DataFormat::PostgresBinary { .. } => {
                s.push_str("binary");
            }
        }
        s.push_str(", header false, encoding 'utf-8');");

        s
    }

    fn get_copy_columns_expression(&self, identifier_quoter: &IdentifierQuoter) -> String {
        self.get_writable_columns()
            .map(|c| c.name.as_str())
            .quote(identifier_quoter, ColumnName)
            .join(", ")
    }

    pub fn get_writable_columns(&self) -> impl Iterator<Item = &PostgresColumn> {
        self.columns
            .iter()
            .filter(|c| c.generated.is_none())
            .sorted_by_key(|c| c.ordinal_position)
    }

    pub fn get_timescale_post_settings(
        &self,
        schema: &PostgresSchema,
        identifier_quoter: &IdentifierQuoter,
    ) -> Option<String> {
        if let TableTypeDetails::TimescaleHypertable {
            compression,
            retention,
            ..
        } = &self.table_type
        {
            let escaped_relation_name = format!(
                "{}.{}",
                schema.name.quote(identifier_quoter, ColumnName),
                self.name.quote(identifier_quoter, ColumnName)
            );
            let mut sql = String::new();
            if let Some(compression) = compression {
                sql.push_str("alter table ");
                compression.add_compression_settings(
                    &mut sql,
                    &escaped_relation_name,
                    identifier_quoter,
                );
            }

            if let Some(retention) = retention {
                if !sql.is_empty() {
                    sql.push('\n');
                }

                retention.add_retention(&mut sql, &escaped_relation_name);
            }

            if !sql.is_empty() {
                return Some(sql);
            }
        }

        None
    }

    pub fn is_timescale_table(&self) -> bool {
        matches!(
            self.table_type,
            TableTypeDetails::TimescaleHypertable { .. }
        )
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Default, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum TableTypeDetails {
    #[default]
    Table,
    PartitionedParentTable {
        partition_strategy: TablePartitionStrategy,
        default_partition_name: Option<String>,
        partition_columns: PartitionedTableColumns,
    },
    PartitionedChildTable {
        parent_table: String,
        partition_expression: String,
    },
    InheritedTable {
        parent_tables: Vec<String>,
    },
    TimescaleHypertable {
        dimensions: Vec<HypertableDimension>,
        compression: Option<HypertableCompression>,
        retention: Option<HypertableRetention>,
    },
}

#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum PartitionedTableColumns {
    Columns(Vec<String>),
    Expression(String),
}

#[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)]
pub enum TablePartitionStrategy {
    Hash,
    List,
    Range,
}

impl FromPgChar for TablePartitionStrategy {
    fn from_pg_char(c: char) -> Result<Self, ElefantToolsError> {
        match c {
            'h' => Ok(TablePartitionStrategy::Hash),
            'l' => Ok(TablePartitionStrategy::List),
            'r' => Ok(TablePartitionStrategy::Range),
            _ => Err(ElefantToolsError::InvalidTablePartitioningStrategy(
                c.to_string(),
            )),
        }
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum HypertableDimension {
    Time {
        column_name: String,
        time_interval: Interval,
    },
    SpaceInterval {
        column_name: String,
        integer_interval: i64,
    },
    SpacePartitions {
        column_name: String,
        num_partitions: i16,
    },
}