14 Haziran 2023 Çarşamba

SpringData Flyway Callbacks

Giriş
Şu satırı dahil ederiz
import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.callback.Context;
import org.flywaydb.core.api.callback.Event;
Flyway Callbacks kullanım senaryosu şöyle
Rebuilding materialized views – we might want to rebuild materialized views whenever we apply migrations affecting the base tables of those views. SQL callbacks are a good fit for executing this type of logic
Flushing a cache – perhaps we have a migration that modifies data that happens to be cached. We can use callbacks to flush caches making sure that our application pulls fresh data from the database
Calling an external system – using callbacks, we can call out to an external system using an arbitrary technology. For example, we might want to publish an event, send an email, or trigger a server restart
Örnek - AFTER_MIGRATE
Şöyle yaparız
@Component  
public class CsvUpsertCallback implements Callback {  
  @Autowired  
  private ResourceLoader resourceLoader;  
  @Override  
  public String getCallbackName() { return "csv_upsert_callback";}  
  @Override  
  public boolean supports(Event event, Context context) { 
    return event == Event.AFTER_MIGRATE;
  }  
  @Override  
  public boolean canHandleInTransaction(Event event, Context context) {return true;}  
  @Override  
  public void handle(Event event, Context context) {  
    Connection connection = context.getConnection();  
    try {  
      // Get the path to the CSV directory  
      PathMatchingResourcePatternResolver resolver = 
        new PathMatchingResourcePatternResolver(resourceLoader);  
      Resource[] resources = resolver.getResources("classpath:csv/*");  
      List<File> files = new ArrayList<>();  
      for (Resource resource : resources) {  
        File file = resource.getFile();  
        if (file.getName().toLowerCase().endsWith(".csv")){  
          files.add(file);  
        }  
      }  
     for (File csvFile : files) {  
       processCsvFile(csvFile, connection);  
     }  
    } catch (IOException | SQLException e) {  
      throw new SQLException("Error processing CSV files", e);  
    }  
  }   
}
Şöyle yaparız
private void processCsvFile(File csvFile, Connection connection)
throws IOException, SQLException {  
  String tableName = "my_table";  
  try (BufferedReader reader = new BufferedReader(new FileReader(csvFile))) {  
    String line;  
    while ((line = reader.readLine()) != null) {  
      // Split the line into columns  
      String[] columns = line.split(",");  
      
      // Perform UPSERT operation  
      String sql = "INSERT INTO " + tableName + " (id, name, email) " +  
          "VALUES (?, ?, ?) " +  
          "ON CONFLICT (id) " +  
          "DO UPDATE SET name = EXCLUDED.name, email = EXCLUDED.email";  
      
      try (PreparedStatement statement = connection.prepareStatement(sql)) {  
       statement.setInt(1, Integer.parseInt(columns[0]));  
       statement.setString(2, columns[1]);  
       statement.setString(3, columns[2]);  
       statement.executeUpdate();  
      }  
    }  
  }  
}

Hiç yorum yok:

Yorum Gönder