@@ -11,14 +11,17 @@ namespace Signum.Engine;
1111
1212public static class Administrator
1313{
14+ public static Func < bool > ? OnTotalGeneration ;
15+
1416 public static void TotalGeneration ( )
1517 {
16- foreach ( var db in Schema . Current . DatabaseNames ( ) )
17- {
18- Connector . Current . CleanDatabase ( db ) ;
19- SafeConsole . WriteColor ( ConsoleColor . DarkGray , '.' ) ;
20- }
18+ CleanAllDatabases ( ) ;
2119
20+ ExecuteGenerationScript ( ) ;
21+ }
22+
23+ public static void ExecuteGenerationScript ( )
24+ {
2225 SqlPreCommandConcat totalScript = ( SqlPreCommandConcat ) Schema . Current . GenerationScipt ( ) ! ;
2326 foreach ( SqlPreCommand command in totalScript . Commands )
2427 {
@@ -27,6 +30,15 @@ public static void TotalGeneration()
2730 }
2831 }
2932
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+
3042 public static string GenerateViewCodes ( params string [ ] tableNames ) => tableNames . ToString ( tn => GenerateViewCode ( tn ) , "\r \n \r \n " ) ;
3143
3244 public static string GenerateViewCode ( string tableName ) => GenerateViewCode ( ObjectName . Parse ( tableName , Schema . Current . Settings . IsPostgres ) ) ;
@@ -77,12 +89,13 @@ private static string GenerateColumnCode(DiffColumn c)
7789 return Schema . Current . GenerationScipt ( ) ;
7890 }
7991
80-
92+
93+ public static Func < bool > ? AvoidSimpleGenerate ;
8194
8295 public static void NewDatabase ( )
8396 {
8497 var databaseName = Connector . Current . DatabaseName ( ) ;
85- if ( Database . View < SysTables > ( ) . Any ( ) )
98+ if ( Connector . Current . HasTables ( ) )
8699 {
87100 SafeConsole . WriteLineColor ( ConsoleColor . Red , $ "Are you sure you want to delete all the data in the database '{ databaseName } '?") ;
88101 Console . Write ( $ "Confirm by writing the name of the database:") ;
@@ -95,8 +108,16 @@ public static void NewDatabase()
95108 }
96109 }
97110
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 ( ) ;
100121 Console . WriteLine ( "Done." ) ;
101122 }
102123
@@ -262,7 +283,13 @@ join s in Database.View<SysSchemas>() on t.schema_id equals s.schema_id
262283 }
263284 }
264285
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 ) ;
265290
291+ return Database . View < SysSchemas > ( ) . Any ( s => s . name == name . Name ) ;
292+ }
266293
267294 public static List < T > TryRetrieveAll < T > ( Replacements replacements )
268295 where T : Entity
@@ -582,7 +609,7 @@ from ifk in targetTable.IncommingForeignKeys()
582609 ParentColumn = parentTable . Columns ( ) . SingleEx ( c => c . column_id == ifk . ForeignKeyColumns ( ) . SingleEx ( ) . parent_column_id ) . name ,
583610 } ) . ToList ( ) ) ;
584611
585- foreignKeys . ForEach ( fk => sqlBuilder . AlterTableDropConstraint ( fk . ParentTable ! , fk . Name ! /*CSBUG*/ ) . ExecuteLeaves ( ) ) ;
612+ foreignKeys . ForEach ( fk => sqlBuilder . AlterTableDropConstraint ( fk . ParentTable ! , fk . Name ) . ExecuteLeaves ( ) ) ;
586613
587614 return new Disposable ( ( ) =>
588615 {
0 commit comments