private void generateOnUpgrade(SQLiteDatabaseSchema schema, List<SQLiteEntity> orderedEntities) {
		MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("onUpgrade").addAnnotation(Override.class)
				.addModifiers(Modifier.PUBLIC);
		methodBuilder.addParameter(SQLiteDatabase.class, "database");
		methodBuilder.addParameter(Integer.TYPE, "previousVersion");
		methodBuilder.addParameter(Integer.TYPE, "currentVersion");
		methodBuilder.addJavadoc("onUpgrade\n");

		Collections.reverse(orderedEntities);

		if (schema.isLogEnabled()) {
			// generate log section - BEGIN
			methodBuilder.addComment("log section BEGIN");
			methodBuilder.beginControlFlow("if (this.logEnabled)");

			methodBuilder.addStatement(
					"$T.info(\"Update database '%s' from version %s to version %s\",this.name, previousVersion, currentVersion)",
					Logger.class);

			// generate log section - END
			methodBuilder.endControlFlow();
			methodBuilder.addComment("log section END");
		}

		methodBuilder.addComment("if we have a list of update task, try to execute them");
		methodBuilder.beginControlFlow("if (options.updateTasks != null)");
		methodBuilder.addStatement("$T<$T> tasks = buildTaskList(previousVersion, currentVersion)", List.class,
				SQLiteUpdateTask.class);
		methodBuilder.beginControlFlow("for ($T task : tasks)", SQLiteUpdateTask.class);

		// generate log section - BEGIN
		methodBuilder.addComment("log section BEGIN");
		methodBuilder.beginControlFlow("if (this.logEnabled)");

		methodBuilder.addStatement(
				"$T.info(\"Begin update database from version %s to %s\", previousVersion, previousVersion+1)",
				Logger.class);

		// generate log section - END
		methodBuilder.endControlFlow();
		methodBuilder.addComment("log section END");

		methodBuilder.addStatement("task.execute(database, previousVersion, previousVersion+1)");

		// generate log section - BEGIN
		methodBuilder.addComment("log section BEGIN");
		methodBuilder.beginControlFlow("if (this.logEnabled)");

		methodBuilder.addStatement(
				"$T.info(\"End update database from version %s to %s\", previousVersion, previousVersion+1)",
				Logger.class);

		// generate log section - END
		methodBuilder.endControlFlow();
		methodBuilder.addComment("log section END");

		methodBuilder.addStatement("previousVersion++");
		methodBuilder.endControlFlow();
		methodBuilder.nextControlFlow("else");

		methodBuilder.addComment("drop all tables");
		methodBuilder.addStatement("$T.dropTablesAndIndices(database)", SQLiteUpdateTaskHelper.class);

		// reorder entities
		Collections.reverse(orderedEntities);

		methodBuilder.addCode("\n");
		methodBuilder.addCode("// generate tables\n");

		for (SQLiteEntity item : orderedEntities) {
			if (schema.isLogEnabled()) {
				// generate log section - BEGIN
				methodBuilder.addComment("log section BEGIN");
				methodBuilder.beginControlFlow("if (this.logEnabled)");

				methodBuilder.addCode("$T.info(\"DDL: %s\",$T.CREATE_TABLE_SQL);\n", Logger.class,
						BindTableGenerator.tableClassName(null, item));

				// generate log section - END
				methodBuilder.endControlFlow();
				methodBuilder.addComment("log section END");
			}
			methodBuilder.addCode("database.execSQL($T.CREATE_TABLE_SQL);\n",
					BindTableGenerator.tableClassName(null, item));
		}

		// use generated entities too
		for (GeneratedTypeElement item : schema.generatedEntities) {
			if (schema.isLogEnabled()) {
				// generate log section - BEGIN
				methodBuilder.addComment("log section BEGIN");
				methodBuilder.beginControlFlow("if (this.logEnabled)");
				methodBuilder.addStatement("$T.info(\"DDL: %s\",$T.CREATE_TABLE_SQL)", Logger.class,
						TypeUtility.className(BindTableGenerator.getTableClassName(item.getQualifiedName())));
				// generate log section - END
				methodBuilder.endControlFlow();
				methodBuilder.addComment("log section END");
			}
			methodBuilder.addStatement("database.execSQL($T.CREATE_TABLE_SQL)",
					TypeUtility.className(BindTableGenerator.getTableClassName(item.getQualifiedName())));
		}

		methodBuilder.endControlFlow();

		methodBuilder.beginControlFlow("if (options.databaseLifecycleHandler != null)");
		methodBuilder.addStatement(
				"options.databaseLifecycleHandler.onUpdate(database, previousVersion, currentVersion, true)");
		methodBuilder.endControlFlow();

		classBuilder.addMethod(methodBuilder.build());
	}
private void generateOnUpgrade ( SQLiteDatabaseSchema schema , List < SQLiteEntity > orderedEntities ) { MethodSpec . Builder methodBuilder = MethodSpec . methodBuilder ( "onUpgrade" ) . addAnnotation ( Override . class ) . addModifiers ( Modifier . PUBLIC ) ; methodBuilder . addParameter ( SQLiteDatabase . class , "database" ) ; methodBuilder . addParameter ( Integer . TYPE , "previousVersion" ) ; methodBuilder . addParameter ( Integer . TYPE , "currentVersion" ) ; methodBuilder . addJavadoc ( "onUpgrade\n" ) ; Collections . reverse ( orderedEntities ) ; if ( schema . isLogEnabled ( ) ) { methodBuilder . addComment ( "log section BEGIN" ) ; methodBuilder . beginControlFlow ( "if (this.logEnabled)" ) ; methodBuilder . addStatement ( "$T.info(\"Update database '%s' from version %s to version %s\",this.name, previousVersion, currentVersion)" , Logger . class ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . addComment ( "log section END" ) ; } methodBuilder . addComment ( "if we have a list of update task, try to execute them" ) ; methodBuilder . beginControlFlow ( "if (options.updateTasks != null)" ) ; methodBuilder . addStatement ( "$T<$T> tasks = buildTaskList(previousVersion, currentVersion)" , List . class , SQLiteUpdateTask . class ) ; methodBuilder . beginControlFlow ( "for ($T task : tasks)" , SQLiteUpdateTask . class ) ; methodBuilder . addComment ( "log section BEGIN" ) ; methodBuilder . beginControlFlow ( "if (this.logEnabled)" ) ; methodBuilder . addStatement ( "$T.info(\"Begin update database from version %s to %s\", previousVersion, previousVersion+1)" , Logger . class ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . addComment ( "log section END" ) ; methodBuilder . addStatement ( "task.execute(database, previousVersion, previousVersion+1)" ) ; methodBuilder . addComment ( "log section BEGIN" ) ; methodBuilder . beginControlFlow ( "if (this.logEnabled)" ) ; methodBuilder . addStatement ( "$T.info(\"End update database from version %s to %s\", previousVersion, previousVersion+1)" , Logger . class ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . addComment ( "log section END" ) ; methodBuilder . addStatement ( "previousVersion++" ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . nextControlFlow ( "else" ) ; methodBuilder . addComment ( "drop all tables" ) ; methodBuilder . addStatement ( "$T.dropTablesAndIndices(database)" , SQLiteUpdateTaskHelper . class ) ; Collections . reverse ( orderedEntities ) ; methodBuilder . addCode ( "\n" ) ; methodBuilder . addCode ( "// generate tables\n" ) ; for ( SQLiteEntity item : orderedEntities ) { if ( schema . isLogEnabled ( ) ) { methodBuilder . addComment ( "log section BEGIN" ) ; methodBuilder . beginControlFlow ( "if (this.logEnabled)" ) ; methodBuilder . addCode ( "$T.info(\"DDL: %s\",$T.CREATE_TABLE_SQL);\n" , Logger . class , BindTableGenerator . tableClassName ( null , item ) ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . addComment ( "log section END" ) ; } methodBuilder . addCode ( "database.execSQL($T.CREATE_TABLE_SQL);\n" , BindTableGenerator . tableClassName ( null , item ) ) ; } for ( GeneratedTypeElement item : schema . generatedEntities ) { if ( schema . isLogEnabled ( ) ) { methodBuilder . addComment ( "log section BEGIN" ) ; methodBuilder . beginControlFlow ( "if (this.logEnabled)" ) ; methodBuilder . addStatement ( "$T.info(\"DDL: %s\",$T.CREATE_TABLE_SQL)" , Logger . class , TypeUtility . className ( BindTableGenerator . getTableClassName ( item . getQualifiedName ( ) ) ) ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . addComment ( "log section END" ) ; } methodBuilder . addStatement ( "database.execSQL($T.CREATE_TABLE_SQL)" , TypeUtility . className ( BindTableGenerator . getTableClassName ( item . getQualifiedName ( ) ) ) ) ; } methodBuilder . endControlFlow ( ) ; methodBuilder . beginControlFlow ( "if (options.databaseLifecycleHandler != null)" ) ; methodBuilder . addStatement ( "options.databaseLifecycleHandler.onUpdate(database, previousVersion, currentVersion, true)" ) ; methodBuilder . endControlFlow ( ) ; classBuilder . addMethod ( methodBuilder . build ( ) ) ; }
Generate on upgrade.

@param schema
the schema
@param orderedEntities
the ordered entities