Skip to content

Commit

Permalink
Deshabilito primaryKey bug para cmd-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Oct 12, 2016
1 parent 8ba4a0c commit 48754d7
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 224 deletions.
38 changes: 26 additions & 12 deletions bin/txt-to-sql-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ program
.usage('[options] input.txt')
.option('-i, --input', 'Name of the input file')
.option('-p, --prepare', 'Analyzes input and generates input.yaml')
//.option('-f, --fast', 'Uses streams to process input')
.option('-f, --fast', 'Uses streams to process input')
//.option('-e, --export-defaults', 'Exports defaults to input-defaults.yaml')
.parse(process.argv);

Expand Down Expand Up @@ -58,10 +58,14 @@ function readConfigData(configFile) {

function doPrepare(params, inputYaml, create) {
// PARCHE hasta resolver #16
if(params.opts && params.opts.columns) {
params.opts.includePrimaryKey = params.opts.columns.filter(function(col) {
return col.inPrimaryKey === true;
}).length>0;
if(params.opts) {
if(params.opts.columns) {
params.opts.includePrimaryKey = params.opts.columns.filter(function(col) {
return col.inPrimaryKey === true;
}).length>0;
}
} else {
params.opts = { disablePrimaryKeyBug: true };
}
// fin PARCHE
var res;
Expand All @@ -86,16 +90,24 @@ function doPrepare(params, inputYaml, create) {
});
}

function doGenerate(params, inputName) {
function doGenerate(params, inputYaml, create, inputName) {
var outSQL = inputName+'.sql';
return txtToSql.generateScripts(params).then(function(result) {
return doPrepare(params, inputYaml, create).then(function(preparedParams) {
return txtToSql.generateScripts(preparedParams);
}).then(function(result) {
if(result.errors) { throw new Error(result.errors); }
return fs.writeFile(outSQL, result.rawSql);
}).then(function() {
process.stdout.write("Generated '"+outSQL+"'")
});
}

function doFast(params, inputName) {
return Promise.resolve().then(function() {
return "not yet";
});
}

var inputName = Path.basename(cmdParams.input, '.txt');
var params = {};
getOutputDir(cmdParams.input).then(function(dir) {
Expand All @@ -116,11 +128,13 @@ getOutputDir(cmdParams.input).then(function(dir) {
}
return fs.readFile(cmdParams.input);
}).then(function(rawInput) {
params.rawTable = rawInput;
return doPrepare(params, inputYaml, createInputYaml);
}).then(function(preparedParams) {
if(! cmdParams.prepare) {
return doGenerate(preparedParams, inputBase);
params.rawTable = rawInput;
if(cmdParams.fast) {
return doFast(params, inputName);
} else if (cmdParams.prepare) {
return doPrepare(params, inputYaml, createInputYaml);
} else {
return doGenerate(params, inputYaml, createInputYaml, inputBase);
}
}).catch(function(err){
process.stderr.write("ERROR\n"+err.stack);
Expand Down
6 changes: 3 additions & 3 deletions lib/txt-to-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function getEncodingSinc(buf) {
}

function getEncoding(buf) {
return Promise.resolve(buf).then(function(buf) {
return Promise.resolve().then(function() {
return getEncodingSinc(buf);
});
}
Expand Down Expand Up @@ -392,7 +392,7 @@ function determinePrimaryKey(info) {
columnsInKey.push(colIndex);
return false;
});
if(haveCustomKeys.length && columnsInKey.length===0) {
if(! info.opts.disablePrimaryKeyBug && haveCustomKeys.length && columnsInKey.length===0) {
throw new Error("includePrimaryKey is on but no columns were selected");
}
try{
Expand Down Expand Up @@ -422,7 +422,7 @@ function determinePrimaryKey(info) {
}catch(err){
if(err.message!=="haveNullColumns") { throw err; }
}
if(haveCustomKeys.length && (! info.primaryKey || ! info.primaryKey.length)) {
if(! info.opts.disablePrimaryKeyBug && haveCustomKeys.length && (! info.primaryKey || ! info.primaryKey.length)) {
var failingColumns = columnsInKey.map(function(col) {
return info.columnsInfo[col].name;
}).join(',');
Expand Down
Loading

0 comments on commit 48754d7

Please sign in to comment.