pg_sync 0.3.0

PostgreSQL DML change management tool
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
 *                                  Apache License
 *                            Version 2.0, January 2004
 *                         http://www.apache.org/licenses/
 *
 *    TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
 *
 *    1. Definitions.
 *
 *       "License" shall mean the terms and conditions for use, reproduction,
 *       and distribution as defined by Sections 1 through 9 of this document.
 *
 *       "Licensor" shall mean the copyright owner or entity authorized by
 *       the copyright owner that is granting the License.
 *
 *       "Legal Entity" shall mean the union of the acting entity and all
 *       other entities that control, are controlled by, or are under common
 *       control with that entity. For the purposes of this definition,
 *       "control" means (i) the power, direct or indirect, to cause the
 *       direction or management of such entity, whether by contract or
 *       otherwise, or (ii) ownership of fifty percent (50%) or more of the
 *       outstanding shares, or (iii) beneficial ownership of such entity.
 *
 *       "You" (or "Your") shall mean an individual or Legal Entity
 *       exercising permissions granted by this License.
 *
 *       "Source" form shall mean the preferred form for making modifications,
 *       including but not limited to software source code, documentation
 *       source, and configuration files.
 *
 *       "Object" form shall mean any form resulting from mechanical
 *       transformation or translation of a Source form, including but
 *       not limited to compiled object code, generated documentation,
 *       and conversions to other media types.
 *
 *       "Work" shall mean the work of authorship, whether in Source or
 *       Object form, made available under the License, as indicated by a
 *       copyright notice that is included in or attached to the work
 *       (an example is provided in the Appendix below).
 *
 *       "Derivative Works" shall mean any work, whether in Source or Object
 *       form, that is based on (or derived from) the Work and for which the
 *       editorial revisions, annotations, elaborations, or other modifications
 *       represent, as a whole, an original work of authorship. For the purposes
 *       of this License, Derivative Works shall not include works that remain
 *       separable from, or merely link (or bind by name) to the interfaces of,
 *       the Work and Derivative Works thereof.
 *
 *       "Contribution" shall mean any work of authorship, including
 *       the original version of the Work and any modifications or additions
 *       to that Work or Derivative Works thereof, that is intentionally
 *       submitted to Licensor for inclusion in the Work by the copyright owner
 *       or by an individual or Legal Entity authorized to submit on behalf of
 *       the copyright owner. For the purposes of this definition, "submitted"
 *       means any form of electronic, verbal, or written communication sent
 *       to the Licensor or its representatives, including but not limited to
 *       communication on electronic mailing lists, source code control systems,
 *       and issue tracking systems that are managed by, or on behalf of, the
 *       Licensor for the purpose of discussing and improving the Work, but
 *       excluding communication that is conspicuously marked or otherwise
 *       designated in writing by the copyright owner as "Not a Contribution."
 *
 *       "Contributor" shall mean Licensor and any individual or Legal Entity
 *       on behalf of whom a Contribution has been received by Licensor and
 *       subsequently incorporated within the Work.
 *
 *    2. Grant of Copyright License. Subject to the terms and conditions of
 *       this License, each Contributor hereby grants to You a perpetual,
 *       worldwide, non-exclusive, no-charge, royalty-free, irrevocable
 *       copyright license to reproduce, prepare Derivative Works of,
 *       publicly display, publicly perform, sublicense, and distribute the
 *       Work and such Derivative Works in Source or Object form.
 *
 *    3. Grant of Patent License. Subject to the terms and conditions of
 *       this License, each Contributor hereby grants to You a perpetual,
 *       worldwide, non-exclusive, no-charge, royalty-free, irrevocable
 *       (except as stated in this section) patent license to make, have made,
 *       use, offer to sell, sell, import, and otherwise transfer the Work,
 *       where such license applies only to those patent claims licensable
 *       by such Contributor that are necessarily infringed by their
 *       Contribution(s) alone or by combination of their Contribution(s)
 *       with the Work to which such Contribution(s) was submitted. If You
 *       institute patent litigation against any entity (including a
 *       cross-claim or counterclaim in a lawsuit) alleging that the Work
 *       or a Contribution incorporated within the Work constitutes direct
 *       or contributory patent infringement, then any patent licenses
 *       granted to You under this License for that Work shall terminate
 *       as of the date such litigation is filed.
 *
 *    4. Redistribution. You may reproduce and distribute copies of the
 *       Work or Derivative Works thereof in any medium, with or without
 *       modifications, and in Source or Object form, provided that You
 *       meet the following conditions:
 *
 *       (a) You must give any other recipients of the Work or
 *           Derivative Works a copy of this License; and
 *
 *       (b) You must cause any modified files to carry prominent notices
 *           stating that You changed the files; and
 *
 *       (c) You must retain, in the Source form of any Derivative Works
 *           that You distribute, all copyright, patent, trademark, and
 *           attribution notices from the Source form of the Work,
 *           excluding those notices that do not pertain to any part of
 *           the Derivative Works; and
 *
 *       (d) If the Work includes a "NOTICE" text file as part of its
 *           distribution, then any Derivative Works that You distribute must
 *           include a readable copy of the attribution notices contained
 *           within such NOTICE file, excluding those notices that do not
 *           pertain to any part of the Derivative Works, in at least one
 *           of the following places: within a NOTICE text file distributed
 *           as part of the Derivative Works; within the Source form or
 *           documentation, if provided along with the Derivative Works; or,
 *           within a display generated by the Derivative Works, if and
 *           wherever such third-party notices normally appear. The contents
 *           of the NOTICE file are for informational purposes only and
 *           do not modify the License. You may add Your own attribution
 *           notices within Derivative Works that You distribute, alongside
 *           or as an addendum to the NOTICE text from the Work, provided
 *           that such additional attribution notices cannot be construed
 *           as modifying the License.
 *
 *       You may add Your own copyright statement to Your modifications and
 *       may provide additional or different license terms and conditions
 *       for use, reproduction, or distribution of Your modifications, or
 *       for any such Derivative Works as a whole, provided Your use,
 *       reproduction, and distribution of the Work otherwise complies with
 *       the conditions stated in this License.
 *
 *    5. Submission of Contributions. Unless You explicitly state otherwise,
 *       any Contribution intentionally submitted for inclusion in the Work
 *       by You to the Licensor shall be under the terms and conditions of
 *       this License, without any additional terms or conditions.
 *       Notwithstanding the above, nothing herein shall supersede or modify
 *       the terms of any separate license agreement you may have executed
 *       with Licensor regarding such Contributions.
 *
 *    6. Trademarks. This License does not grant permission to use the trade
 *       names, trademarks, service marks, or product names of the Licensor,
 *       except as required for reasonable and customary use in describing the
 *       origin of the Work and reproducing the content of the NOTICE file.
 *
 *    7. Disclaimer of Warranty. Unless required by applicable law or
 *       agreed to in writing, Licensor provides the Work (and each
 *       Contributor provides its Contributions) on an "AS IS" BASIS,
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 *       implied, including, without limitation, any warranties or conditions
 *       of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
 *       PARTICULAR PURPOSE. You are solely responsible for determining the
 *       appropriateness of using or redistributing the Work and assume any
 *       risks associated with Your exercise of permissions under this License.
 *
 *    8. Limitation of Liability. In no event and under no legal theory,
 *       whether in tort (including negligence), contract, or otherwise,
 *       unless required by applicable law (such as deliberate and grossly
 *       negligent acts) or agreed to in writing, shall any Contributor be
 *       liable to You for damages, including any direct, indirect, special,
 *       incidental, or consequential damages of any character arising as a
 *       result of this License or out of the use or inability to use the
 *       Work (including but not limited to damages for loss of goodwill,
 *       work stoppage, computer failure or malfunction, or any and all
 *       other commercial damages or losses), even if such Contributor
 *       has been advised of the possibility of such damages.
 *
 *    9. Accepting Warranty or Additional Liability. While redistributing
 *       the Work or Derivative Works thereof, You may choose to offer,
 *       and charge a fee for, acceptance of support, warranty, indemnity,
 *       or other liability obligations and/or rights consistent with this
 *       License. However, in accepting such obligations, You may act only
 *       on Your own behalf and on Your sole responsibility, not on behalf
 *       of any other Contributor, and only if You agree to indemnify,
 *       defend, and hold each Contributor harmless for any liability
 *       incurred by, or claims asserted against, such Contributor by reason
 *       of your accepting any such warranty or additional liability.
 *
 *    END OF TERMS AND CONDITIONS
 *
 *    APPENDIX: How to apply the Apache License to your work.
 *
 *       To apply the Apache License to your work, attach the following
 *       boilerplate notice, with the fields enclosed by brackets "[]"
 *       replaced with your own identifying information. (Don't include
 *       the brackets!)  The text should be enclosed in the appropriate
 *       comment syntax for the file format. We also recommend that a
 *       file or class name and description of purpose be included on the
 *       same "printed page" as the copyright notice for easier
 *       identification within third-party archives.
 *
 *    Copyright [2022] [pg-sync author(s)]
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
#![allow(unused_variables)]

use itertools::Itertools;
use log::{debug, info};
use postgres::Client;
use sqlparser::ast;
use sqlparser::ast::{AlterTableOperation, ColumnDef, DataType, Ident, ObjectName, ObjectType, Statement};
use sqlparser::dialect::PostgreSqlDialect;

use crate::{file_loader, SQLParser};
use crate::plan::{Error, Plan};

pub fn apply_file(files: Vec<String>, schema: &String, client: &mut Client) -> Result<Plan, Error> {
    let mut plan = Plan::new();
    let mut all_statements: Vec<ast::Statement> = vec![];

    debug!("files: {:?}",files);

    create_schema_if_not_exist_or_public(&schema, client, &mut plan);
    // client.execute("SET search_path TO myschema;", &[schema]);
    let dialect = PostgreSqlDialect {}; // or AnsiDialect, or your own dialect ...
    for file in files {
        let contents = file_loader::load(&file);
        let ast: Vec<ast::Statement> = SQLParser::parse_sql(&dialect, &contents).unwrap();
        info!("AST: {:?}", ast);
        all_statements.extend(ast);
    }


    fetch_table_names_all_from_file(&mut plan, &all_statements);

    plan.table_names_dup_from_file = plan.table_names_all_from_file.clone().into_iter().duplicates().collect::<Vec<String>>();
    if !plan.table_names_dup_from_file.is_empty() { return Err(Error::DuplicateTableName { table_names: plan.table_names_dup_from_file.get(0).unwrap().to_string() }); }
    plan.table_names_unique_from_file = plan.table_names_all_from_file.clone().into_iter().unique().collect::<Vec<String>>();

    fetch_table_names_all_from_db(&schema, client, &mut plan);
    fetch_table_names_and_table_names_existing_or_drop(&schema, client, &mut plan);
    filter_table_names_new(&mut plan);
    filter_table_names_existing_for_table_names_unchanged_or_table_statements_changes(&schema, client, &mut plan, &all_statements);
    fill_table_statements_dropped(&mut plan);
    fill_table_statements_new(&mut plan, &all_statements);
    make_sql_statements(&mut plan);
    make_reverse_plan(&mut plan, &schema, client);
    return Ok(plan);
}

fn create_schema_if_not_exist_or_public(schema: &String, client: &mut Client, plan: &mut Plan) {
    debug!("!schema.eq_ignore_ascii_case(public): {} | {}",schema,!schema.eq_ignore_ascii_case("public"));
    if !schema.eq_ignore_ascii_case("public") {
        if let is_schema_exist = client.query_one("SELECT EXISTS(SELECT 1 FROM information_schema.schemata WHERE schema_name = $1)", &[schema]).unwrap() {
            let schema_status: bool = is_schema_exist.get(0);
            plan.schema_name = schema.to_string();
            plan.schema_does_not_exist = !schema_status;
            debug!("New schema {} found: status:{}",schema,schema_status);
        }
    }
}

fn fill_table_statements_new(plan: &mut Plan, ast: &Vec<Statement>) {
    for statement in ast {
        match statement {
            Statement::CreateTable { or_replace, temporary, external, global, if_not_exists, name, columns, constraints, hive_distribution, hive_formats, table_properties, with_options, file_format, location, query, without_rowid, like, engine, default_charset, collation, on_commit } => {
                if plan.table_names_new.contains(&name.to_string()) {
                    plan.table_statements_new.push(statement.clone());
                }
            }
            _ => {}
        }
    }
}

fn fill_table_statements_dropped(plan: &mut Plan) {
    for table_name in &plan.table_names_dropped {
        plan.table_statements_dropped.push(Statement::Drop {
            object_type: ObjectType::Table,
            if_exists: false,
            names: vec![ObjectName(vec![Ident { value: table_name.to_string(), quote_style: None }])],
            cascade: false,
            purge: false,
        })
    }
}

fn filter_table_names_existing_for_table_names_unchanged_or_table_statements_changes(schema: &&String, client: &mut Client, plan: &mut Plan, ast: &Vec<Statement>) {
    for table_name in &plan.table_names_existing {
        let column_defs_from_db = make_column_def_by_table_name(&schema, client, &table_name);

        for statement in ast {
            match statement {
                Statement::CreateTable { or_replace, temporary, external, global, if_not_exists, name, columns, constraints, hive_distribution, hive_formats, table_properties, with_options, file_format, location, query, without_rowid, like, engine, default_charset, collation, on_commit } => {
                    if name.to_string() == table_name.to_string() {
                        debug!("{:?}", columns);
                        let mut table_changes = diff_from_table_statements(name, columns.to_vec(), column_defs_from_db);
                        if table_changes.len() == 0 {
                            plan.table_names_unchanged.push(name.to_string());
                        }
                        plan.table_statements_changes.append(&mut table_changes);
                        break;
                    }
                }
                _ => {}
            }
        }
    }
}

fn filter_table_names_new(plan: &mut Plan) {
    for name in &plan.table_names_unique_from_file {
        if !&plan.table_names_existing.contains(&name.to_string()) && !&plan.table_names_dropped.contains(&name.to_string()) {
            plan.table_names_new.push(name.to_string());
        }
    }
}

fn fetch_table_names_and_table_names_existing_or_drop(schema: &&String, client: &mut Client, plan: &mut Plan) {
    for row_table in client.query("Select table_name from information_schema.tables where table_schema= $1 ", &[schema]).unwrap() {
        let table_name: &str = row_table.get(0);
        for name in &plan.table_names_unique_from_file {
            if name == table_name {
                plan.table_names_existing.push(table_name.to_string());
            }
        }
        if !&plan.table_names_existing.contains(&table_name.to_string()) {
            plan.table_names_dropped.push(table_name.to_string())
        }
    }
}

fn fetch_table_names_all_from_file(plan: &mut Plan, ast: &Vec<Statement>) {
    for statement in ast {
        match statement {
            Statement::CreateTable { or_replace, temporary, external, global, if_not_exists, name, columns, constraints, hive_distribution, hive_formats, table_properties, with_options, file_format, location, query, without_rowid, like, engine, default_charset, collation, on_commit } => {
                plan.table_names_all_from_file.push(name.to_string());
            }
            _ => {}
        }
    }
}

fn fetch_table_names_all_from_db(schema: &&String, client: &mut Client, plan: &mut Plan) {
    for row_table in client.query("Select table_name from information_schema.tables where table_schema= $1 ", &[schema]).unwrap() {
        let table_name: &str = row_table.get(0);
        plan.table_names_all_from_db.push(table_name.to_string());
    }
}

fn make_column_def_by_table_name(schema: &&String, client: &mut Client, table_name: &String) -> Vec<ColumnDef> {
    let mut column_defs_from_db: Vec<ColumnDef> = vec![];
    for row_column in client.query("Select * from information_schema.columns where table_schema = $1 and table_name= $2 ", &[&schema, &table_name.to_string()]).unwrap() {
        let table_catalog: &str = row_column.get("table_catalog"); //Name of the database containing the table (always the current database)
        let table_schema: &str = row_column.get("table_schema"); //Name of the schema containing the table
        let table_name: &str = row_column.get("table_name"); //Name of the table
        let column_name: &str = row_column.get("column_name"); //Name of the column
        let ordinal_position: i32 = row_column.get("ordinal_position"); //Ordinal position of the column within the table (count starts at 1)
        let column_default: Option<&str> = row_column.get("column_default"); //Default expression of the column
        let is_nullable: bool = if row_column.get::<&str, &str>("is_nullable") == "YES" { true } else { false }; //YES if the column is possibly nullable, NO if it is known not nullable. A not-null constraint is one way a column can be known not nullable, but there can be others.
        let data_type: &str = row_column.get("data_type"); //Data type of the column, if it is a built-in type, or ARRAY if it is some array (in that case, see the view element_types), else USER-DEFINED (in that case, the type is identified in udt_name and associated columns). If the column is based on a domain, this column refers to the type underlying the domain (and the domain is identified in domain_name and associated columns).
        let character_maximum_length: Option<i32> = row_column.get("character_maximum_length"); //If data_type identifies a character or bit string type, the declared maximum length; null for all other data types or if no maximum length was declared.
        let character_octet_length: Option<i32> = row_column.get("character_octet_length"); //If data_type identifies a character type, the maximum possible length in octets (bytes) of a datum; null for all other data types. The maximum octet length depends on the declared character maximum length (see above) and the server encoding.
        let numeric_precision: Option<i32> = row_column.get("numeric_precision"); //If data_type identifies a numeric type, this column contains the (declared or implicit) precision of the type for this column. The precision indicates the number of significant digits. It can be expressed in decimal (base 10) or binary (base 2) terms, as specified in the column numeric_precision_radix. For all other data types, this column is null.
        let numeric_precision_radix: Option<i32> = row_column.get("numeric_precision_radix"); //If data_type identifies a numeric type, this column indicates in which base the values in the columns numeric_precision and numeric_scale are expressed. The value is either 2 or 10. For all other data types, this column is null.
        let numeric_scale: Option<i32> = row_column.get("numeric_scale"); //If data_type identifies an exact numeric type, this column contains the (declared or implicit) scale of the type for this column. The scale indicates the number of significant digits to the right of the decimal point. It can be expressed in decimal (base 10) or binary (base 2) terms, as specified in the column numeric_precision_radix. For all other data types, this column is null.
        let datetime_precision: Option<i32> = row_column.get("datetime_precision"); //If data_type identifies a date, time, timestamp, or interval type, this column contains the (declared or implicit) fractional seconds precision of the type for this column, that is, the number of decimal digits maintained following the decimal point in the seconds value. For all other data types, this column is null.
        let interval_type: Option<&str> = row_column.get("interval_type"); //If data_type identifies an interval type, this column contains the specification which fields the intervals include for this column, e.g., YEAR TO MONTH, DAY TO SECOND, etc. If no field restrictions were specified (that is, the interval accepts all fields), and for all other data types, this field is null.
        let interval_precision: Option<i32> = row_column.get("interval_precision"); //Applies to a feature not available in PostgreSQL (see datetime_precision for the fractional seconds precision of interval type columns)
        let character_set_catalog: Option<&str> = row_column.get("character_set_catalog"); //Applies to a feature not available in PostgreSQL
        let character_set_schema: Option<&str> = row_column.get("character_set_schema"); //Applies to a feature not available in PostgreSQL
        let character_set_name: Option<&str> = row_column.get("character_set_name"); //Applies to a feature not available in PostgreSQL
        let collation_catalog: Option<&str> = row_column.get("collation_catalog"); //Name of the database containing the collation of the column (always the current database), null if default or the data type of the column is not collatable
        let collation_schema: Option<&str> = row_column.get("collation_schema"); //Name of the schema containing the collation of the column, null if default or the data type of the column is not collatable
        let collation_name: Option<&str> = row_column.get("collation_name"); //Name of the collation of the column, null if default or the data type of the column is not collatable
        let domain_catalog: Option<&str> = row_column.get("domain_catalog"); //If the column has a domain type, the name of the database that the domain is defined in (always the current database), else null.
        let domain_schema: Option<&str> = row_column.get("domain_schema"); //If the column has a domain type, the name of the schema that the domain is defined in, else null.
        let domain_name: Option<&str> = row_column.get("domain_name"); //If the column has a domain type, the name of the domain, else null.
        let udt_catalog: Option<&str> = row_column.get("udt_catalog"); //Name of the database that the column data type (the underlying type of the domain, if applicable) is defined in (always the current database)
        let udt_schema: Option<&str> = row_column.get("udt_schema"); //Name of the schema that the column data type (the underlying type of the domain, if applicable) is defined in
        let udt_name: Option<&str> = row_column.get("udt_name"); //Name of the column data type (the underlying type of the domain, if applicable)
        let maximum_cardinality: Option<i32> = row_column.get("maximum_cardinality"); //Always null, because arrays always have unlimited maximum cardinality in PostgreSQL
        let dtd_identifier: Option<&str> = row_column.get("dtd_identifier"); //An identifier of the data type descriptor of the column, unique among the data type descriptors pertaining to the table. This is mainly useful for joining with other instances of such identifiers. (The specific format of the identifier is not defined and not guaranteed to remain the same in future versions.)
        let is_identity: Option<&str> = row_column.get("is_identity"); //If the column is an identity column, then YES, else NO.
        let identity_generation: Option<&str> = row_column.get("identity_generation"); //If the column is an identity column, then ALWAYS or BY DEFAULT, reflecting the definition of the column.
        let identity_start: Option<&str> = row_column.get("identity_start"); //If the column is an identity column, then the start value of the internal sequence, else null.
        let identity_increment: Option<&str> = row_column.get("identity_increment"); //If the column is an identity column, then the increment of the internal sequence, else null.
        let identity_maximum: Option<&str> = row_column.get("identity_maximum"); //If the column is an identity column, then the maximum value of the internal sequence, else null.
        let identity_minimum: Option<&str> = row_column.get("identity_minimum"); //If the column is an identity column, then the minimum value of the internal sequence, else null.
        let identity_cycle: Option<&str> = row_column.get("identity_cycle"); //If the column is an identity column, then YES if the internal sequence cycles or NO if it does not; otherwise null.
        let is_generated: Option<&str> = row_column.get("is_generated"); //If the column is a generated column, then ALWAYS, else NEVER.
        let generation_expression: Option<&str> = row_column.get("generation_expression"); //If the column is a generated column, then the generation expression, else null.
        let is_updatable: Option<&str> = row_column.get("is_updatable"); //YES if the column is updatable, NO if not (Columns in base tables are always updatable, columns in views not necessarily)


        let dt = match data_type {
            "integer" => { DataType::Int(None) }
            "text" => { DataType::Text }
            &_ => { DataType::Text }
        };
        debug!("{}, {}", column_name, data_type);
        let stmt_column = ColumnDef { name: Ident { value: column_name.to_string(), quote_style: None }, data_type: dt, collation: None, options: vec![] };
        column_defs_from_db.push(stmt_column);
    }
    column_defs_from_db
}

fn diff_from_table_statements(table_name: &ObjectName, from_file: Vec<ColumnDef>, from_db: Vec<ColumnDef>) -> Vec<Statement> {
    let mut table_statement_updated: Vec<Statement> = vec![];
    for column_file in &from_file {
        for column_db in &from_db {
            debug!("{},{}", column_file, column_db);
            if column_file.name.value == column_db.name.value {
                debug!("diff_from_table_statements: {},{}", column_file, column_db);
                if column_file.data_type != column_db.data_type {
                    table_statement_updated.push(
                        Statement::AlterTable {
                            name: table_name.clone(),
                            operation: AlterTableOperation::DropColumn {
                                column_name: column_file.name.clone(),
                                if_exists: false,
                                cascade: false,
                            },
                        }
                    );
                    table_statement_updated.push(
                        Statement::AlterTable {
                            name: table_name.clone(),
                            operation: AlterTableOperation::AddColumn {
                                column_def: ColumnDef {
                                    name: column_file.name.clone(),
                                    data_type: column_file.data_type.clone(),
                                    collation: None,
                                    options: vec![],
                                },
                            },
                        }
                    );
                    // With cast
                    // table_statement_updated.push(
                    //     Statement::AlterTable {
                    //         name: table_name.clone(),
                    //         operation: AlterTableOperation::AlterColumn {
                    //             column_name: column_file.name.clone(),
                    //             op: AlterColumnOperation::SetDataType { data_type: column_file.data_type.clone(), using:
                    //             // Box::<>(Identifier(Ident { value: column_file.name.value, quote_style: None })
                    //             Some(Cast { expr: Box::new(Expr::Identifier(column_file.name.clone())), data_type: column_file.data_type.clone() })
                    //             },
                    //         },
                    //     }
                    // );
                }
                break;
            }
        }
    }
    return table_statement_updated;
}


fn make_sql_statements(plan: &mut Plan) {
    for table_statement_dropped in &plan.table_statements_dropped {
        plan.sql_statements_for_step_up.push(table_statement_dropped.to_string());
    }

    for table_statements_change in &plan.table_statements_changes {
        plan.sql_statements_for_step_up.push(table_statements_change.to_string());
    }

    for table_statement_new in &plan.table_statements_new {
        plan.sql_statements_for_step_up.push(table_statement_new.to_string());
    }
}

fn make_reverse_plan(plan: &mut Plan, schema: &&String, client: &mut Client) {
    for table_statement_dropped in &plan.table_statements_dropped {
        match table_statement_dropped {
            Statement::Drop { object_type: _, if_exists: _, names, cascade: _, purge: _ } => {
                let table_name = names[0].to_string();
                debug!("{}",table_name);
                // let column_defs = make_column_def_by_table_name(schema,     &mut client, &&table_name);
                // let statement = Statement::CreateTable {
                //     or_replace: false,
                //     temporary: false,
                //     external: false,
                //     global: None,
                //     if_not_exists: false,
                //     name: ObjectName(vec![Ident { value: table_name, quote_style: None }]),
                //     columns: column_defs,
                //     constraints: vec![],
                //     hive_distribution: HiveDistributionStyle::NONE,
                //     hive_formats: None,
                //     table_properties: vec![],
                //     with_options: vec![],
                //     file_format: None,
                //     location: None,
                //     query: None,
                //     without_rowid: false,
                //     like: None,
                //     engine: None,
                //     default_charset: None,
                //     collation: None,
                //     on_commit: None,
                // };
                // plan.sql_statements_for_step_down.push(statement.to_string());
            }
            _ => {}
        }
    }

    for table_statements_change in &plan.table_statements_changes {
        match table_statements_change {
            Statement::Drop { object_type, if_exists, names, cascade, purge } => {}
            _ => {}
        }
    }

    for table_statement_new in &plan.table_statements_new {
        match table_statement_new {
            Statement::CreateTable { or_replace, temporary, external, global, if_not_exists, name, columns, constraints, hive_distribution, hive_formats, table_properties, with_options, file_format, location, query, without_rowid, like, engine, default_charset, collation, on_commit } => {
                plan.sql_statements_for_step_down.push(Statement::Drop {
                    object_type: ObjectType::Table,
                    if_exists: false,
                    names: vec![name.to_owned()],
                    cascade: false,
                    purge: false,
                }.to_string())
            }
            _ => {}
        }
    }
}