OSDN Git Service

JPAでのDB作成をやめ, liquibaseで作成するよう変更.
authoryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Thu, 12 Nov 2009 12:17:51 +0000 (12:17 +0000)
committeryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Thu, 12 Nov 2009 12:17:51 +0000 (12:17 +0000)
作成者カラムをテーブルへ追加

git-svn-id: http://192.168.11.7/svn/repository/NicoBrowser/branches/20091112_add_author@208 bdf3b611-c98c-6041-8292-703d9c9adbe7

nbproject/project.properties
src/META-INF/persistence.xml
src/nicobrowser/main/Main.java
src/nicobrowser/update/DBUpdater.java [new file with mode: 0644]
src/resources/db_for_sync_script.xml [new file with mode: 0644]
src/resources/db_update_script.xml [new file with mode: 0644]
test/nicobrowser/update/UpdaterTest.java [new file with mode: 0644]

index e92718a..4141fac 100644 (file)
@@ -21,6 +21,7 @@ dist.jar=${dist.dir}/NicoBrowser.jar
 dist.javadoc.dir=${dist.dir}/javadoc\r
 excludes=\r
 file.reference.commons-io-1.4.jar=F:\\data\\java\\commons\\commons-io-1.4\\commons-io-1.4.jar\r
+file.reference.liquibase-1.9.5.jar=F:\\data\\java\\liquibase-1.9.5\\liquibase-1.9.5.jar\r
 includes=**\r
 jar.compress=false\r
 javac.classpath=\\r
@@ -33,7 +34,8 @@ javac.classpath=\
     ${libs.HttpClient4.0.classpath}:\\r
     ${libs.NekoHtml.classpath}:\\r
     ${file.reference.commons-io-1.4.jar}:\\r
-    ${libs.groovy-all.classpath}\r
+    ${libs.groovy-all.classpath}:\\r
+    ${file.reference.liquibase-1.9.5.jar}\r
 # Space-separated list of extra javac options\r
 javac.compilerargs=\r
 javac.deprecation=false\r
index 4f93d6c..549ffaa 100644 (file)
@@ -8,7 +8,6 @@
       <property name="toplink.jdbc.password" value=""/>
       <property name="toplink.jdbc.url" value="jdbc:h2:db/NicoDB"/>
       <property name="toplink.jdbc.driver" value="org.h2.Driver"/>
-      <property name="toplink.ddl-generation" value="create-tables"/>
     </properties>
   </persistence-unit>
 </persistence>
index 0390aa5..09c9187 100644 (file)
@@ -1,6 +1,7 @@
 /*$Id$*/
 package nicobrowser.main;
 
+import nicobrowser.update.DBUpdater;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -32,6 +33,13 @@ public class Main {
             return;
         }
 
+        DBUpdater updater = new DBUpdater();
+        if (args.length > 0 && "sync".equals(args[0])) {
+            updater.sync();
+            return;
+        }
+        updater.update();
+
         new Main().start();
     }
 
diff --git a/src/nicobrowser/update/DBUpdater.java b/src/nicobrowser/update/DBUpdater.java
new file mode 100644 (file)
index 0000000..81df8a8
--- /dev/null
@@ -0,0 +1,94 @@
+/** */
+package nicobrowser.update;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import liquibase.commandline.Main;
+import liquibase.exception.CommandLineParsingException;
+import nicobrowser.Config;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class DBUpdater {
+
+    private static Log log = LogFactory.getLog(DBUpdater.class);
+    private static final String UPDATE_FILE = "db_update_script.xml";
+    private static final String SYNC_FILE = "db_for_sync_script.xml";
+
+    public void sync() throws IOException {
+        File updateFile = new File(new File(Config.getInstance().getDbFile()).getParent(), UPDATE_FILE);
+        InputStream resource = ClassLoader.getSystemResourceAsStream("resources/" + SYNC_FILE);
+
+        createFile(resource, updateFile);
+
+        String[] args = new String[]{
+            "--driver=org.h2.Driver",
+            "--changeLogFile=" + updateFile.toString(),
+            "--url=jdbc:h2:" + Config.getInstance().getDbFile(),
+            "--username=sa",
+            "changeLogSync"};
+        try {
+            Main.main(args);
+
+            log.info("DBのアップデートが終了しました.");
+        } catch (CommandLineParsingException ex) {
+            log.fatal("DBのアップデートに失敗しました.", ex);
+            throw new IOException(ex);
+        }
+    }
+
+    public void update() throws IOException {
+        File updateFile = new File(new File(Config.getInstance().getDbFile()).getParent(), UPDATE_FILE);
+        InputStream resource = ClassLoader.getSystemResourceAsStream("resources/" + UPDATE_FILE);
+
+        createFile(resource, updateFile);
+
+        String[] args = new String[]{
+            "--driver=org.h2.Driver",
+            "--changeLogFile=" + updateFile.toString(),
+            "--url=jdbc:h2:" + Config.getInstance().getDbFile(),
+            "--username=sa",
+            "update"};
+        try {
+            Main.main(args);
+
+            log.info("DBのアップデートが終了しました.");
+        } catch (CommandLineParsingException ex) {
+            log.fatal("DBのアップデートに失敗しました.", ex);
+            throw new IOException(ex);
+        }
+    }
+
+    private void createFile(InputStream resource, File updateFile) throws IOException {
+        BufferedInputStream is = null;
+        BufferedOutputStream os = null;
+        try {
+            is = new BufferedInputStream(resource);
+            os = new BufferedOutputStream(new FileOutputStream(updateFile));
+            byte[] b = new byte[1024];
+            int l;
+            while ((l = is.read(b)) != -1) {
+                os.write(b, 0, l);
+            }
+        } finally {
+            try {
+                if (is != null) {
+                    is.close();
+                }
+            } catch (IOException ex) {
+            }
+            if (os != null) {
+                try {
+                    os.close();
+                } catch (IOException ex) {
+                }
+            }
+        }
+    }
+}
diff --git a/src/resources/db_for_sync_script.xml b/src/resources/db_for_sync_script.xml
new file mode 100644 (file)
index 0000000..58a7f41
--- /dev/null
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>\r
+<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">\r
+    <changeSet author="yuki (generated)" id="1258025189836-1">\r
+        <createTable schemaName="PUBLIC" tableName="NICOCONTENT">\r
+            <column name="ID" type="DECIMAL(19,0)">\r
+                <constraints nullable="false" primaryKey="true" primaryKeyName="PRIMARY_KEY_4"/>\r
+            </column>\r
+            <column name="CONVERTEDMP4" type="DECIMAL(1,0)">\r
+                <constraints nullable="false"/>\r
+            </column>\r
+            <column name="NICOID" type="VARCHAR(255)">\r
+                <constraints nullable="false"/>\r
+            </column>\r
+            <column name="FILENAME" type="VARCHAR(255)"/>\r
+            <column name="CONVERTEDMP3" type="DECIMAL(1,0)">\r
+                <constraints nullable="false"/>\r
+            </column>\r
+            <column name="TITLE" type="VARCHAR(255)"/>\r
+            <column name="PAGELINK" type="VARCHAR(255)"/>\r
+            <column name="VERSION" type="DECIMAL(19,0)"/>\r
+            <column name="FAILTIMES" type="DECIMAL(10,0)"/>\r
+            <column name="STATUS" type="DECIMAL(10,0)">\r
+                <constraints nullable="false"/>\r
+            </column>\r
+        </createTable>\r
+    </changeSet>\r
+    <changeSet author="yuki (generated)" id="1258025189836-2">\r
+        <createTable schemaName="PUBLIC" tableName="SEQUENCE">\r
+            <column name="SEQ_NAME" type="VARCHAR(50)">\r
+                <constraints nullable="false" primaryKey="true" primaryKeyName="PRIMARY_KEY_7"/>\r
+            </column>\r
+            <column name="SEQ_COUNT" type="DECIMAL(19,0)"/>\r
+        </createTable>\r
+    </changeSet>\r
+</databaseChangeLog>\r
diff --git a/src/resources/db_update_script.xml b/src/resources/db_update_script.xml
new file mode 100644 (file)
index 0000000..5f41180
--- /dev/null
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>\r
+<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">\r
+    <changeSet author="yuki (generated)" id="1258025189836-1">\r
+        <createTable schemaName="PUBLIC" tableName="NICOCONTENT">\r
+            <column name="ID" type="DECIMAL(19,0)">\r
+                <constraints nullable="false" primaryKey="true" primaryKeyName="PRIMARY_KEY_4"/>\r
+            </column>\r
+            <column name="CONVERTEDMP4" type="DECIMAL(1,0)">\r
+                <constraints nullable="false"/>\r
+            </column>\r
+            <column name="NICOID" type="VARCHAR(255)">\r
+                <constraints nullable="false"/>\r
+            </column>\r
+            <column name="FILENAME" type="VARCHAR(255)"/>\r
+            <column name="CONVERTEDMP3" type="DECIMAL(1,0)">\r
+                <constraints nullable="false"/>\r
+            </column>\r
+            <column name="TITLE" type="VARCHAR(255)"/>\r
+            <column name="PAGELINK" type="VARCHAR(255)"/>\r
+            <column name="VERSION" type="DECIMAL(19,0)"/>\r
+            <column name="FAILTIMES" type="DECIMAL(10,0)"/>\r
+            <column name="STATUS" type="DECIMAL(10,0)">\r
+                <constraints nullable="false"/>\r
+            </column>\r
+        </createTable>\r
+    </changeSet>\r
+    <changeSet author="yuki (generated)" id="1258025189836-2">\r
+        <createTable schemaName="PUBLIC" tableName="SEQUENCE">\r
+            <column name="SEQ_NAME" type="VARCHAR(50)">\r
+                <constraints nullable="false" primaryKey="true" primaryKeyName="PRIMARY_KEY_7"/>\r
+            </column>\r
+            <column name="SEQ_COUNT" type="DECIMAL(19,0)"/>\r
+        </createTable>\r
+    </changeSet>\r
+    <changeSet author="yuki" id="1">\r
+        <addColumn schemaName="PUBLIC" tableName="NICOCONTENT">\r
+            <column name="AUTHOR" type="VARCHAR(255)"/>\r
+        </addColumn>\r
+    </changeSet>\r
+</databaseChangeLog>\r
diff --git a/test/nicobrowser/update/UpdaterTest.java b/test/nicobrowser/update/UpdaterTest.java
new file mode 100644 (file)
index 0000000..7d30943
--- /dev/null
@@ -0,0 +1,23 @@
+/** $Id$ */
+package nicobrowser.update;
+
+import java.io.IOException;
+import org.junit.Test;
+
+public class UpdaterTest {
+
+//    @Test
+    public void testUpdate() throws IOException {
+        System.out.println("update");
+        DBUpdater instance = new DBUpdater();
+        instance.update();
+        // TODO review the generated test code and remove the default call to fail.
+    }
+
+    @Test
+    public void testSyncAndUpdate() throws IOException{
+        DBUpdater updater = new DBUpdater();
+        updater.sync();
+        updater.update();
+    }
+}