mod_storage_sql: Auto-initialize SQLite3 database.
authorWaqas Hussain <waqas20@gmail.com>
Sun, 12 Dec 2010 00:39:24 +0000 (05:39 +0500)
committerWaqas Hussain <waqas20@gmail.com>
Sun, 12 Dec 2010 00:39:24 +0000 (05:39 +0500)
plugins/mod_storage_sql.lua

index 68b381344a0b182f32cd21746c1c456eb7c4b0bb..593acd3151813f3d05d12a51808ccf3f3a731202 100644 (file)
@@ -47,6 +47,19 @@ do -- process options to get a db connection
 
        dbh:autocommit(false); -- don't commit automatically
        connection = dbh;
+       
+       if params.driver == "SQLite3" then -- auto initialize
+               local stmt = assert(connection:prepare("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='Prosody';"));
+               local ok = assert(stmt:execute());
+               local count = stmt:fetch()[1];
+               if count == 0 then
+                       local stmt = assert(connection:prepare("CREATE TABLE Prosody (host TEXT, user TEXT, store TEXT, key TEXT, subkey TEXT, type TEXT, value TEXT);"));
+                       assert(stmt:execute());
+                       assert(connection:commit());
+                       module:log("debug", "Initialized new SQLite3 database");
+               end
+               --print("===", json.stringify())
+       end
 end
 
 local function serialize(value)