@@ -340,6 +340,9 @@ public String afterUpgrade(ISymmetricDialect symmetricDialect, String tablePrefi
340340 + " where source_node_id = ? and completed = 1)" ,
341341 engine .getNodeId (), engine .getNodeId ());
342342 }
343+ if (engine .getDatabasePlatform ().getName ().equals (DatabaseNamesConstants .H2 )) {
344+ createH2SequenceIfMissing (symmetricDialect , tablePrefix );
345+ }
343346 engine .getPullService ().pullConfigData (false );
344347 return sb .toString ();
345348 }
@@ -532,6 +535,34 @@ protected boolean isUpgradeFromPre316(String tablePrefix, Database currentModel)
532535 return table != null && table .findColumn ("extract_thread_id" ) == null ;
533536 }
534537
538+ protected void createH2SequenceIfMissing (ISymmetricDialect symmetricDialect , String tablePrefix ) {
539+ String dataIdSequenceName = symmetricDialect .getSequenceName (SequenceIdentifier .DATA ).toUpperCase () + "_SEQ" ;
540+ if (engine .getSqlTemplate ().queryForInt ("select count(*) from information_schema.sequences where sequence_name = ?" , dataIdSequenceName ) == 0 ) {
541+ String dataTableName = TableConstants .getTableName (tablePrefix , TableConstants .SYM_DATA );
542+ ISqlTransaction transaction = null ;
543+ try {
544+ transaction = engine .getSqlTemplate ().startSqlTransaction ();
545+ transaction .prepareAndExecute ("set exclusive 1" );
546+ long maxDataId = Math .max (transaction .queryForInt ("select max(data_id) from " + dataTableName ), 1 );
547+ log .info ("After upgrade, creating {} sequence with a value of {} because it doesn't exist" , dataIdSequenceName , maxDataId + 1 );
548+ transaction .prepareAndExecute ("create sequence \" " + dataIdSequenceName + "\" start with " + (maxDataId + 1 ));
549+ log .info ("After upgrade, altering {}.data_id to use the new sequence instead of auto_increment" , dataTableName );
550+ transaction .prepareAndExecute ("alter table " + dataTableName
551+ + " alter column data_id bigint not null default nextval('" + dataIdSequenceName + "')" );
552+ transaction .prepareAndExecute ("set exclusive 0" );
553+ } catch (Exception e ) {
554+ log .info ("Unable to create sequence: {}" , e .getMessage ());
555+ if (transaction != null ) {
556+ transaction .rollback ();
557+ }
558+ } finally {
559+ if (transaction != null ) {
560+ transaction .close ();
561+ }
562+ }
563+ }
564+ }
565+
535566 @ Override
536567 public void setSymmetricEngine (ISymmetricEngine engine ) {
537568 this .engine = engine ;
0 commit comments