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

Commit dbab5ae8 by Kevin Martin

Added a few tests, DRY'd out some PG ENUM code

1 parent 8c17e416
...@@ -36,6 +36,10 @@ function pgEscape(val) { ...@@ -36,6 +36,10 @@ function pgEscape(val) {
return "'"+val+"'"; return "'"+val+"'";
} }
function pgEnum(tableName, attr, dataType) {
return "CREATE TYPE " + Utils.escape("enum_" + tableName + "_" + attr) + " AS " + dataType.match(/^ENUM\(.+\)/)[0] + "; "
}
function padInt(i) { function padInt(i) {
return (i < 10) ? '0' + i.toString() : i.toString() return (i < 10) ? '0' + i.toString() : i.toString()
} }
...@@ -65,6 +69,10 @@ function pgDataTypeMapping(tableName, attr, dataType) { ...@@ -65,6 +69,10 @@ function pgDataTypeMapping(tableName, attr, dataType) {
tables[tableName][attr] = 'serial' tables[tableName][attr] = 'serial'
} }
if (dataType.match(/^ENUM\(/)) {
dataType = dataType.replace(/^ENUM\(.+\)/, Utils.escape("enum_" + tableName + "_" + attr))
}
return dataType return dataType
} }
...@@ -87,7 +95,7 @@ module.exports = (function() { ...@@ -87,7 +95,7 @@ module.exports = (function() {
attrStr.push(addQuotes(attr) + " " + dataType) attrStr.push(addQuotes(attr) + " " + dataType)
if (attributes[attr].match(/^ENUM\(/)) { if (attributes[attr].match(/^ENUM\(/)) {
query = "CREATE TYPE " + Utils.escape("enum_" + tableName + "_" + attr) + " AS " + attributes[attr].match(/^ENUM\(.+\)/)[0] + "; " + query query = pgEnum(tableName, attr, attributes[attr])) + query;
} }
} }
...@@ -137,7 +145,7 @@ module.exports = (function() { ...@@ -137,7 +145,7 @@ module.exports = (function() {
})) }))
if (definition.match(/^ENUM\(/)) { if (definition.match(/^ENUM\(/)) {
query = "CREATE TYPE " + Utils.escape("enum_" + tableName + "_" + attrName) + " AS " + definition.match(/^ENUM\(.+\)/)[0] + "; " + query query = pgEnum(tableName, attrName, definition) + query
} }
} }
...@@ -170,16 +178,17 @@ module.exports = (function() { ...@@ -170,16 +178,17 @@ module.exports = (function() {
}) })
} }
if (definition.match(/^ENUM\(/)) {
query = pgEnum(tableName, attributeName, definition) + query
definition = definition.replace(/^ENUM\(.+\)/, Utils.escape("enum_" + tableName + "_" + attributeName))
}
attrSql += Utils._.template(query)({ attrSql += Utils._.template(query)({
tableName: addQuotes(tableName), tableName: addQuotes(tableName),
query: addQuotes(attributeName) + ' TYPE ' + definition query: addQuotes(attributeName) + ' TYPE ' + definition
}) })
sql.push(attrSql) sql.push(attrSql)
if (definition.match(/^ENUM\(/)) {
query = "CREATE TYPE " + Utils.escape("enum_" + tableName + "_" + attributeName) + " AS " + definition.match(/^ENUM\(.+\)/)[0] + "; " + query
}
} }
return sql.join('') return sql.join('')
...@@ -194,10 +203,6 @@ module.exports = (function() { ...@@ -194,10 +203,6 @@ module.exports = (function() {
before: addQuotes(attrBefore), before: addQuotes(attrBefore),
after: addQuotes(attributeName), after: addQuotes(attributeName),
})) }))
if (attributes[attributeName].match(/^ENUM\(/)) {
query = "CREATE TYPE " + Utils.escape("enum_" + tableName + "_" + attributeName) + " AS " + attributes[attributeName].match(/^ENUM\(.+\)/)[0] + "; " + query
}
} }
return Utils._.template(query)({ tableName: addQuotes(tableName), attributes: attrString.join(', ') }) return Utils._.template(query)({ tableName: addQuotes(tableName), attributes: attrString.join(', ') })
......
...@@ -22,6 +22,10 @@ describe('QueryGenerator', function() { ...@@ -22,6 +22,10 @@ describe('QueryGenerator', function() {
{ {
arguments: ['myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}, {charset: 'latin1'}], arguments: ['myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}, {charset: 'latin1'}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=latin1;" expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=latin1;"
},
{
arguments: ['myTable', {title: 'ENUM("A", "B", "C")', name: 'VARCHAR(255)'}, {charset: 'latin1'}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` ENUM(\"A\", \"B\", \"C\"), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=latin1;"
} }
], ],
......
...@@ -23,7 +23,10 @@ describe('QueryGenerator', function() { ...@@ -23,7 +23,10 @@ describe('QueryGenerator', function() {
arguments: ['mySchema.myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}], arguments: ['mySchema.myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}],
expectation: "CREATE TABLE IF NOT EXISTS \"mySchema\".\"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));" expectation: "CREATE TABLE IF NOT EXISTS \"mySchema\".\"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));"
}, },
{
arguments: ['myTable', {title: 'ENUM("A", "B", "C")', name: 'VARCHAR(255)'}],
expectation: "CREATE TYPE \"enum_myTable_title\" AS ENUM(\"A\", \"B\", \"C\"); CREATE TABLE IF NOT EXISTS \"myTable\" (\"title\" \"enum_myTable_title\", \"name\" VARCHAR(255));"
}
], ],
dropTableQuery: [ dropTableQuery: [
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!