1515import java .sql .ResultSet ;
1616import java .sql .Timestamp ;
1717import java .text .MessageFormat ;
18- import java .util .ArrayList ;
1918import java .util .List ;
2019import java .util .Set ;
2120import java .util .concurrent .TimeUnit ;
@@ -45,10 +44,10 @@ public Connection getConnection() {
4544 int pauseTimeSeconds = DBGitConfig .getInstance ().getInteger ("core" , "TRY_DELAY" , DBGitConfig .getInstance ().getIntegerGlobal ("core" , "TRY_DELAY" , 1000 ));
4645 int currentTry = 0 ;
4746
48- if (connect .isValid (0 ))
47+ if (connect .isValid (0 )){
48+ connect .setAutoCommit (false );
4949 return connect ;
50-
51- else {
50+ } else {
5251 ConsoleWriter .println ("Connection lost, trying to reconnect..." );
5352 while (currentTry <= maxTriesCount ) {
5453 TimeUnit .SECONDS .sleep (pauseTimeSeconds );
@@ -60,6 +59,7 @@ public Connection getConnection() {
6059 conn = DBConnection .getInstance (true );
6160 ConsoleWriter .println ("Successful reconnect" );
6261 connect = conn .getConnect ();
62+ connect .setAutoCommit (false );
6363 return connect ;
6464 }
6565 }
@@ -92,11 +92,19 @@ public void restoreDataBase(IMapMetaObject updateObjs) throws Exception {
9292 DBGitLang lang = DBGitLang .getInstance ();
9393
9494 try {
95- List <MetaTable > tables = new ArrayList <>();
95+ SortedListMetaObject tables = new SortedListMetaObject (updateObjs .values ().stream ().filter (x ->x instanceof MetaTable ).collect (Collectors .toList ()));
96+ SortedListMetaObject tablesExists = new SortedListMetaObject (updateObjs .values ().stream ().filter (x ->x instanceof MetaTable && isExists (x )).collect (Collectors .toList ()));
97+
9698 Set <String > createdSchemas = getSchemes ().values ().stream ().map (DBOptionsObject ::getName ).collect (Collectors .toSet ());
9799 Set <String > createdRoles = getRoles ().values ().stream ().map (DBRole ::getName ).collect (Collectors .toSet ());
98100
99- for (IMetaObject obj : updateObjs .getSortedList ().sortFromFree ()) {
101+ // remove table indexes and constraints, which is step(-2) of restoreMetaObject(MetaTable)
102+ ConsoleWriter .println ("Dropping constraints for all updating tables..." );
103+ for (IMetaObject table : tablesExists .sortFromDependencies ()) {
104+ getFactoryRestore ().getAdapterRestore (DBGitMetaType .DBGitTable , this ).restoreMetaObject (table , -2 );
105+ }
106+
107+ for (IMetaObject obj : updateObjs .getSortedList ().sortFromReferenced ()) {
100108 Timestamp timestampBefore = new Timestamp (System .currentTimeMillis ());
101109 int step = 0 ;
102110 boolean res = false ;
@@ -113,15 +121,15 @@ public void restoreDataBase(IMapMetaObject updateObjs) throws Exception {
113121 res = restoreAdapter .restoreMetaObject (obj , step ++);
114122
115123 if (step > 100 ) { throw new Exception (lang .getValue ("errors" , "restore" , "restoreErrorDidNotReturnTrue" ).toString ()); }
116- if (obj instanceof MetaTable ){ tables .add ((MetaTable ) obj ); }
117124 }
118125
119126 Long timeDiff = new Timestamp (System .currentTimeMillis ()).getTime () - timestampBefore .getTime ();
120127 ConsoleWriter .detailsPrintlnGreen (MessageFormat .format ("({1} {2})" , obj .getName (), timeDiff , lang .getValue ("general" , "add" , "ms" )));
121128 }
122129
123130 // restore table constraints, which is step(-1) of restoreMetaObject(MetaTable)
124- for (MetaTable table : tables ) {
131+ ConsoleWriter .println ("Restoring constraints for all updated tables..." );
132+ for (IMetaObject table : tables .sortFromReferenced ()) {
125133 getFactoryRestore ().getAdapterRestore (DBGitMetaType .DBGitTable , this ).restoreMetaObject (table , -1 );
126134 }
127135 connect .commit ();
@@ -144,7 +152,7 @@ public void deleteDataBase(IMapMetaObject deleteObjs, boolean isDeleteFromIndex)
144152 DBGitIndex index = DBGitIndex .getInctance ();
145153
146154 try {
147- List <IMetaObject > deleteObjsSorted = deleteObjs .getSortedList ().sortFromDependant ();
155+ List <IMetaObject > deleteObjsSorted = deleteObjs .getSortedList ().sortFromDependencies ();
148156 for (IMetaObject obj : deleteObjsSorted ) {
149157 getFactoryRestore ().getAdapterRestore (obj .getType (), this ).removeMetaObject (obj );
150158 if (isDeleteFromIndex ) index .removeItem (obj );
@@ -219,6 +227,17 @@ private void createRoleIfNeed(IMetaObject obj, Set<String> createdRoles) throws
219227 }
220228
221229 }
230+ protected boolean isExists (IMetaObject obj ){
231+ try {
232+ IDBBackupAdapter backupAdapter = getBackupAdapterFactory ().getBackupAdapter (AdapterFactory .createAdapter ());
233+ return backupAdapter .isExists (
234+ obj .getUnderlyingDbObject ().getSchema (),
235+ obj .getUnderlyingDbObject ().getName ()
236+ );
237+ } catch (Exception ex ) {
238+ return false ;
239+ }
240+ }
222241
223242 private boolean checkContainsNativeFields (IMetaObject obj ){
224243 if (obj instanceof MetaTable ) {
0 commit comments