Skip to content

Commit

Permalink
Fixed DB adapter connect/disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Aug 16, 2012
1 parent 1aef6b9 commit 48fb464
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lib/init/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ module.exports = new (function () {
config = _getAdapterConfig(geddy.config.db, adapterName);
adapterCtor = require(adapterPath).Adapter;
adapter = new adapterCtor(config);
if (typeof adapter.connect == 'function') {
adapter.connect();
}
model.loadedAdapters[adapterName] = adapter;
}
model.adapters[name] = adapter;
Expand Down
30 changes: 23 additions & 7 deletions templates/Jakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ var fs = require('fs')

namespace('env', function () {
task('init', function () {
geddy.model = require('model');
geddy.config = require('../lib/config').readConfig();
geddy.model = require('model');

require('../lib/init').init(complete);

Expand All @@ -22,22 +22,28 @@ namespace('env', function () {

task('cleanup', function () {
// Disconnect all the adapters
adapters = geddy.model.loadedAdapters;
adapters = geddy.model.loadedAdapters
, adapter;
for (var p in adapters) {
adapters[p].disconnect();
adapter = adapters[p];
if (typeof adapter.disconnect == 'function') {
adapter.disconnect();
}
}
});

}, {async: true});

namespace('db', function () {
task('retrofit', ['env:init'], function () {
var createTable

task('createTable', ['env:init'], function (name) {
var modelName = typeof name == 'string' ? [name] : name
, createTable
, adapters
, adapter
, models = Object.keys(geddy.model.descriptionRegistry);
, modelNames = Object.keys(geddy.model.descriptionRegistry);
createTable = function () {
if ((m = models.shift())) {
if ((m = modelNames.shift())) {
console.log('Creating table for ' + m);
adapter = geddy.model.adapters[m];
adapter.createTable(m, function (err, data) {
Expand All @@ -50,6 +56,16 @@ namespace('db', function () {
}
};
createTable();

}, {async: true});

task('retrofit', ['env:init'], function () {
var modelNames = Object.keys(geddy.model.descriptionRegistry)
, createTask = jake.Task['db:createTable'];
createTask.once('complete', function () {
complete();
});
createTask.invoke(modelNames);
}, {async: true});

});
Expand Down

0 comments on commit 48fb464

Please sign in to comment.