1
- // Copyright (C) 2012 Xtensive LLC.
2
- // All rights reserved .
3
- // For conditions of distribution and use, see license .
1
+ // Copyright (C) 2012-2020 Xtensive LLC.
2
+ // This code is distributed under MIT license terms .
3
+ // See the License.txt file in the project root for more information .
4
4
// Created by: Denis Krjuchkov
5
5
// Created: 2012.03.15
6
6
@@ -25,13 +25,15 @@ private static SqlWorkerResult Run(UpgradeServiceAccessor services, SqlWorkerTas
25
25
{
26
26
var result = new SqlWorkerResult ( ) ;
27
27
var executor = new SqlExecutor ( services . StorageDriver , services . Connection ) ;
28
- if ( ( task & SqlWorkerTask . DropSchema ) > 0 )
28
+ if ( ( task & SqlWorkerTask . DropSchema ) > 0 ) {
29
29
DropSchema ( services , executor ) ;
30
- if ( ( task & SqlWorkerTask . ExtractSchema ) > 0 )
30
+ }
31
+ if ( ( task & SqlWorkerTask . ExtractSchema ) > 0 ) {
31
32
result . Schema = ExtractSchema ( services , executor ) ;
32
- if ( ( task & ( SqlWorkerTask . ExtractMetadataTypes | SqlWorkerTask . ExtractMetadataAssemblies | SqlWorkerTask . ExtractMetadataExtension ) ) > 0 )
33
+ }
34
+ if ( ( task & ( SqlWorkerTask . ExtractMetadataTypes | SqlWorkerTask . ExtractMetadataAssemblies | SqlWorkerTask . ExtractMetadataExtension ) ) > 0 ) {
33
35
ExtractMetadata ( services , executor , result , task ) ;
34
-
36
+ }
35
37
return result ;
36
38
}
37
39
@@ -40,15 +42,19 @@ private static void ExtractMetadata(UpgradeServiceAccessor services, SqlExecutor
40
42
var set = new MetadataSet ( ) ;
41
43
var mapping = new MetadataMapping ( services . StorageDriver , services . NameBuilder ) ;
42
44
var metadataExtractor = new MetadataExtractor ( mapping , executor ) ;
43
- foreach ( var metadataTask in services . MappingResolver . GetMetadataTasks ( )
44
- . Where ( metadataTask => ! ShouldSkipMetadataExtraction ( mapping , result , metadataTask ) ) ) {
45
+ var metadataTasks = services . MappingResolver . GetMetadataTasks ( )
46
+ . Where ( metadataTask => ! ShouldSkipMetadataExtraction ( mapping , result , metadataTask ) ) ;
47
+ foreach ( var metadataTask in metadataTasks ) {
45
48
try {
46
- if ( task . HasFlag ( SqlWorkerTask . ExtractMetadataAssemblies ) )
49
+ if ( task . HasFlag ( SqlWorkerTask . ExtractMetadataAssemblies ) ) {
47
50
metadataExtractor . ExtractAssemblies ( set , metadataTask ) ;
48
- if ( task . HasFlag ( SqlWorkerTask . ExtractMetadataTypes ) )
51
+ }
52
+ if ( task . HasFlag ( SqlWorkerTask . ExtractMetadataTypes ) ) {
49
53
metadataExtractor . ExtractTypes ( set , metadataTask ) ;
50
- if ( task . HasFlag ( SqlWorkerTask . ExtractMetadataExtension ) )
54
+ }
55
+ if ( task . HasFlag ( SqlWorkerTask . ExtractMetadataExtension ) ) {
51
56
metadataExtractor . ExtractExtensions ( set , metadataTask ) ;
57
+ }
52
58
}
53
59
catch ( Exception exception ) {
54
60
UpgradeLog . Warning ( Strings . LogFailedToExtractMetadataFromXYZ , metadataTask . Catalog , metadataTask . Schema , exception ) ;
@@ -59,11 +65,12 @@ private static void ExtractMetadata(UpgradeServiceAccessor services, SqlExecutor
59
65
60
66
private static bool ShouldSkipMetadataExtraction ( MetadataMapping mapping , SqlWorkerResult result , SqlExtractionTask task )
61
67
{
62
- if ( result . Schema == null )
68
+ if ( result . Schema == null ) {
63
69
return false ;
70
+ }
64
71
65
72
var tables = GetSchemaTables ( result , task ) ;
66
- return tables [ mapping . Assembly ] == null && tables [ mapping . Type ] == null && tables [ mapping . Extension ] == null ;
73
+ return tables [ mapping . Assembly ] == null && tables [ mapping . Type ] == null && tables [ mapping . Extension ] == null ;
67
74
}
68
75
69
76
private static PairedNodeCollection < Schema , Table > GetSchemaTables ( SqlWorkerResult result , SqlExtractionTask task )
@@ -82,9 +89,9 @@ private static Catalog GetCatalog(SqlWorkerResult result, string catalogName)
82
89
83
90
private static Schema GetSchema ( Catalog catalog , string schemaName )
84
91
{
85
- if ( schemaName . IsNullOrEmpty ( ) )
86
- return catalog . Schemas . Single ( s => s . Name == schemaName ) ;
87
- return catalog . Schemas [ schemaName ] ;
92
+ return schemaName . IsNullOrEmpty ( )
93
+ ? catalog . Schemas . Single ( s => s . Name == schemaName )
94
+ : catalog . Schemas [ schemaName ] ;
88
95
}
89
96
90
97
private static SchemaExtractionResult ExtractSchema ( UpgradeServiceAccessor services , ISqlExecutor executor )
@@ -97,8 +104,8 @@ private static void DropSchema(UpgradeServiceAccessor services, ISqlExecutor exe
97
104
{
98
105
var driver = services . StorageDriver ;
99
106
var extractionResult = ExtractSchema ( services , executor ) ;
100
- var schemas = extractionResult . Catalogs . SelectMany ( c => c . Schemas ) . ToList ( ) ;
101
- var tables = schemas . SelectMany ( s => s . Tables ) . ToList ( ) ;
107
+ var schemas = extractionResult . Catalogs . SelectMany ( c => c . Schemas ) . ToChainedBuffer ( ) ;
108
+ var tables = schemas . SelectMany ( s => s . Tables ) . ToChainedBuffer ( ) ;
102
109
var sequences = schemas . SelectMany ( s => s . Sequences ) ;
103
110
104
111
DropForeignKeys ( driver , tables , executor ) ;
@@ -108,27 +115,38 @@ private static void DropSchema(UpgradeServiceAccessor services, ISqlExecutor exe
108
115
109
116
private static void DropSequences ( StorageDriver driver , IEnumerable < Sequence > sequences , ISqlExecutor executor )
110
117
{
111
- var statements = sequences
112
- . Select ( s => driver . Compile ( SqlDdl . Drop ( s ) ) . GetCommandText ( ) )
113
- . ToList ( ) ;
118
+ var statements = BreakEvery ( sequences
119
+ . Select ( s => driver . Compile ( SqlDdl . Drop ( s ) ) . GetCommandText ( ) ) , 25 ) . ToChainedBuffer ( ) ;
114
120
executor . ExecuteMany ( statements ) ;
115
121
}
116
122
117
123
private static void DropTables ( StorageDriver driver , IEnumerable < Table > tables , ISqlExecutor executor )
118
124
{
119
- var statements = tables
120
- . Select ( t => driver . Compile ( SqlDdl . Drop ( t ) ) . GetCommandText ( ) )
121
- . ToList ( ) ;
125
+ var statements = BreakEvery ( tables
126
+ . Select ( t => driver . Compile ( SqlDdl . Drop ( t ) ) . GetCommandText ( ) ) , 25 ) ;
122
127
executor . ExecuteMany ( statements ) ;
123
128
}
124
129
125
130
private static void DropForeignKeys ( StorageDriver driver , IEnumerable < Table > tables , ISqlExecutor executor )
126
131
{
127
- var statements = tables
132
+ var statements = BreakEvery ( tables
128
133
. SelectMany ( t => t . TableConstraints . OfType < ForeignKey > ( ) )
129
- . Select ( fk => driver . Compile ( SqlDdl . Alter ( fk . Table , SqlDdl . DropConstraint ( fk ) ) ) . GetCommandText ( ) )
130
- . ToList ( ) ;
134
+ . Select ( fk => driver . Compile ( SqlDdl . Alter ( fk . Table , SqlDdl . DropConstraint ( fk ) ) ) . GetCommandText ( ) ) , 25 ) ;
131
135
executor . ExecuteMany ( statements ) ;
132
136
}
137
+
138
+ private static IEnumerable < TItem > BreakEvery < TItem > ( IEnumerable < TItem > source , int breakOnCount )
139
+ where TItem : class
140
+ {
141
+ var count = 0 ;
142
+ foreach ( var item in source ) {
143
+ count ++ ;
144
+ if ( count == breakOnCount ) {
145
+ yield return default ( TItem ) ;
146
+ count = 0 ;
147
+ }
148
+ yield return item ;
149
+ }
150
+ }
133
151
}
134
152
}
0 commit comments