不要怂,就是干,撸起袖子干!

Commit 27c447c0 by Sascha Depold

unified metadata receival

1 parent e893107e
...@@ -70,6 +70,27 @@ module.exports = (function() { ...@@ -70,6 +70,27 @@ module.exports = (function() {
result = handleShowTableQuery.call(this, data) result = handleShowTableQuery.call(this, data)
} else if (isShowOrDescribeQuery.call(this)) { } else if (isShowOrDescribeQuery.call(this)) {
result = data result = data
if (this.sql.toLowerCase().indexOf('describe') === 0) {
result = result.map(function(result) {
return {
attribute: result.Field,
type: result.Type.toUpperCase(),
allowNull: (result.Null === 'YES'),
defaultValue: result.Default
}
})
} else if (this.sql.toLowerCase().indexOf('show index from') === 0) {
result = Utils._.uniq(result.map(function(result) {
return {
name: result.Key_name,
tableName: result.Table,
unique: (result.Non_unique !== 1)
}
}), false, function(row) {
return row.name
})
}
} else if (isCallQuery.call(this)) { } else if (isCallQuery.call(this)) {
result = data[0] result = data[0]
} }
......
...@@ -59,11 +59,34 @@ module.exports = (function() { ...@@ -59,11 +59,34 @@ module.exports = (function() {
, isRelNameQuery = (this.sql.indexOf('SELECT relname FROM pg_class WHERE oid IN') === 0) , isRelNameQuery = (this.sql.indexOf('SELECT relname FROM pg_class WHERE oid IN') === 0)
if (isTableNameQuery || isRelNameQuery) { if (isTableNameQuery || isRelNameQuery) {
return this.emit('success', rows.map(function(row) { return Utils._.values(row) })) if (isRelNameQuery) {
results = rows.map(function(row) {
return {
name: row.relname,
tableName: row.relname.split('_')[0]
}
})
} else {
results = rows.map(function(row) { return Utils._.values(row) })
}
return this.emit('success', results)
} }
if (this.send('isSelectQuery')) { if (this.send('isSelectQuery')) {
this.emit('success', this.send('handleSelectQuery', rows)) if (this.sql.toLowerCase().indexOf('select column_name') === 0) {
rows = rows.map(function(result) {
return {
attribute: result.Field,
type: result.Type.toUpperCase(),
allowNull: (result.Null === 'YES'),
defaultValue: result.Default
}
})
this.emit('success', rows)
} else {
this.emit('success', this.send('handleSelectQuery', rows))
}
} else if (this.send('isShowOrDescribeQuery')) { } else if (this.send('isShowOrDescribeQuery')) {
this.emit('success', results) this.emit('success', results)
} else if (this.send('isInsertQuery')) { } else if (this.send('isInsertQuery')) {
......
...@@ -103,10 +103,23 @@ module.exports = (function() { ...@@ -103,10 +103,23 @@ module.exports = (function() {
result = results result = results
} else if (this.sql.indexOf('PRAGMA INDEX_LIST') !== -1) { } else if (this.sql.indexOf('PRAGMA INDEX_LIST') !== -1) {
// this is the sqlite way of getting the indexes of a table // this is the sqlite way of getting the indexes of a table
result = results.map(function(result) { return { Key_name: result.name } }) result = results.map(function(result) {
return {
name: result.name,
tableName: result.name.split('_')[0],
unique: (result.unique === 0)
}
})
} else if (this.sql.indexOf('PRAGMA TABLE_INFO') !== -1) { } else if (this.sql.indexOf('PRAGMA TABLE_INFO') !== -1) {
// this is the sqlite way of getting the metadata of a table // this is the sqlite way of getting the metadata of a table
console.log(results) result = results.map(function(result) {
return {
attribute: result.name,
type: result.type,
allowNull: (result.notnull === 0),
defaultValue: result.dflt_value
}
})
} }
this.emit('success', result) this.emit('success', result)
......
...@@ -138,14 +138,15 @@ module.exports = (function() { ...@@ -138,14 +138,15 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
self.describeTable(tableName).success(function(data) { self.describeTable(tableName).success(function(data) {
data = data.filter(function(h) { return h.Field == attrNameBefore })[0] || {} data = data.filter(function(h) { return h.attribute == attrNameBefore })[0] || {}
var options = {} var options = {}
options[attrNameAfter] = { options[attrNameAfter] = {
type: data.Type, attribute: data.attribute,
allowNull: data.Null == 'YES', type: data.type,
defaultValue: data.Default allowNull: data.allowNull,
defaultValue: data.defaultValue
} }
var sql = self.QueryGenerator.renameColumnQuery(tableName, var sql = self.QueryGenerator.renameColumnQuery(tableName,
......
...@@ -121,9 +121,9 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() { ...@@ -121,9 +121,9 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
}) })
}) })
;(dialect === 'sqlite' ? itEventually : it)("executes migration #20111117063700 and correctly adds isBetaMember", function(done) { it("executes migration #20111117063700 and correctly adds isBetaMember", function(done) {
this.sequelize.getQueryInterface().describeTable('Person').success(function(data) { this.sequelize.getQueryInterface().describeTable('Person').success(function(data) {
var fields = data.map(function(d) { return d.Field }).sort() var fields = data.map(function(d) { return d.attribute }).sort()
expect(fields).toEqual([ 'isBetaMember', 'name' ]) expect(fields).toEqual([ 'isBetaMember', 'name' ])
done() done()
}) })
...@@ -190,7 +190,6 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() { ...@@ -190,7 +190,6 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
this.init({ from: 20111117063700, to: 20111205162700 }, function(migrator) { this.init({ from: 20111117063700, to: 20111205162700 }, function(migrator) {
migrator.migrate().success(function() { migrator.migrate().success(function() {
this.sequelize.getQueryInterface().describeTable('User').success(function(data) { this.sequelize.getQueryInterface().describeTable('User').success(function(data) {
console.log(data)
var signature = data.filter(function(hash){ return hash.Field == 'signature' })[0] var signature = data.filter(function(hash){ return hash.Field == 'signature' })[0]
, isAdmin = data.filter(function(hash){ return hash.Field == 'isAdmin' })[0] , isAdmin = data.filter(function(hash){ return hash.Field == 'isAdmin' })[0]
, shopId = data.filter(function(hash){ return hash.Field == 'shopId' })[0] , shopId = data.filter(function(hash){ return hash.Field == 'shopId' })[0]
...@@ -217,16 +216,16 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() { ...@@ -217,16 +216,16 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
this.init({ to: 20111206061400 }, function(migrator) { this.init({ to: 20111206061400 }, function(migrator) {
migrator.migrate().success(function(){ migrator.migrate().success(function(){
this.sequelize.getQueryInterface().describeTable('User').success(function(data) { this.sequelize.getQueryInterface().describeTable('User').success(function(data) {
var signature = data.filter(function(hash){ return hash.Field == 'signature' })[0] var signature = data.filter(function(hash){ return hash.attribute == 'signature' })[0]
, isAdmin = data.filter(function(hash){ return hash.Field == 'isAdmin' })[0] , isAdmin = data.filter(function(hash){ return hash.attribute == 'isAdmin' })[0]
, shopId = data.filter(function(hash){ return hash.Field == 'shopId' })[0] , shopId = data.filter(function(hash){ return hash.attribute == 'shopId' })[0]
expect(signature.Field).toEqual('signature') expect(signature.attribute).toEqual('signature')
expect(signature.Null).toEqual('NO') expect(signature.allowNull).toEqual(false)
expect(isAdmin.Field).toEqual('isAdmin') expect(isAdmin.attribute).toEqual('isAdmin')
expect(isAdmin.Null).toEqual('NO') expect(isAdmin.allowNull).toEqual(false)
expect(isAdmin.Default).toEqual('0') expect(isAdmin.defaultValue).toEqual('0')
expect(shopId).toBeFalsy() expect(shopId).toBeFalsy()
...@@ -242,12 +241,12 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() { ...@@ -242,12 +241,12 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
this.init({ to: 20111206063000 }, function(migrator) { this.init({ to: 20111206063000 }, function(migrator) {
migrator.migrate().success(function() { migrator.migrate().success(function() {
this.sequelize.getQueryInterface().describeTable('User').success(function(data) { this.sequelize.getQueryInterface().describeTable('User').success(function(data) {
var signature = data.filter(function(hash){ return hash.Field == 'signature' })[0] var signature = data.filter(function(hash){ return hash.attribute == 'signature' })[0]
expect(signature.Field).toEqual('signature') expect(signature.attribute).toEqual('signature')
expect(signature.Type).toEqual('varchar(255)') expect(signature.type).toEqual('VARCHAR(255)')
expect(signature.Null).toEqual('NO') expect(signature.allowNull).toEqual(false)
expect(signature.Default).toEqual('Signature') expect(signature.defaultValue).toEqual('Signature')
done() done()
}) })
...@@ -262,8 +261,8 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() { ...@@ -262,8 +261,8 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
this.init({ to: 20111206163300 }, function(migrator) { this.init({ to: 20111206163300 }, function(migrator) {
migrator.migrate().success(function(){ migrator.migrate().success(function(){
this.sequelize.getQueryInterface().describeTable('User').success(function(data) { this.sequelize.getQueryInterface().describeTable('User').success(function(data) {
var signature = data.filter(function(hash){ return hash.Field == 'signature' })[0] var signature = data.filter(function(hash){ return hash.attribute === 'signature' })[0]
, sig = data.filter(function(hash){ return hash.Field == 'sig' })[0] , sig = data.filter(function(hash){ return hash.attribute === 'sig' })[0]
expect(signature).toBeFalsy() expect(signature).toBeFalsy()
expect(sig).toBeTruthy() expect(sig).toBeTruthy()
......
...@@ -54,7 +54,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() { ...@@ -54,7 +54,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
}) })
}) })
describe('=>indexes', function() { describe('indexes', function() {
before(function(done) { before(function(done) {
this.interface.createTable('User', { this.interface.createTable('User', {
username: Helpers.Sequelize.STRING, username: Helpers.Sequelize.STRING,
...@@ -69,7 +69,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() { ...@@ -69,7 +69,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
this.interface.showIndex('User').complete(function(err, indexes) { this.interface.showIndex('User').complete(function(err, indexes) {
expect(err).toBeNull() expect(err).toBeNull()
var indexColumns = Helpers.Sequelize.Utils._.uniq(indexes.map(function(index) { return index.Key_name })) var indexColumns = Helpers.Sequelize.Utils._.uniq(indexes.map(function(index) { return index.name }))
expect(indexColumns).toEqual(['user_username_is_admin']) expect(indexColumns).toEqual(['user_username_is_admin'])
this.interface.removeIndex('User', ['username', 'isAdmin']).complete(function(err) { this.interface.removeIndex('User', ['username', 'isAdmin']).complete(function(err) {
...@@ -78,7 +78,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() { ...@@ -78,7 +78,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
this.interface.showIndex('User').complete(function(err, indexes) { this.interface.showIndex('User').complete(function(err, indexes) {
expect(err).toBeNull() expect(err).toBeNull()
indexColumns = Helpers.Sequelize.Utils._.uniq(indexes.map(function(index) { return index.Key_name })) indexColumns = Helpers.Sequelize.Utils._.uniq(indexes.map(function(index) { return index.name }))
expect(indexColumns).toEqual([]) expect(indexColumns).toEqual([])
done() done()
...@@ -90,6 +90,32 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() { ...@@ -90,6 +90,32 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
}) })
describe('describeTable', function() { describe('describeTable', function() {
before(function(done) {
this.interface.createTable('User', {
username: Helpers.Sequelize.STRING,
isAdmin: Helpers.Sequelize.BOOLEAN
}).success(done)
})
it('reads the metadata of the table', function(done) {
this.interface.describeTable('User').complete(function(err, metadata) {
expect(err).toBeNull()
var username = metadata.filter(function(m) { return m.attribute === 'username' })[0]
var isAdmin = metadata.filter(function(m) { return m.attribute === 'isAdmin' })[0]
expect(username.attribute).toEqual('username')
expect(username.type).toEqual(dialect === 'postgres' ? 'CHARACTER VARYING' : 'VARCHAR(255)')
expect(username.allowNull).toBeTrue()
expect(username.defaultValue).toBeNull()
expect(isAdmin.attribute).toEqual('isAdmin')
expect(isAdmin.type).toEqual(dialect === 'postgres' ? 'BOOLEAN' : 'TINYINT(1)')
expect(isAdmin.allowNull).toBeTrue()
expect(isAdmin.defaultValue).toBeNull()
done()
})
})
}) })
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!