Permalink
Browse files

db migration working

  • Loading branch information...
1 parent 26469d2 commit 8ba6ffb238b700ec3eb4c166db485ae3fe8cf5fc @joelhockey committed Jul 12, 2010
View
@@ -1,10 +1,3 @@
/*C4*/SET SCHEMA PUBLIC
CONNECT USER SA
-/*C5*/SET SCHEMA PUBLIC
-CONNECT USER SA
-DISCONNECT
-/*C6*/SET SCHEMA PUBLIC
-CONNECT USER SA
-/*C7*/SET SCHEMA PUBLIC
-CONNECT USER SA
DISCONNECT
View
@@ -1,5 +1,5 @@
#HSQL Database Engine 1.8.0.10
-#Thu Jul 08 22:35:26 EST 2010
+#Mon Jul 12 22:17:32 EST 2010
hsqldb.script_format=0
runtime.gc_interval=0
sql.enforce_strict_size=false
View
@@ -1,8 +1,11 @@
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
-CREATE MEMORY TABLE USER(NAME VARCHAR,LOGONS INTEGER)
+CREATE MEMORY TABLE DB_VERSION(VERSION INTEGER NOT NULL,CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,FILENAME VARCHAR,SCRIPT VARCHAR,CONSTRAINT UNQ_DB_VERSION_VERSION UNIQUE(VERSION))
CREATE MEMORY TABLE DUAL(DUMMY VARCHAR(1))
+CREATE MEMORY TABLE USER(NAME VARCHAR,LOGONS INTEGER)
CREATE USER SA PASSWORD ""
GRANT DBA TO SA
SET WRITE_DELAY 10
SET SCHEMA PUBLIC
+INSERT INTO DB_VERSION VALUES(0,'2010-07-12 19:10:35.765000000','000_init.sql','-- 0 init\u000d\u000acreate table db_version (\u000d\u000a version integer not null,\u000d\u000a created_at timestamp default current_timestamp,\u000d\u000a filename varchar,\u000d\u000a script varchar\u000d\u000a);\u000d\u000aalter table db_version add constraint unq_db_version_version unique (version);\u000d\u000a\u000d\u000acreate table dual(dummy varchar(1));\u000d\u000ainsert into dual(dummy) values (''X'');\u000d\u000a')
+INSERT INTO DB_VERSION VALUES(1,'2010-07-12 19:10:35.812000000','001_create.sql','create table user(\u000d\u000a name varchar,\u000d\u000a logons integer\u000d\u000a);\u000d\u000a')
INSERT INTO DUAL VALUES('X')
@@ -50,6 +50,7 @@
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.tools.shell.Global;
/**
@@ -166,7 +167,7 @@ public boolean parseFile(String fullpath) throws IOException {
}
public String readFile(String path, Object objOuts) throws IOException {
- if (objOuts == null) {
+ if (objOuts == null || objOuts instanceof Undefined) {
log.info("readFile: " + path);
return readFile(path);
} else {
@@ -251,7 +252,9 @@ private NativeObject loadjst(String name) throws IOException {
Function f = (Function) templates.get(name, templates);
return (NativeObject) f.construct(cx, this, null);
} catch (JavaScriptException jse) {
- throw new IOException("Error loading views/" + name + ".js: " + jse.getMessage(), jse);
+ IOException ioe = new IOException("Error loading views/" + name + ".js: " + jse.getMessage());
+ ioe.initCause(jse);
+ throw ioe;
} finally {
Context.exit();
}
@@ -71,6 +71,7 @@ public void init() throws ServletException {
CirrusScope scope = localScope.get();
scope.load("/WEB-INF/db/migrate.js");
} catch (Exception e) {
+ log.error("Error migrating db", e);
throw new ServletException("Error migrating db", e);
}
}
@@ -58,10 +58,10 @@ function getController(controller) {
var ic = new javax.naming.InitialContext();
var DATASOURCE = ic.lookup("jdbc/deuce");
-var DB = com.joelhockey.cirrus.DB
+var DB = com.joelhockey.cirrus.DB;
var dbconn = DATASOURCE.getConnection();
var stmtrs = DB.select(dbconn, "select * from user where name = ? or name = ? and logons > ?", ["alice", null, 3]);
-stmtrs.close()
+stmtrs.close();
print("dual size: " + DB.selectInt(dbconn, "select count(*) from dual"));
print("user size: " + DB.selectInt(dbconn, "select count(*) from user where name = ?", ["x"]));
@@ -1,12 +1,11 @@
-- 0 init
create table db_version (
version integer not null,
- created_at timestamp not null,
- filename varchar(255),
- script clob
+ created_at timestamp default current_timestamp,
+ filename varchar,
+ script varchar
);
+alter table db_version add constraint unq_db_version_version unique (version);
create table dual(dummy varchar(1));
insert into dual(dummy) values ('X');
-
--- 1 create
@@ -20,26 +20,26 @@
dbversion = DB.selectInt(dbconn, "select max(version) from db_version");
} catch (e) {
// error reading from 'db_version' table, try init script
- log("Error getting db version, will try and load init script", e);
+ log.warn("Error getting db version, will try and load init script: " + e);
var stmt = dbconn.createStatement();
var sql = readFile("/WEB-INF/db/000_init.sql");
- DB.insert("insert into db_version (version, created_at, desc, script) values (0, systimestamp, '000_init.sql', ?)", [sql]);
stmt.execute(sql);
stmt.close();
+ DB.insert(dbconn, "insert into db_version (version, filename, script) values (0, '000_init.sql', ?)", [sql]);
dbversion = DB.selectInt(dbconn, "select max(version) from db_version");
}
// check if up to date
+ var msg = "db at version: " + dbversion + ", app requires version: " + version;
if (dbversion === version) {
- log("already at expected version: " + version);
+ log.info("db version ok. " + msg);
return;
- } else if (dbversion > version) {
- // very strange
- throw new java.sql.SQLException("require version " + version + ", but db at: " + dbversion);
+ } else if (dbversion > version) { // very strange
+ throw new java.sql.SQLException(msg);
}
// move from dbversion to version
- log("doing db migration, current version: " + dbversion + ", app requires: " + version);
+ log.info("doing db migration. " + msg);
// look in dir /WEB-INF/db to find required files
var dbpath = sconf.getServletContext().getRealPath("/WEB-INF/db");
@@ -77,9 +77,9 @@
// run scripts
var stmt = dbconn.createStatement();
for (var i = dbversion + 1; i <= version; i++) {
- log("running script: " + fileMap[i]);
- DB.insert("insert into db_version (version, created_at, desc, script) values (?, systimestamp, ?, ?)", [i, fileMap[i], sql]);
+ log.info("db migration running script: " + fileMap[i]);
var sql = readFile("/WEB-INF/db/" + fileMap[i]);
+ DB.insert(dbconn, "insert into db_version (version, filename, script) values (?, ?, ?)", [i, fileMap[i], sql]);
stmt.execute(sql);
}
stmt.close();

0 comments on commit 8ba6ffb

Please sign in to comment.