Skip to content

Instantly share code, notes, and snippets.

@hoonto
Created June 16, 2013 14:57
Show Gist options
  • Save hoonto/5792293 to your computer and use it in GitHub Desktop.
Save hoonto/5792293 to your computer and use it in GitHub Desktop.
Patch for sequelize that allows integration with Postgres-XC
diff -Naur '--exclude-from=../patches/exclude' sequelize_git/lib/dao-factory.js sequelize/lib/dao-factory.js
--- sequelize_git/lib/dao-factory.js 2013-02-21 14:48:41.968230261 +0000
+++ sequelize/lib/dao-factory.js 2013-02-21 16:08:29.181145551 +0000
@@ -61,6 +61,9 @@
if ((attributeName !== 'id') && (dataTypeString.indexOf('PRIMARY KEY') !== -1)) {
self.primaryKeys[attributeName] = dataTypeString
+ } //MLM: pgxc:
+ else if((attributeName == 'id') && (self.options.pgxc == true)){
+ self.primaryKeys[attributeName] = dataTypeString
}
})
diff -Naur '--exclude-from=../patches/exclude' sequelize_git/lib/data-types.js sequelize/lib/data-types.js
--- sequelize_git/lib/data-types.js 2013-02-21 14:48:41.968230261 +0000
+++ sequelize/lib/data-types.js 2013-02-21 16:22:25.655274165 +0000
@@ -7,5 +7,11 @@
BOOLEAN: 'TINYINT(1)',
FLOAT: 'FLOAT',
NOW: 'NOW',
- ENUM: 'ENUM'
+ ENUM: 'ENUM',
+ //MLM: Added for pgxc - bytea
+ BYTEA: 'BYTEA',
+ //MLM: Added for pgxc - serial8
+ SERIAL8: 'SERIAL8',
+ //MLM: Added for pgxc - char(16)
+ CHAR16: 'CHAR(16)'
}
diff -Naur '--exclude-from=../patches/exclude' sequelize_git/lib/dialects/postgres/connector-manager.js sequelize/lib/dialects/postgres/connector-manager.js
--- sequelize_git/lib/dialects/postgres/connector-manager.js 2013-02-21 14:48:41.969230250 +0000
+++ sequelize/lib/dialects/postgres/connector-manager.js 2013-02-21 16:53:29.106426151 +0000
@@ -58,14 +58,20 @@
this.isConnecting = true
this.isConnected = false
+ this.connectError = ''; //MLM: Added (may have been there originally)
- var uri = this.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(this.config)
+ //MLM: Keeping track of array of uri's, so assigining to this:
+ this.uri = this.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(this.config)
+ this.urii = 0; //MLM: Added
- var connectCallback = function(err, client) {
+ //MLM: self, not local
+ self.connectCallback = function(err, client) {
self.isConnecting = false
if (!!err) {
emitter.emit('error', err)
+ self.connectError = err; //MLM
+ self.tryConnect(); //MLM
} else if (client) {
client.query("SET TIME ZONE 'UTC'")
.on('end', function() {
@@ -77,14 +83,26 @@
}
}
- if (this.pooling) {
- // acquire client from pool
- this.pg.connect(uri, connectCallback)
- } else {
- //create one-off client
- this.client = new this.pg.Client(uri)
- this.client.connect(connectCallback)
+ //MLM: New method, this's are changed to self's
+ self.tryConnect = function() {
+ if(self.urii < self.uri.length){
+ console.log('Database attempt connect to node' + (self.urii + 1));
+ if (self.pooling) {
+ // acquire client from pool
+ self.pg.connect(self.uri[self.urii], self.connectCallback)
+ if(self.isConnected) self.urii = self.uri.length;
+ } else {
+ //create one-off client
+ self.client = new self.pg.Client(self.uri[self.urii])
+ self.client.connect(self.connectCallback)
+ if(self.isConnected) self.urii = self.uri.length;
+ }
+ self.urii++;
+ }else{
+ throw new Error(self.connectError);
+ }
}
+ this.tryConnect(); //MLM: Call it
return emitter
}
diff -Naur '--exclude-from=../patches/exclude' sequelize_git/lib/dialects/postgres/query-generator.js sequelize/lib/dialects/postgres/query-generator.js
--- sequelize_git/lib/dialects/postgres/query-generator.js 2013-02-21 14:48:41.969230250 +0000
+++ sequelize/lib/dialects/postgres/query-generator.js 2013-02-21 17:40:28.563475385 +0000
@@ -92,7 +92,18 @@
primaryKeys[tableName] = []
tables[tableName] = {}
- var query = "CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)"
+ //MLM: Changed for Postgres:
+ // options.plan: "distribute by replication"
+ // options.plan: "distribute by hash(column)"
+ // options.plan: "distribute by modulo(column)"
+ //default is to replicate: "distribute by replication;"
+ //console.log('options.plan == ' + options.plan);
+ //console.log('options.pgxc == ' + options.pgxc);
+
+ if(!options.pgxc) var plan = ""; //MLM
+ else var plan = " " + options.plan; //MLM
+ //MLM: Changed this to include plan:
+ var query = "CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)" + plan
, attrStr = []
for (var attr in attributes) {
@@ -214,7 +225,8 @@
},
selectQuery: function(tableName, options) {
- var query = "SELECT <%= attributes %> FROM <%= table %>"
+ //MLM: Added distinct:
+ var query = "SELECT " + ((options.distinct)? 'DISTINCT ':'') + "<%= attributes %> FROM <%= table %>",
, table = null
options = options || {}
@@ -316,6 +328,12 @@
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;"
, returning = []
+ //MLM: added for PGXC - does not support RETURNING yet:
+ if(this.options.pgxc){
+ query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>); SELECT * FROM <%= table %> where id=currval('<%=tablename %>_id_seq');"
+ }
+
+
Utils._.forEach(attrValueHash, function(value, key, hash) {
if (tables[tableName] && tables[tableName][key]) {
switch (tables[tableName][key]) {
@@ -328,6 +346,7 @@
});
var replacements = {
+ tablename: tableName, //MLM: Added
table: addQuotes(tableName),
attributes: Object.keys(attrValueHash).map(function(attr){return addQuotes(attr)}).join(","),
values: Utils._.values(attrValueHash).map(function(value){
@@ -344,12 +363,23 @@
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %> RETURNING *"
, values = []
+ //MLM: added for PGXC - does not support RETURNING yet:
+ if(this.options.pgxc){
+ var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>; SELECT * FROM <%= tablename %> WHERE <%= where %>;"
+ }
+
for (var key in attrValueHash) {
var value = attrValueHash[key]
- values.push(addQuotes(key) + "=" + pgEscape(value))
+ //MLM: Added for PGXC
+ // XC does not support update of a distribution key.
+ // So nothing ending in "id" can be updated.
+ if(this.options.pgxc && key.indexOf('id', key.length - 2) === -1){
+ values.push(addQuotes(key) + "=" + pgEscape(value))
+ }
}
var replacements = {
+ tablename: tableName, //MLM
table: addQuotes(tableName),
values: values.join(","),
where: QueryGenerator.getWhereConditions(where)
@@ -374,6 +404,7 @@
}
var replacements = {
+ tablename: tableName, //MLM
table: addQuotes(tableName),
where: QueryGenerator.getWhereConditions(where),
limit: pgEscape(options.limit),
@@ -562,7 +593,23 @@
},
databaseConnectionUri: function(config) {
+ //MLM: Changes this for hosts separated by ','
var template = '<%= protocol %>://<%= user %>:<%= password %>@<%= host %><% if(port) { %>:<%= port %><% } %>/<%= database %>';
+ if(config.pgxc){
+ var hosts = config.host.split(',');
+ var uris = [];
+ for(var i=0; i < hosts.length; i++){
+ uris[i] = Utils._.template(template)({
+ user: config.username,
+ password: config.password,
+ database: config.database,
+ host: hosts[i],
+ port: config.port,
+ protocol: config.protocol
+ })
+ }
+ return uris;
+ }else{
return Utils._.template(template)({
user: encodeURIComponent(config.username),
@@ -572,6 +619,7 @@
port: config.port,
protocol: config.protocol
})
+ }
}
}
diff -Naur '--exclude-from=../patches/exclude' sequelize_git/lib/sequelize.js sequelize/lib/sequelize.js
--- sequelize_git/lib/sequelize.js 2013-02-21 14:48:41.971230229 +0000
+++ sequelize/lib/sequelize.js 2013-02-21 17:51:23.658549997 +0000
@@ -56,6 +56,8 @@
port : this.options.port,
pool : this.options.pool,
protocol: this.options.protocol,
+ //MLM: Added for pgxc:
+ pgxc : (typeof this.options.pgxc != 'undefined' && this.options.pgxc) ? true : false,
queue : this.options.queue,
native : this.options.native,
replication: this.options.replication,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment