001package io.ebeaninternal.dbmigration.model;
002
003import io.ebean.config.DatabaseConfig;
004import io.ebean.config.dbplatform.DatabasePlatform;
005import io.ebean.util.IOUtils;
006import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler;
007import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
008import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
009import io.ebeaninternal.dbmigration.ddlgeneration.platform.PlatformDdl;
010import io.ebeaninternal.dbmigration.migration.ChangeSet;
011import io.ebeaninternal.dbmigration.migration.ChangeSetType;
012import io.ebeaninternal.dbmigration.migration.Migration;
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016import java.io.File;
017import java.io.IOException;
018import java.io.Writer;
019import java.util.List;
020
021/**
022 * Writes migration changes as platform specific DDL.
023 */
024public class PlatformDdlWriter {
025
026  private static final Logger logger = LoggerFactory.getLogger(PlatformDdlWriter.class);
027
028  private final DatabaseConfig databaseConfig;
029  private final PlatformDdl platformDdl;
030  private final int lockTimeoutSeconds;
031
032  public PlatformDdlWriter(DatabasePlatform platform, DatabaseConfig dbConfig, int lockTimeoutSeconds) {
033    this.platformDdl = PlatformDdlBuilder.create(platform);
034    this.databaseConfig = dbConfig;
035    this.lockTimeoutSeconds = lockTimeoutSeconds;
036  }
037
038  /**
039   * Write the migration as platform specific ddl.
040   */
041  public void processMigration(Migration dbMigration, DdlWrite writer, File writePath, String fullVersion)
042    throws IOException {
043    DdlHandler handler = handler();
044    handler.generateProlog(writer);
045    if (lockTimeoutSeconds > 0) {
046      String lockSql = platformDdl.setLockTimeout(lockTimeoutSeconds);
047      if (lockSql != null) {
048        writer.apply().append(lockSql).endOfStatement().newLine();
049      }
050    }
051    List<ChangeSet> changeSets = dbMigration.getChangeSet();
052    for (ChangeSet changeSet : changeSets) {
053      if (isApply(changeSet)) {
054        handler.generate(writer, changeSet);
055      }
056    }
057    handler.generateEpilog(writer);
058    writePlatformDdl(writer, writePath, fullVersion);
059  }
060
061  /**
062   * Return true if the changeSet is APPLY and not empty.
063   */
064  private boolean isApply(ChangeSet changeSet) {
065    return changeSet.getType() == ChangeSetType.APPLY && !changeSet.getChangeSetChildren().isEmpty();
066  }
067
068  /**
069   * Write the ddl files.
070   */
071  protected void writePlatformDdl(DdlWrite writer, File resourcePath, String fullVersion) throws IOException {
072    if (!writer.isApplyEmpty()) {
073      try (Writer applyWriter = createWriter(resourcePath, fullVersion, ".sql")) {
074        writeApplyDdl(applyWriter, writer);
075        applyWriter.flush();
076      }
077    }
078  }
079
080  protected Writer createWriter(File path, String fullVersion, String suffix) throws IOException {
081    File applyFile = new File(path, fullVersion + suffix);
082    return IOUtils.newWriter(applyFile);
083  }
084
085  /**
086   * Write the 'Apply' DDL buffers to the writer.
087   */
088  protected void writeApplyDdl(Writer writer, DdlWrite ddl) throws IOException {
089    String header = databaseConfig.getDdlHeader();
090    if (header != null && !header.isEmpty()) {
091      writer.append(header).append('\n');
092    }
093    ddl.writeApply(writer);
094  }
095
096  /**
097   * Return the platform specific DdlHandler (to generate DDL).
098   */
099  protected DdlHandler handler() {
100    return platformDdl.createDdlHandler(databaseConfig);
101  }
102
103  /**
104   * Return a sub directory (for multi-platform ddl generation).
105   */
106  public File subPath(File path, String suffix) {
107    File subPath = new File(path, suffix);
108    if (!subPath.exists()) {
109      if (!subPath.mkdirs()) {
110        logger.error("failed to create directories for " + subPath.getAbsolutePath());
111      }
112    }
113    return subPath;
114  }
115
116}