A5:SQL Mk-2のER図(.a5er)からDDLを生成する

TL;DR

コマンドラインユーティリティ自体の実行方法

A5:SQL Mk-2 コマンドラインユーティリティからzipファイルをダウンロードして適当な場所に展開します。

zipファイルには説明書も付属してます。
ここの説明はそれを元にしてるので、正確な詳しい内容は説明書を参照してください

次のように実行すると、c:\work\model.a5er に置いたER図から c:\work\model.ddlSQLDDL)が生成されます。

A5M2cmd.exe /ERDDL ^
/ERD=c:\work\model.a5er ^
/OutFileName=c:\work\model.sql

Maven で実行する方法

A5:SQL Mk-2 で ER 図を開いていると、他のプロセスからファイルを開けなくて失敗します

MavenDDLを生成できるようになっていると便利な気がします。

mvn generate-sources -Pgenerate-sources-ddl -Derd.path=c:/work/model.a5er

こういうふうに記述すると実現できます。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>a5m2cmd-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>a5m2cmd-demo</name>
    <packaging>jar</packaging>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
    </dependencies>

    <profiles>
        <profile>
            <id>generate-sources-ddl</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <erd.path>c:/work/model.a5er</erd.path>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>wagon-maven-plugin</artifactId>
                        <version>2.0.2</version>
                        <executions>
                            <!-- A5m2cmd.zipをダウンロード -->
                            <execution>
                                <id>fetch-a5m2command</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>download-single</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <skipIfExists>true</skipIfExists>
                            <url>https://ftp.vector.co.jp</url>
                            <fromFile>71/97/3301/A5M2cmd_2.14.3_x64.zip</fromFile>
                            <toFile>${project.build.directory}/A5M2cmd/A5M2cmd.zip</toFile>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.8</version>
                        <executions>
                            <!-- A5m2cmd.zipをtargetに展開 -->
                            <execution>
                                <id>extract-zip</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <unzip src="${project.build.directory}/A5M2cmd/A5M2cmd.zip" dest="${project.build.directory}/A5M2cmd" />
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>3.0.0</version>
                        <executions>
                            <!-- A5m2cmd.exeを実行してDDLを生成 -->
                            <execution>
                                <id>generate-sources-ddl</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>${project.build.directory}/A5M2cmd/A5M2cmd.exe</executable>
                                    <arguments>
                                        <argument>/ERDDL</argument>
                                        <argument>/ERD=${erd.path}</argument>
                                        <argument>/OutEncoding=UTF-8</argument>
                                        <argument>/OutFileName=${project.basedir}/src/main/resources/db/migration/V0_0_001__table.sql</argument>
                                        <argument>/RDBMSType=POSTGRESQL</argument>
                                        <argument>/CreateOrder=Dependent</argument>
                                        <argument>/GenerateComment=Y</argument>
                                        <argument>/GenerateDropTableStatement=Y</argument>
                                        <argument>/DropTableIfExists=Y</argument>
                                        <argument>/BackupRestoreTempTable=N</argument>
                                        <argument>/ForceQuoteIdentifier=N</argument>
                                        <argument>/CreatePkIndex=Y</argument>
                                        <argument>/CreateFk=Y</argument>
                                        <argument>/CreateFK_ParentCard1Only=Y</argument>
                                        <argument>/FKParentIndex=Y</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Gradle で実行する方法

こういう感じに実行できると便利そうです。

gradle generateDDL -Perd.path=c:/work/model.a5er

こういう風に記述すると実現できます。

plugins {
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

def erdPath = findProperty('erd.path') ?: 'er.a5er'

repositories {
    mavenCentral()
}

task fetchA5M2cmd {
    def url = 'https://ftp.vector.co.jp/71/97/3301/A5M2cmd_2.14.3_x64.zip'
    doLast {
        file("${buildDir}").mkdirs()
        def a5M2cmdZip = file("${buildDir}/A5M2cmd.zip")
        a5M2cmdZip.withOutputStream { os -> new URL(url).withInputStream { is -> os << is }}
    }
}

task unzipA5M2cmd(type: Copy) {

  def zipPath = file("${buildDir}/A5M2cmd.zip")
  def zipFile = file(zipPath)
  def outputDir = file("${buildDir}/A5M2cmd")

  from zipTree(zipFile)
  into outputDir
}

task generateDDL(type:Exec) {

    executable file("${buildDir}/A5M2cmd/A5M2cmd.exe")
    args = [
        "/ERDDL",
        "/ERD=${erdPath}",
        "/OutEncoding=UTF-8",
        "/OutFileName=${projectDir}/src/main/resources/db/migration/V0_0_001__table.sql",
        "/RDBMSType=POSTGRESQL",
        "/CreateOrder=Dependent",
        "/GenerateComment=Y",
        "/GenerateDropTableStatement=Y",
        "/DropTableIfExists=Y",
        "/BackupRestoreTempTable=N",
        "/ForceQuoteIdentifier=N",
        "/CreatePkIndex=Y",
        "/CreateFk=Y",
        "/CreateFK_ParentCard1Only=Y",
        "/FKParentIndex=Y",
    ]
}

tasks.generateDDL.dependsOn 'fetchA5M2cmd', 'unzipA5M2cmd'

おまけ:GitリポジトリでER図(.a5er)を管理するときは .gitattributes を作ったほうがいい

ER図(.a5er)ファイルの改行コードは CRLF ですが、ソースコードや設定ファイルの改行コードは LF になっている場合がほとんどです。

そういう状態のままER図を編集していると、コミット時に改行コードが変換されることを示す警告メッセージが表示されます。

改行コードの変換を避ける方法はいろいろあるのですが、 .gitattributes を設定するのがいいと思います。

Git - Git の属性

具体的にはリポジトリに .gitattributes というファイルを作成して次のように記述します。

*.a5er text eol=crlf