forked from tianyu-li/rscriptdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileScripter.cs
More file actions
1242 lines (1105 loc) · 36.2 KB
/
FileScripter.cs
File metadata and controls
1242 lines (1105 loc) · 36.2 KB
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2005-2016 Commerce Technologies, LLC
//
// 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.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Npgsql;
using NpgsqlTypes;
namespace Mercent.AWS.Redshift
{
public class FileScripter : IDisposable
{
static readonly HashSet<string> knownExtensions = new HashSet<string>
(
new[] { ".sql" },
StringComparer.OrdinalIgnoreCase
);
Char allEmptyDirectoriesResponseChar = '\0';
Char allExtraFilesResponseChar = '\0';
NpgsqlConnection connection;
HashSet<string> fileSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
SortedSet<string> ignoreFileSet = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
bool ignoreFileSetModified = false;
List<ScriptFile> scriptFiles = new List<ScriptFile>();
public FileScripter(string connectionString)
{
if(connectionString == null)
throw new ArgumentNullException("connectionString");
NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder(connectionString);
if(String.IsNullOrEmpty(builder.Database))
throw new ArgumentException("The database name must be included in the connection string.", "connectionString");
this.DatabaseName = builder.Database;
// Set defaults.
this.Encoding = new UTF8Encoding(false);
this.MaxRowCount = 100000;
this.OutputDirectory = String.Empty;
this.QuoteMode = QuoteMode.WhenNecessary;
this.IgnoreDataFile = false;
// Open the connection.
connection = new NpgsqlConnection(connectionString);
connection.Open();
}
public event EventHandler<MessageReceivedEventArgs> ErrorMessageReceived;
public event EventHandler<MessageReceivedEventArgs> OutputMessageReceived;
public event EventHandler<MessageReceivedEventArgs> ProgressMessageReceived;
/// <summary>
/// Gets the name of the database that is being scripted.
/// </summary>
/// <remarks>
/// This is set in the constructor based on the connection string.
/// </remarks>
public string DatabaseName { get; private set; }
public Encoding Encoding { get; set; }
/// <summary>
/// Force the scripter to continue even when errors or data loss may occur.
/// </summary>
/// <remarks>
/// Set this to <c>true</c> or <c>false</c> when running using automation tools
/// that don't have an user interaction. This avoids prompting the user.
/// This setting affects any errors where the user would normally be given the option
/// to continue or abort (a "prompted" error). It also suppresses prompting the user for what to do with
/// extra files. When set to <c>true</c> the scripter will continue on prompted
/// errors and will delete extra files. When set to <c>false</c> the scripter
/// will abort on prompted errors and keep extra files.
/// </remarks>
public bool? ForceContinue { get; set; }
/// <summary>
/// Gets or sets the maximum number of rows to export.
/// </summary>
/// <remarks>
/// This is used to prevent exporting data from a table that has a large number of rows.
/// The default value is 100,000.
/// </remarks>
public int MaxRowCount { get; set; }
public bool IgnoreDataFile { get; set; }
public string OutputDirectory { get; set; }
public QuoteMode QuoteMode { get; set; }
public void Script()
{
Database database;
using(SchemaExtractor extractor = new SchemaExtractor(this.connection.ConnectionString))
{
database = extractor.GetDatabase(this.DatabaseName);
}
Script(database);
}
public void Script(Database database)
{
if(database == null)
throw new ArgumentNullException("database");
VerifyProperties();
scriptFiles.Clear();
ignoreFileSet.Clear();
ignoreFileSetModified = false;
fileSet.Clear();
if(!ForceContinue.HasValue)
{
allEmptyDirectoriesResponseChar = '\0';
allExtraFilesResponseChar = '\0';
}
else if(ForceContinue.Value)
{
allEmptyDirectoriesResponseChar = 'd';
allExtraFilesResponseChar = 'd';
}
else
{
allEmptyDirectoriesResponseChar = 'k';
allExtraFilesResponseChar = 'k';
}
if(this.OutputDirectory.Length > 0 && !Directory.Exists(this.OutputDirectory))
Directory.CreateDirectory(this.OutputDirectory);
ScriptDatabase(database);
// We don't currently script out groups because they are shared at
// the server level and do not belong to a database.
//ScriptGroups(database);
ScriptSchemas(database);
ScriptViewHeaders(database);
ScriptTables(database);
ScriptViews(database);
using(StreamWriter writer = new StreamWriter(Path.Combine(OutputDirectory, "CreateDatabaseObjects.sql"), false, Encoding))
{
writer.WriteLine(@"\set ON_ERROR_STOP on");
foreach(ScriptFile file in this.scriptFiles.Where(f => f.Command != null))
{
writer.WriteLine();
writer.WriteLine(@"\echo '{0}'", file.FileName.Replace('\\', '/'));
writer.WriteLine(file.Command);
}
}
AddScriptFile("CreateDatabaseObjects.sql", null);
DirectoryInfo outputDirectoryInfo;
if(OutputDirectory != "")
outputDirectoryInfo = new DirectoryInfo(OutputDirectory);
else
outputDirectoryInfo = new DirectoryInfo(".");
// Prompt the user for what to do with extra files.
// When objects are deleted from the database ensure that the user
// wants to delete the corresponding files. There may also be other
// files in the directory that are not scripted files.
AddIgnoreFiles();
PromptExtraFiles(outputDirectoryInfo, "");
SaveIgnoreFiles();
}
void AddIgnoreFiles()
{
string ignoreFileName = Path.Combine(OutputDirectory, "IgnoreFiles.txt");
AddScriptFile("IgnoreFiles.txt", null);
if(File.Exists(ignoreFileName))
{
foreach(string line in File.ReadAllLines(ignoreFileName))
{
string ignoreLine = line.Trim();
ignoreFileSet.Add(ignoreLine);
if(ignoreLine.Contains("*"))
{
string directory = OutputDirectory;
string filePattern = ignoreLine;
string[] parts = ignoreLine.Split('\\', '/');
if(parts.Length > 0)
{
string[] dirs = parts.Take(parts.Length - 1).ToArray();
directory = Path.Combine(OutputDirectory, Path.Combine(dirs));
filePattern = parts.Last();
}
if(Directory.Exists(directory))
{
foreach(string fileName in Directory.EnumerateFiles(directory, filePattern))
{
// Get the path to the fileName relative to the OutputDirectory.
string relativePath = fileName.Substring(OutputDirectory.Length).TrimStart('/', '\\');
AddScriptFile(relativePath, null);
}
}
}
else
AddScriptFile(ignoreLine, null);
}
}
// Ignore the bin and obj directories of an SSDT project.
// It doesn't hurt to always ignore these, so no need
// to wrap this in a check for if(TargetDataTools)...
AddScriptFile("bin", null);
AddScriptFile("obj", null);
}
void AddScriptFile(ScriptFile scriptFile)
{
if(scriptFile == null)
throw new ArgumentNullException("scriptFile");
this.scriptFiles.Add(scriptFile);
if(scriptFile.FileName != null)
this.fileSet.Add(scriptFile.FileName);
}
void AddScriptFile(string fileName)
{
AddScriptFile(new ScriptFile(fileName));
}
void AddScriptFile(string fileName, string command)
{
AddScriptFile(new ScriptFile(fileName, command));
}
void AddScriptFileRange(IEnumerable<string> fileNames)
{
foreach(string fileName in fileNames)
AddScriptFile(fileName);
}
void AppendChecksum(StringBuilder builder, Table table)
{
// Use either primary key, unique key, distribution key or all collumns (listed in order of priority).
Constraint primaryKey = table.PrimaryKey;
if(primaryKey != null)
{
AppendChecksum(builder, primaryKey.Columns);
return;
}
Constraint uniqueKey = table.UniqueConstraints().FirstOrDefault();
if(uniqueKey != null)
{
AppendChecksum(builder, uniqueKey.Columns);
return;
}
Column distributionKey = table.DistributionKey;
if(distributionKey != null)
{
AppendChecksum(builder, new[] { distributionKey });
return;
}
AppendChecksum(builder, table.Columns);
}
void AppendChecksum(StringBuilder builder, IEnumerable<Column> columns)
{
builder.Append("CHECKSUM(('' ");
foreach(Column column in columns)
{
builder.Append(" || ");
if(column.IsNullable)
builder.AppendFormat("COALESCE({0}, '')", column.GetQuotedName(this.QuoteMode));
else
builder.Append(column.GetQuotedName(this.QuoteMode));
}
builder.Append(")::varchar(max))");
}
void AppendColumns(StringBuilder builder, IEnumerable<Column> columns, string delimiter = ", ")
{
string innerDelimiter = null;
foreach(var column in columns)
{
if(innerDelimiter != null)
builder.Append(innerDelimiter);
else
innerDelimiter = delimiter;
builder.Append(column.GetQuotedName(this.QuoteMode));
}
}
void AppendOrderBy(StringBuilder builder, Table table)
{
// Use either primary key, unique key, or all columns (listed in order of priority).
builder.Append("ORDER BY ");
Constraint primaryKey = table.PrimaryKey;
if(primaryKey != null)
{
AppendColumns(builder, primaryKey.Columns);
return;
}
Constraint uniqueKey = table.UniqueConstraints().FirstOrDefault();
if(uniqueKey != null)
{
AppendColumns(builder, uniqueKey.Columns);
return;
}
AppendColumns(builder, table.Columns);
}
NpgsqlDataReader ExecuteReader(string query, params NpgsqlParameter[] parameters)
{
return RedshiftUtility.ExecuteReader(connection, query, parameters);
}
string GetInsertClause(Table table)
{
StringBuilder builder = new StringBuilder();
builder.AppendFormat("INSERT INTO {0}\r\n(\r\n\t", table.GetQualifiedName(this.QuoteMode));
AppendColumns(builder, table.Columns, ",\r\n\t");
builder.Append("\r\n)");
return builder.ToString();
}
IEnumerable<string> GetPrivileges(string privilegeCodes)
{
foreach(char code in privilegeCodes)
{
// See #define ACL_INSERT_CHR (and other definitions) in http://doxygen.postgresql.org/acl_8h_source.html
switch(code)
{
case 'a':
yield return "INSERT";
break;
case 'r':
yield return "SELECT";
break;
case 'w':
yield return "UPDATE";
break;
case 'd':
yield return "DELETE";
break;
case 'D':
// Redshift doesn't allow explicitly granting the TRUNCATE privilege.
// yield return "TRUNCATE";
break;
case 'x':
yield return "REFERENCES";
break;
case 't':
// Redshift doesn't allow explicitly granting the TRIGGER privilege.
// yield return "TRIGGER";
break;
case 'E':
yield return "EXECUTE";
break;
case 'U':
yield return "USAGE";
break;
case 'C':
yield return "CREATE";
break;
case 'T':
yield return "CREATE TEMP";
break;
case 'c':
yield return "CONNECT";
break;
}
}
}
string GetQuotedIdentifier(string identifier)
{
return RedshiftUtility.GetQuotedIdentifier(identifier, this.QuoteMode);
}
string GetSelectCommand(Table table)
{
StringBuilder selectCommand = new StringBuilder();
selectCommand.AppendFormat("SELECT TOP {0} *,\r\n\t", this.MaxRowCount);
AppendChecksum(selectCommand, table);
selectCommand.AppendFormat("\r\nFROM {0}\r\n", table.GetQualifiedName(this.QuoteMode));
AppendOrderBy(selectCommand, table);
selectCommand.AppendLine(";");
return selectCommand.ToString();
}
void OnErrorMessageReceived(string message)
{
if(ErrorMessageReceived == null)
Console.Error.WriteLine(message);
else
ErrorMessageReceived(this, new MessageReceivedEventArgs(message));
}
void OnProgressMessageReceived(string message)
{
if(ProgressMessageReceived == null)
{
// For the console indicate progress with a period when the message is null.
if(message == null)
Console.Write('.');
else
Console.WriteLine(message);
}
else
ProgressMessageReceived(this, new MessageReceivedEventArgs(message));
}
void PromptExtraFiles(DirectoryInfo dirInfo, string relativeDir)
{
string relativeName;
foreach(FileInfo fileInfo in dirInfo.GetFiles())
{
// Skip over the file if it isn't a known extension (.sql, .dat, .udat, .fmt).
if(!knownExtensions.Contains(fileInfo.Extension))
continue;
relativeName = Path.Combine(relativeDir, fileInfo.Name);
if(!fileSet.Contains(relativeName))
{
Console.WriteLine("Extra file: {0}", relativeName);
char responseChar = this.allExtraFilesResponseChar;
if(allExtraFilesResponseChar == '\0')
{
Console.WriteLine("Keep, delete, or ignore this file? For all extra files? (press k, d, i, or a)");
ConsoleKeyInfo key = Console.ReadKey(true);
responseChar = key.KeyChar;
if(responseChar == 'a')
{
Console.WriteLine("Keep, delete, or ignore all remaining extra files? (press k, d, i)");
key = Console.ReadKey(true);
responseChar = key.KeyChar;
// Only accept the response char if it is k, d, or i.
// Other characters are ignored, which is the same as keeping this file.
if(responseChar == 'k' || responseChar == 'd' || responseChar == 'i')
allExtraFilesResponseChar = responseChar;
}
}
if(responseChar == 'd')
{
try
{
fileInfo.Delete();
Console.WriteLine("Deleted file.");
}
catch(Exception ex)
{
Console.WriteLine("Delete failed. {0}: {1}", ex.GetType().Name, ex.Message);
}
}
else if(responseChar == 'i')
{
ignoreFileSetModified = true;
ignoreFileSet.Add(relativeName);
}
}
}
foreach(DirectoryInfo subDirInfo in dirInfo.GetDirectories())
{
string relativeSubDir = Path.Combine(relativeDir, subDirInfo.Name);
// Skip the directory if it is hidden or in the file set (because it was in the ignore list).
if(subDirInfo.Attributes.HasFlag(FileAttributes.Hidden) || fileSet.Contains(relativeSubDir))
continue;
// If the directory is not empty then recursively call PromptExtraFiles...
if(subDirInfo.EnumerateFileSystemInfos().Any())
PromptExtraFiles(subDirInfo, relativeSubDir);
else
{
// If the directory is empty, prompt about deleting it.
Console.WriteLine("Empty directory: {0}", relativeSubDir);
char responseChar = this.allEmptyDirectoriesResponseChar;
if(allEmptyDirectoriesResponseChar == '\0')
{
Console.WriteLine("Keep, delete, or ignore this directory? For all empty directories? (press k, d, i, or a)");
ConsoleKeyInfo key = Console.ReadKey(true);
responseChar = key.KeyChar;
if(responseChar == 'a')
{
Console.WriteLine("Keep, delete, or ignore all remaining empty directories? (press k, d, i)");
key = Console.ReadKey(true);
responseChar = key.KeyChar;
// Only accept the response char if it is k, d, or i.
// Other characters are ignored, which is the same as keeping this directory.
if(responseChar == 'k' || responseChar == 'd' || responseChar == 'i')
allEmptyDirectoriesResponseChar = responseChar;
}
}
if(responseChar == 'd')
{
try
{
subDirInfo.Delete();
Console.WriteLine("Deleted directory.");
}
catch(Exception ex)
{
Console.WriteLine("Delete failed. {0}: {1}", ex.GetType().Name, ex.Message);
}
}
else if(responseChar == 'i')
{
ignoreFileSetModified = true;
ignoreFileSet.Add(relativeSubDir);
}
}
}
}
void SaveIgnoreFiles()
{
if(ignoreFileSetModified)
{
string ignoreFileName = Path.Combine(OutputDirectory, "IgnoreFiles.txt");
File.WriteAllLines(ignoreFileName, this.ignoreFileSet);
}
}
void ScriptAccessControlList(TextWriter writer, Database database, bool newline = false)
{
string grantedObject = String.Format("DATABASE {0}", database.GetQuotedName(this.QuoteMode));
ScriptAccessControlList(writer, grantedObject, database.AccessControlList, newline);
}
void ScriptAccessControlList(TextWriter writer, Schema schema, bool newline = false)
{
string grantedObject = String.Format("SCHEMA {0}", schema.GetQuotedName(this.QuoteMode));
ScriptAccessControlList(writer, grantedObject, schema.AccessControlList, newline);
}
void ScriptAccessControlList(TextWriter writer, Table table, bool newline = false)
{
string grantedObject = String.Format("TABLE {0}", table.GetQualifiedName(this.QuoteMode));
ScriptAccessControlList(writer, grantedObject, table.AccessControlList, newline);
}
void ScriptAccessControlList(TextWriter writer, View view, bool newline = false)
{
string grantedObject = view.GetQualifiedName(this.QuoteMode);
ScriptAccessControlList(writer, grantedObject, view.AccessControlList, newline);
}
void ScriptAccessControlList(TextWriter writer, string grantedObject, string accessControlList, bool newline = false)
{
if(accessControlList == null)
return;
var entries =
from entry in accessControlList.Split('\n')
let entryParts = entry.Split('=', '/')
let grantee = entryParts[0]
// Only include privileges granted to groups.
where grantee.StartsWith("group ", StringComparison.OrdinalIgnoreCase)
orderby grantee
select new { Grantee = entryParts[0], PrivilegeCodes = entryParts[1] };
foreach(var entry in entries)
{
if(newline)
writer.WriteLine();
else
newline = true;
writer.Write("GRANT ");
WriteRange(writer, GetPrivileges(entry.PrivilegeCodes), ", ");
writer.WriteLine(" ON {0} TO {1};", grantedObject, entry.Grantee);
}
}
void ScriptConstraint(TextWriter writer, Constraint constraint)
{
writer.WriteLine
(
"ALTER TABLE {0} ADD CONSTRAINT {1} {2};",
constraint.Parent.GetQualifiedName(this.QuoteMode),
constraint.GetQuotedName(this.QuoteMode),
constraint.Definition
);
ScriptDescription(writer, constraint, true);
}
void ScriptConstraints(TextWriter writer, IEnumerable<Constraint> constraints, bool newline = false)
{
foreach(var constraint in constraints)
{
if(newline)
writer.WriteLine();
else
newline = true;
ScriptConstraint(writer, constraint);
}
}
void ScriptDatabase(Database database)
{
string fileName = "Database.sql";
string outputFileName = Path.Combine(this.OutputDirectory, fileName);
OnProgressMessageReceived(fileName);
using(var writer = new StreamWriter(outputFileName, false, this.Encoding))
{
writer.Write("CREATE DATABASE :newdbname");
if(database.Owner != null)
writer.Write(" WITH OWNER {0}", GetQuotedIdentifier(database.Owner));
writer.WriteLine(';');
ScriptDescription(writer, database, true);
ScriptAccessControlList(writer, database, true);
writer.WriteLine();
writer.WriteLine(@"\c :newdbname");
}
AddScriptFile(fileName);
}
void ScriptDescription(TextWriter writer, Column column, bool newline = false)
{
if(column.Description != null)
{
if(newline)
writer.WriteLine();
writer.WriteLine("COMMENT ON COLUMN {0} IS", column.GetQualifiedName(this.QuoteMode));
WriteStringLiteral(writer, column.Description);
writer.WriteLine(';');
}
}
void ScriptDescription(TextWriter writer, Constraint constraint, bool newline = false)
{
if(constraint.Description != null)
{
if(newline)
writer.WriteLine();
writer.WriteLine
(
"COMMENT ON CONSTRAINT {0} ON {1} IS",
constraint.GetQuotedName(this.QuoteMode),
constraint.Parent.GetQualifiedName(this.QuoteMode)
);
WriteStringLiteral(writer, constraint.Description);
writer.WriteLine(';');
}
}
void ScriptDescription(TextWriter writer, Database database, bool newline = false)
{
if(database.Description != null)
{
if(newline)
writer.WriteLine();
writer.WriteLine("COMMENT ON DATABASE {0} IS", database.GetQuotedName(this.QuoteMode));
WriteStringLiteral(writer, database.Description);
writer.WriteLine(';');
}
}
void ScriptDescription(TextWriter writer, Schema schema, bool newline = false)
{
if(schema.Description != null)
{
if(newline)
writer.WriteLine();
writer.WriteLine("COMMENT ON SCHEMA {0} IS", schema.GetQuotedName(this.QuoteMode));
WriteStringLiteral(writer, schema.Description);
writer.WriteLine(';');
}
}
void ScriptDescription(TextWriter writer, Table table, bool newline = false)
{
if(table.Description != null)
{
if(newline)
writer.WriteLine();
writer.WriteLine("COMMENT ON TABLE {0} IS", table.GetQualifiedName(this.QuoteMode));
WriteStringLiteral(writer, table.Description);
writer.WriteLine(';');
}
}
void ScriptDescription(TextWriter writer, View view, bool newline = false)
{
if(view.Description != null)
{
if(newline)
writer.WriteLine();
writer.WriteLine("COMMENT ON VIEW {0} IS", view.GetQualifiedName(this.QuoteMode));
WriteStringLiteral(writer, view.Description);
writer.WriteLine(';');
}
}
void ScriptDescriptions(TextWriter writer, IEnumerable<Column> columns, bool newline = false)
{
foreach(var column in columns)
{
if(column.Description != null)
{
if(newline)
writer.WriteLine();
else
newline = true;
ScriptDescription(writer, column);
}
}
}
void ScriptSchema(TextWriter writer, Schema schema)
{
writer.Write("CREATE SCHEMA ");
writer.Write(schema.GetQuotedName(this.QuoteMode));
if(schema.Owner != null)
{
writer.Write(" AUTHORIZATION ");
writer.Write(GetQuotedIdentifier(schema.Owner));
}
writer.WriteLine(';');
ScriptDescription(writer, schema);
ScriptAccessControlList(writer, schema, true);
}
void ScriptSchemas(Database database)
{
Directory.CreateDirectory(Path.Combine(OutputDirectory, "Schemas"));
string fileName = @"Schemas\Schemas.sql";
string outputFileName = Path.Combine(this.OutputDirectory, fileName);
OnProgressMessageReceived(fileName);
using(var writer = new StreamWriter(outputFileName, false, this.Encoding))
{
bool hasPublicSchema = false;
bool newline = false;
foreach(var schema in database.Schemas)
{
// The public schema is automatically created when the database is created.
// It will cause an error if we try to create it.
if(String.Equals(schema.Name, "public", StringComparison.OrdinalIgnoreCase))
{
hasPublicSchema = true;
if(schema.Description != null)
{
ScriptDescription(writer, schema, newline);
newline = true;
}
}
else
{
if(newline)
writer.WriteLine();
else
newline = true;
ScriptSchema(writer, schema);
}
}
// If the database that we are scripting out does not have a public schema
// then script a DROP SCHEMA statement to drop the public schema when creating a new database.
if(!hasPublicSchema)
{
if(newline)
writer.WriteLine();
writer.WriteLine("DROP SCHEMA public;");
}
}
AddScriptFile(fileName);
}
void ScriptTable(TextWriter writer, Table table)
{
// Start of CREATE TABLE statement.
writer.WriteLine("CREATE TABLE {0}", table.GetQualifiedName(this.QuoteMode));
writer.Write("(\r\n\t");
ScriptTableColumns(writer, table);
writer.Write(')');
// Distribution Style.
// Note that we don't script DISTSTYLE EVEN because that is the default.
if(table.DistributionStyle == DistributionStyle.All)
writer.Write("\r\nDISTSTYLE ALL");
else if(table.DistributionStyle == DistributionStyle.Key)
{
writer.Write("\r\nDISTKEY(");
writer.Write(table.DistributionKey.GetQuotedName(this.QuoteMode));
writer.Write(')');
}
// Sort Key.
string sortKeyColumnNames = String.Join(", ", table.SortKeys().Select(c => c.GetQuotedName(this.QuoteMode)));
if(sortKeyColumnNames.Length > 0)
{
writer.Write("\r\nSORTKEY(");
writer.Write(sortKeyColumnNames);
writer.Write(')');
}
// End of CREATE TABLE statement.
writer.WriteLine(';');
if(table.Owner != null)
{
writer.WriteLine();
writer.WriteLine("ALTER TABLE {0} OWNER TO {1};", table.GetQualifiedName(this.QuoteMode), GetQuotedIdentifier(table.Owner));
}
// Table Description.
ScriptDescription(writer, table, true);
// Column Descriptions.
ScriptDescriptions(writer, table.Columns, true);
// Primary Key.
Constraint primaryKey = table.PrimaryKey;
if(primaryKey != null)
{
writer.WriteLine();
ScriptConstraint(writer, primaryKey);
}
// Unique Constraints.
ScriptConstraints(writer, table.UniqueConstraints(), true);
// Access Control List (Grant Privileges).
ScriptAccessControlList(writer, table, true);
}
void ScriptTableColumn(TextWriter writer, Column column)
{
// Name
string quotedName = column.GetQuotedName(this.QuoteMode);
writer.Write(quotedName);
// Pad with spaces between the name and data type to make them take up 50 characters.
// This will fit a 25 char name + one space + 24 char data type (e.g. "character varying(65535)").
int nameAndTypeLength = quotedName.Length + column.DataType.Length;
int paddingLength = 50 - nameAndTypeLength;
if(paddingLength < 1)
writer.Write(' ');
else
writer.Write(new String(' ', paddingLength));
// Data Type
writer.Write(column.DataType);
// Nullable
// Note that we right align this.
if(column.IsNullable)
writer.Write(" NULL");
else
writer.Write(" NOT NULL");
// Compression Encoding
if(column.HasCompressionEncoding)
{
writer.Write(" ENCODE ");
// Attempt to right align the encoding type by left padding it
// to a length of 9 characters ("runlength" is 9 characters).
writer.Write(column.CompressionEncoding.ToUpper().PadLeft(9));
}
// Default Value
if(column.DefaultValue != null)
{
// If the column does not have a compression encoding then add enough padding
// to make the the word "DEFAULT" align with other columns that do have an encoding.
if(column.HasCompressionEncoding)
writer.Write(' ');
else
writer.Write(new String(' ', 18));
if(!column.DefaultValue.StartsWith("IDENTITY(", StringComparison.OrdinalIgnoreCase))
writer.Write("DEFAULT ");
writer.Write(column.DefaultValue);
}
}
void ScriptTableColumns(TextWriter writer, Table table)
{
string delimiter = null;
foreach(var column in table.Columns)
{
if(delimiter == null)
delimiter = ",\r\n\t";
else
writer.Write(delimiter);
ScriptTableColumn(writer, column);
}
writer.WriteLine();
}
void ScriptTableData(Table table)
{
// If --id is specified, do not generate data.
if(IgnoreDataFile)
return;
// If the table does not have any rows then skip querying it for data.
if(table.EstimatedRowCount == 0)
return;
// If the table has over MaxRowCount rows then output a warning and skip it.
if(table.EstimatedRowCount > this.MaxRowCount)
{
string warning = String.Format
(
"Warning: Skipping data export of table {0}.{1} because the table has an estimated {2:N0} rows. Any table with more than {3:N0} rows will be skipped.",
table.Schema.Name,
table.Name,
table.EstimatedRowCount,
this.MaxRowCount
);
OnErrorMessageReceived(warning);
return;
}
string relativeDir = Path.Combine("Schemas", table.Schema.Name, "Data");
string dir = Path.Combine(OutputDirectory, relativeDir);
if(!Directory.Exists(dir))
Directory.CreateDirectory(dir);
string fileName = Path.Combine(relativeDir, table.Name + ".sql");
string outputFileName = Path.Combine(OutputDirectory, fileName);
AddScriptFile(fileName);
OnProgressMessageReceived(fileName);
string selectCommand = GetSelectCommand(table);
string insertClause = GetInsertClause(table);
using(NpgsqlDataReader reader = ExecuteReader(selectCommand))
using(var writer = new StreamWriter(outputFileName, false, this.Encoding))
{
const int maxBatchSize = 1000;
const int divisor = 511;
const int remainder = 510;
int checksumOrdinal = reader.FieldCount - 1;
object[] values = new object[reader.FieldCount];
NpgsqlDbType[] types = new NpgsqlDbType[reader.FieldCount];
for(int i = 0; i < reader.FieldCount; i++)
{
types[i] = reader.GetFieldNpgsqlDbType(i);
}
bool isFirstBatch = true;
int rowCount = 0;
IList<Column> columns = table.Columns;
// Note that we don't currently properly handle tables with an identity column.
while(reader.Read())
{
int checksum = reader.GetInt32(checksumOrdinal);
if(checksum % divisor == remainder || rowCount % maxBatchSize == 0)
{
// Reset rowCount for the start of a new batch.
rowCount = 0;
// If this isn't the first batch then we want to output ";" to separate the batches.
if(isFirstBatch)
isFirstBatch = false;
else
writer.Write(";\r\n\r\n");
writer.Write(insertClause);
writer.Write(" VALUES\r\n(\r\n\t");
}
else
writer.Write(",\r\n(\r\n\t");
reader.GetValues(values);
for(int i = 0; i < columns.Count; i++)
{
if(i > 0)
writer.Write(",\r\n\t");
string castType = rowCount == 0 ? columns[i].DataType : null;
WriteLiteral(writer, values[i], types[i]);
}
writer.Write("\r\n)");
rowCount++;
}
writer.WriteLine(';');
writer.WriteLine();
writer.WriteLine("VACUUM {0};", table.GetQualifiedName(this.QuoteMode));
writer.WriteLine();
writer.WriteLine("ANALYZE {0};", table.GetQualifiedName(this.QuoteMode));
}
}