开发者

How to run sql scripts in order to update a Derby schema from java code?

The derby database installations of our customer sides have different schema versions. E.g. Customer1 has db schema version 4.1.5.0240, Customer2 has version 4.0.1.0330.

The idea is to update theses schemas to actual version when a new software is installed. Ther开发者_运维问答e exists several sql scripts in order update a version to next level. E.g. update_4.1.5.0240_to_4.3.1.0020.sql.

The tool/procedure I am looking for should read the actual installed version from a derby database table. According to that version the appropriate sql script shall be run in order to update the schema to next level. This procedure must be repeated until no matching sql script can be found for the actual read version.

The installation at customer site should be done headless without any administrators help.

1.) I would prefer a java program that reads the version using JDBC. But how to run the sql scripts from within java? Should I call the ij commandline tool from java?

2.) Are there better alternatives?

Thanks Viktor


You might find the ij.runScript method helpful: http://db.apache.org/derby/javadoc/publishedapi/jdbc4/org/apache/derby/tools/ij.html

One thing to be aware of is that it's not easy to automate the error handling inside the script, as there are no control flow (if/then/else) constructs in the ij scripting. But if you just need to perform a series of DDL operations in a clean manner, ij.runScript is a pretty good technique.


You could store the SQL in a text file:

From your java code you read in the SQL for updating/inserting data into the various tables:

Your java code could also perform checks on which 'SQL update file to run'.

something along these lines (in very pseudo code)

Installed = getCurrentInstalledVersion();//info stored in an admin table for current version

if (Installed < V4.1){//This would probably work better as a switch
String sql = updateWith(URL file:\\PathTov4.1\sql.txt);
}
else{
     if(Installed = OtherVersions){
      String sql = UpdateWith(URL file:\\PathToOtherVersion\sql.txt);
      }
}

Connect.sendSQL(sql);//send the sql to the DB

I leave you to figure out the fuller details. If you already found a solution, why not post it here.

If you use a switch you could run updates to a newer version via other versions if required by placing the versions in the correct order, and inserting code before any break;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜