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
//! PostgreSQL schemas.
use std::{fmt, sync::Arc};
use super::{catalog, CheckCatalog, PgCreateTable, PgCreateType, PgName};
use crate::common::*;
use crate::parse_error::{Annotation, FileInfo, ParseError};
mod schema_sql;
/// A collection of PostgreSQL `CREATE TABLE` and `CREATE TYPE` defintions.
///
/// **This does _not_ correspond to what PostgreSQL calls a "schema"!** A
/// PostgreSQL "schema" is a namespace. This is what `dbcrossbar` calls a
/// schema, which is a collection of named tables and types.
#[derive(Clone, Debug)]
pub(crate) struct PgSchema {
pub(crate) types: Vec<PgCreateType>,
pub(crate) tables: Vec<PgCreateTable>,
}
impl PgSchema {
/// Parse a source file containing PostgreSQL `CREATE TABLE` and `CREATE
/// TYPE` statements.
pub(crate) fn parse(
file_name: String,
file_contents: String,
) -> Result<Self, ParseError> {
let file_info = Arc::new(FileInfo::new(file_name, file_contents));
schema_sql::parse(&file_info.contents).map_err(|err| {
ParseError::new(
file_info,
vec![Annotation::primary(
err.location.offset,
format!("expected {}", err.expected),
)],
"error parsing Postgres CREATE TABLE",
)
})
}
/// Look up `full_table_name` in the database, and return a new
/// `PgCreateTable` based on what we find in `pg_catalog`.
///
/// Returns `None` if no matching table exists.
#[instrument(level = "trace", skip(ctx))]
pub(crate) async fn from_pg_catalog(
ctx: &Context,
database_url: &UrlWithHiddenPassword,
table_name: &PgName,
) -> Result<Option<Self>> {
catalog::fetch_from_url(ctx, database_url, table_name).await
}
/// Look up `full_table_name` in the database, and return a new
/// `PgCreateTable` based on what we find in `pg_catalog`.
///
/// If this fails, use `full_table_name` and `default` to construct a new
/// table.
#[instrument(level = "trace", skip(ctx))]
pub(crate) async fn from_pg_catalog_or_default(
ctx: &Context,
check_catalog: CheckCatalog,
database_url: &UrlWithHiddenPassword,
table_name: &PgName,
default: &Schema,
) -> Result<Self> {
// If we can't find a catalog in the database, use this one.
let default_dest_schema = Self::from_schema_and_name(default, table_name)?;
// Should we check the catalog to see if the table schema exists?
match check_catalog {
// Nope, we just want to use the default.
CheckCatalog::No => Ok(default_dest_schema),
// See if the table is listed in the catalog.
CheckCatalog::Yes => {
let opt_dest_schema =
Self::from_pg_catalog(ctx, database_url, table_name).await?;
Ok(match opt_dest_schema {
Some(dest_schema) => {
dest_schema.aligned_with(&default_dest_schema)?
}
None => default_dest_schema,
})
}
}
}
/// Construct a PostgreSQL schema from a portable schema and a table name
/// (which will be used instead of the name in the schema).
#[instrument(level = "trace")]
pub(crate) fn from_schema_and_name(
schema: &Schema,
name: &PgName,
) -> Result<Self> {
let types = schema
.named_data_types
.values()
.map(PgCreateType::from_named_data_type)
.collect::<Result<Vec<_>>>()?;
let tables = vec![PgCreateTable::from_name_and_columns(
schema,
name.to_owned(),
&schema.table.columns,
)?];
Ok(PgSchema { types, tables })
}
/// Convert to a portable schema.
pub(crate) fn to_schema(&self) -> Result<Schema> {
if self.tables.len() != 1 {
Err(format_err!("Postgres schema must contain exactly 1 table"))
} else {
let types = self
.types
.iter()
.map(|ty| ty.to_named_data_type())
.collect::<Result<Vec<_>>>()?;
let table = self.tables[0].to_table()?;
Schema::from_types_and_table(types, table)
}
}
/// Return either the sole table associated with this schema, or an error.
pub(crate) fn table(&self) -> Result<&PgCreateTable> {
if self.tables.len() != 1 {
Err(format_err!(
"expected PostgreSQL schema to contain only one table"
))
} else {
Ok(&self.tables[0])
}
}
/// Return either the sole table associated with this schema, or an error.
pub(crate) fn table_mut(&mut self) -> Result<&mut PgCreateTable> {
if self.tables.len() != 1 {
Err(format_err!(
"expected PostgreSQL schema to contain only one table"
))
} else {
Ok(&mut self.tables[0])
}
}
/// Create a new scehma based on this schema, but with columns matching the
/// the names and order of the columns in `other_table`. This is useful if
/// we want to insert from `other_schema`'s table into `self`'s table.
pub(crate) fn aligned_with(&self, other_schema: &PgSchema) -> Result<PgSchema> {
// Get the table from each schema, erroring out if we have more than one
// at this point, and align them.
let self_table = self.table()?;
let other_table = other_schema.table()?;
let aligned_table = self_table.aligned_with(other_table)?;
Ok(PgSchema {
tables: vec![aligned_table],
types: self.types.clone(),
})
}
/// Write a `COPY (SELECT ...) TO STDOUT ...` statement for this schema's
/// table.
pub(crate) fn write_export_sql(
&self,
f: &mut dyn Write,
source_args: &SourceArguments<Verified>,
) -> Result<()> {
self.table()?.write_export_sql(f, source_args)
}
/// Write a `SELECT ...` statement for this schema's table.
pub(crate) fn write_export_select_sql(
&self,
f: &mut dyn Write,
source_args: &SourceArguments<Verified>,
) -> Result<()> {
self.table()?.write_export_select_sql(f, source_args)
}
/// Write a `SELECT COUNT(*) ...` statement for this schema's table.
pub(crate) fn write_count_sql(
&self,
f: &mut dyn Write,
source_args: &SourceArguments<Verified>,
) -> Result<()> {
self.table()?.write_count_sql(f, source_args)
}
}
impl fmt::Display for PgSchema {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for ty in &self.types {
writeln!(f, "{}", ty)?;
}
for tb in &self.tables {
write!(f, "{}", tb)?;
}
Ok(())
}
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
use std::collections::HashMap;
use super::*;
use crate::schema::{Column, DataType, NamedDataType, Srid};
#[test]
fn simple_table() {
let input = include_str!("schema_sql_example.sql");
let pg_schema =
PgSchema::parse("test.sql".to_owned(), input.to_owned()).unwrap();
let table = pg_schema.to_schema().unwrap();
let mut expected_named_data_types = HashMap::new();
expected_named_data_types.insert(
"color".to_owned(),
NamedDataType {
name: "color".to_owned(),
data_type: DataType::OneOf(vec![
"red".to_owned(),
"green".to_owned(),
"blue".to_owned(),
]),
},
);
// mood
expected_named_data_types.insert(
"mood".to_owned(),
NamedDataType {
name: "mood".to_owned(),
data_type: DataType::OneOf(vec![
"happy".to_owned(),
"sad".to_owned(),
"amused".to_owned(),
]),
},
);
let expected = Schema {
named_data_types: expected_named_data_types,
table: Table {
name: "example".to_string(),
columns: vec![
Column {
name: "a".to_string(),
is_nullable: true,
data_type: DataType::Text,
comment: None,
},
Column {
name: "b".to_string(),
is_nullable: true,
data_type: DataType::Int32,
comment: None,
},
Column {
name: "c".to_string(),
is_nullable: false,
data_type: DataType::Uuid,
comment: None,
},
Column {
name: "d".to_string(),
is_nullable: true,
data_type: DataType::Date,
comment: None,
},
Column {
name: "e".to_string(),
is_nullable: true,
data_type: DataType::Float64,
comment: None,
},
Column {
name: "f".to_string(),
is_nullable: true,
data_type: DataType::Array(Box::new(DataType::Text)),
comment: None,
},
Column {
name: "g".to_string(),
is_nullable: true,
data_type: DataType::Array(Box::new(DataType::Int32)),
comment: None,
},
Column {
name: "h".to_string(),
is_nullable: true,
data_type: DataType::GeoJson(Srid::wgs84()),
comment: None,
},
Column {
name: "i".to_string(),
is_nullable: true,
data_type: DataType::GeoJson(Srid::new(3857)),
comment: None,
},
Column {
name: "j".to_string(),
is_nullable: true,
data_type: DataType::Int16,
comment: None,
},
Column {
name: "k".to_string(),
is_nullable: true,
data_type: DataType::TimestampWithoutTimeZone,
comment: None,
},
Column {
name: "l".to_string(),
is_nullable: true,
data_type: DataType::Named("color".to_owned()),
comment: None,
},
Column {
name: "m".to_string(),
is_nullable: true,
data_type: DataType::Named("mood".to_owned()),
comment: None,
},
],
},
};
assert_eq!(table, expected);
// Now try writing and re-reading.
let mut out = vec![];
write!(&mut out, "{}", &pg_schema).expect("error writing table");
let pg_parsed_again =
PgSchema::parse("test.sql".to_owned(), String::from_utf8(out).unwrap())
.expect("error re-parsing table");
let parsed_again = pg_parsed_again.to_schema().unwrap();
assert_eq!(parsed_again, expected);
}
}