All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.ebeaninternal.dbmigration.model.PlatformDdlWriter Maven / Gradle / Ivy

There is a newer version: 15.8.0
Show newest version
package io.ebeaninternal.dbmigration.model;

import io.avaje.applog.AppLog;
import io.ebean.DatabaseBuilder;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.util.IOUtils;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
import io.ebeaninternal.dbmigration.ddlgeneration.platform.PlatformDdl;
import io.ebeaninternal.dbmigration.migration.ChangeSet;
import io.ebeaninternal.dbmigration.migration.ChangeSetType;
import io.ebeaninternal.dbmigration.migration.Migration;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

import static java.lang.System.Logger.Level.ERROR;

/**
 * Writes migration changes as platform specific DDL.
 */
public class PlatformDdlWriter {

  private static final System.Logger logger = AppLog.getLogger(PlatformDdlWriter.class);

  private final DatabaseBuilder.Settings config;
  private final PlatformDdl platformDdl;
  private final int lockTimeoutSeconds;

  public PlatformDdlWriter(DatabasePlatform platform, DatabaseBuilder.Settings config, int lockTimeoutSeconds) {
    this.platformDdl = PlatformDdlBuilder.create(platform);
    this.config = config;
    this.lockTimeoutSeconds = lockTimeoutSeconds;
  }

  /**
   * Write the migration as platform specific ddl.
   */
  public void processMigration(Migration dbMigration, DdlWrite writer, File writePath, String fullVersion)
    throws IOException {
    DdlHandler handler = handler();
    handler.generateProlog(writer);
    if (lockTimeoutSeconds > 0) {
      String lockSql = platformDdl.setLockTimeout(lockTimeoutSeconds);
      if (lockSql != null) {
        writer.apply().append(lockSql).endOfStatement().newLine();
      }
    }
    List changeSets = dbMigration.getChangeSet();
    for (ChangeSet changeSet : changeSets) {
      if (isApply(changeSet)) {
        handler.generate(writer, changeSet);
      }
    }
    handler.generateEpilog(writer);
    writePlatformDdl(writer, writePath, fullVersion);
  }

  /**
   * Return true if the changeSet is APPLY and not empty.
   */
  private boolean isApply(ChangeSet changeSet) {
    return changeSet.getType() == ChangeSetType.APPLY && !changeSet.getChangeSetChildren().isEmpty();
  }

  /**
   * Write the ddl files.
   */
  protected void writePlatformDdl(DdlWrite writer, File resourcePath, String fullVersion) throws IOException {
    if (!writer.isApplyEmpty()) {
      try (Writer applyWriter = createWriter(resourcePath, fullVersion, ".sql")) {
        writeApplyDdl(applyWriter, writer);
        applyWriter.flush();
      }
    }
  }

  protected Writer createWriter(File path, String fullVersion, String suffix) throws IOException {
    File applyFile = new File(path, fullVersion + suffix);
    return IOUtils.newWriter(applyFile);
  }

  /**
   * Write the 'Apply' DDL buffers to the writer.
   */
  protected void writeApplyDdl(Writer writer, DdlWrite ddl) throws IOException {
    String header = config.getDdlHeader();
    if (header != null && !header.isEmpty()) {
      writer.append(header).append('\n');
    }
    ddl.writeApply(writer);
  }

  /**
   * Return the platform specific DdlHandler (to generate DDL).
   */
  protected DdlHandler handler() {
    return platformDdl.createDdlHandler(config);
  }

  /**
   * Return a sub directory (for multi-platform ddl generation).
   */
  public File subPath(File path, String suffix) {
    File subPath = new File(path, suffix);
    if (!subPath.exists()) {
      if (!subPath.mkdirs()) {
        logger.log(ERROR, "failed to create directories for " + subPath.getAbsolutePath());
      }
    }
    return subPath;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy