@@ -11,14 +11,17 @@ namespace Signum.Engine;
11
11
12
12
public static class Administrator
13
13
{
14
+ public static Func < bool > ? OnTotalGeneration ;
15
+
14
16
public static void TotalGeneration ( )
15
17
{
16
- foreach ( var db in Schema . Current . DatabaseNames ( ) )
17
- {
18
- Connector . Current . CleanDatabase ( db ) ;
19
- SafeConsole . WriteColor ( ConsoleColor . DarkGray , '.' ) ;
20
- }
18
+ CleanAllDatabases ( ) ;
21
19
20
+ ExecuteGenerationScript ( ) ;
21
+ }
22
+
23
+ public static void ExecuteGenerationScript ( )
24
+ {
22
25
SqlPreCommandConcat totalScript = ( SqlPreCommandConcat ) Schema . Current . GenerationScipt ( ) ! ;
23
26
foreach ( SqlPreCommand command in totalScript . Commands )
24
27
{
@@ -27,6 +30,15 @@ public static void TotalGeneration()
27
30
}
28
31
}
29
32
33
+ private static void CleanAllDatabases ( )
34
+ {
35
+ foreach ( var db in Schema . Current . DatabaseNames ( ) )
36
+ {
37
+ Connector . Current . CleanDatabase ( db ) ;
38
+ SafeConsole . WriteColor ( ConsoleColor . DarkGray , '.' ) ;
39
+ }
40
+ }
41
+
30
42
public static string GenerateViewCodes ( params string [ ] tableNames ) => tableNames . ToString ( tn => GenerateViewCode ( tn ) , "\r \n \r \n " ) ;
31
43
32
44
public static string GenerateViewCode ( string tableName ) => GenerateViewCode ( ObjectName . Parse ( tableName , Schema . Current . Settings . IsPostgres ) ) ;
@@ -77,12 +89,13 @@ private static string GenerateColumnCode(DiffColumn c)
77
89
return Schema . Current . GenerationScipt ( ) ;
78
90
}
79
91
80
-
92
+
93
+ public static Func < bool > ? AvoidSimpleGenerate ;
81
94
82
95
public static void NewDatabase ( )
83
96
{
84
97
var databaseName = Connector . Current . DatabaseName ( ) ;
85
- if ( Database . View < SysTables > ( ) . Any ( ) )
98
+ if ( Connector . Current . HasTables ( ) )
86
99
{
87
100
SafeConsole . WriteLineColor ( ConsoleColor . Red , $ "Are you sure you want to delete all the data in the database '{ databaseName } '?") ;
88
101
Console . Write ( $ "Confirm by writing the name of the database:") ;
@@ -95,8 +108,16 @@ public static void NewDatabase()
95
108
}
96
109
}
97
110
98
- Console . Write ( "Creating new database..." ) ;
99
- Administrator . TotalGeneration ( ) ;
111
+ Console . Write ( "Cleaning database..." ) ;
112
+ using ( Connector . CommandTimeoutScope ( 5 * 60 ) )
113
+ CleanAllDatabases ( ) ;
114
+ Console . WriteLine ( "Done." ) ;
115
+
116
+ if ( AvoidSimpleGenerate ? . Invoke ( ) == true )
117
+ return ;
118
+
119
+ Console . Write ( "Generating new database database..." ) ;
120
+ ExecuteGenerationScript ( ) ;
100
121
Console . WriteLine ( "Done." ) ;
101
122
}
102
123
@@ -262,7 +283,13 @@ join s in Database.View<SysSchemas>() on t.schema_id equals s.schema_id
262
283
}
263
284
}
264
285
286
+ public static bool ExistSchema ( SchemaName name )
287
+ {
288
+ if ( Schema . Current . Settings . IsPostgres )
289
+ return Database . View < PgNamespace > ( ) . Any ( ns => ns . nspname == name . Name ) ;
265
290
291
+ return Database . View < SysSchemas > ( ) . Any ( s => s . name == name . Name ) ;
292
+ }
266
293
267
294
public static List < T > TryRetrieveAll < T > ( Replacements replacements )
268
295
where T : Entity
@@ -582,7 +609,7 @@ from ifk in targetTable.IncommingForeignKeys()
582
609
ParentColumn = parentTable . Columns ( ) . SingleEx ( c => c . column_id == ifk . ForeignKeyColumns ( ) . SingleEx ( ) . parent_column_id ) . name ,
583
610
} ) . ToList ( ) ) ;
584
611
585
- foreignKeys . ForEach ( fk => sqlBuilder . AlterTableDropConstraint ( fk . ParentTable ! , fk . Name ! /*CSBUG*/ ) . ExecuteLeaves ( ) ) ;
612
+ foreignKeys . ForEach ( fk => sqlBuilder . AlterTableDropConstraint ( fk . ParentTable ! , fk . Name ) . ExecuteLeaves ( ) ) ;
586
613
587
614
return new Disposable ( ( ) =>
588
615
{
0 commit comments