Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmerz committed Jan 27, 2025
1 parent ea49880 commit d037c25
Show file tree
Hide file tree
Showing 8 changed files with 430 additions and 247 deletions.
73 changes: 38 additions & 35 deletions cmd/run/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,47 @@ checkRequired(program);
let pbar;

(async function() {
let files = program.file.split(',');

let pgOptions = getPgOptions(program);
await pg.connect(pgOptions);

await model.loadUids();

pbar = new cliProgress.Bar({etaBuffer: 50}, cliProgress.Presets.shades_classic);

model.on('insert-start', e => {
pbar.start(e.total, 0)
});
model.on('insert-update', (e) => pbar.update(e.current));


for( let file of files ) {
if( !file ) continue;
try {
let filepath = resolveFilePath(file);
let filename = model.checkAndGetFilename(filepath);

let sheet = await csv.getData(filepath);
let data = sheet.records;

console.log(`\nInserting ${data.length} rows into ${program.table} from source: ${source.getSourceName(filename, program.sheet)}`);

await model.insert(filename, program.sheet, program.table, data, {revision: sheet.revision});

pbar.stop();
} catch(e) {
console.log('');
printError(e);
try {
let files = program.file.split(',');
let pgOptions = getPgOptions(program);

await pg.connect(pgOptions);
await model.loadUids();

pbar = new cliProgress.Bar({etaBuffer: 50}, cliProgress.Presets.shades_classic);

model.on('insert-start', e => {
pbar.start(e.total, 0)
});
model.on('insert-update', (e) => pbar.update(e.current));


for( let file of files ) {
if( !file ) continue;
try {
let filepath = resolveFilePath(file);
let filename = model.checkAndGetFilename(filepath);

let sheet = await csv.getData(filepath);
let data = sheet.records;

console.log(`\nInserting ${data.length} rows into ${program.table} from source: ${source.getSourceName(filename, program.sheet)}`);

await model.insert(filename, program.sheet, program.table, data, {revision: sheet.revision});

pbar.stop();
} catch(e) {
console.log('');
printError(e);
}
}
}

try {

await pg.client.end();
} catch(e) {}
} catch(e) {
console.log('');
printError(e);
}

process.exit();
})()
2 changes: 2 additions & 0 deletions cmd/utils/wrapPgOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ module.exports = cmd => {
.option('-d, --pg-database [database]', 'Name of PG database to use')
.option('-P, --pg-port [port]', 'PG port to use')
.option('-l, --pg-sslmode', 'Set sslmode connection to PG. \'disable\' or \'require\' supported')
.option('--pgdm-source-table [table]', 'Name of table to store source file information')
.option('--pgdm-list-table [table]', 'Name of table to store list of tables available to pgdm')
}
14 changes: 7 additions & 7 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ class Model extends EventEmitter {
let dInfo = this._getDeleteInfo(s);
await pg.query(dInfo.stmt, [dInfo.value]);

let stmt = `DELETE from source where source_id = $1`;
await pg.query(stmt, [s.source_id]);
let stmt = `DELETE from pgdm_source where pgdm_source_id = $1`;
await pg.query(stmt, [s.pgdm_source_id]);

await pg.query('COMMIT');
} catch(e) {
Expand All @@ -448,8 +448,8 @@ class Model extends EventEmitter {
_getDeleteInfo(s) {
// delete from table config
let table = config.tables[s.table_view];
let field = 'source_id';
let value = s.source_id;
let field = 'pgdm_source_id';
let value = s.pgdm_source_id;

// delete from view config
if( config.deleteFromView[s.table_view] ) {
Expand Down Expand Up @@ -500,7 +500,7 @@ class Model extends EventEmitter {
}

// UPDATE source revision
await pg.query('UPDATE source SET revision = revision + 1 WHERE source_id = $1', [s.soure_id]);
await pg.query('UPDATE source SET revision = revision + 1 WHERE pgdm_source_id = $1', [s.pgdm_source_id]);

await pg.query('COMMIT');
} catch(e) {
Expand Down Expand Up @@ -529,8 +529,8 @@ class Model extends EventEmitter {

let table = config.tables[s.table_view];

let stmt = `select count(*) as count from ${table} where source_id = $1`;
let rows = await pg.query(stmt, [s.source_id]);
let stmt = `select count(*) as count from ${table} where pgdm_source_id = $1`;
let rows = await pg.query(stmt, [s.pgdm_source_id]);
return {
table : s.table_view,
count : rows[0].count
Expand Down
21 changes: 19 additions & 2 deletions lib/pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const fs = require('fs');
const os = require('os');
const clone = require('clone');
const gconfig = require('./config');

class PG {

Expand All @@ -14,24 +15,40 @@ class PG {

async connect(config={}) {
if( config.service ) {
config = Object.assign(config, this.getConfig(config.service));
config = Object.assign({}, this.getConfig(config.service), config);
} else if( !config.ignoreServiceEnv && process.env.PGSERVICE && this.getConfig(process.env.PGSERVICE)) {
config.service = process.env.PGSERVICE;
config = Object.assign({}, this.getConfig(config.service), config);
}
if( !config.pgdm ) config.pgdm = {};

if( config.dmsourcetable ) {
config.pgdm.source = config.dmsourcetable;
gconfig.TABLES.SOURCE = config.dmsourcetable;
delete config.dmsourcetable;
}
if( config.dmlisttable ) {
config.pgdm.table = config.dmlisttable;
gconfig.TABLES.PK = config.dmlisttable;
delete config.dmlisttable;
}

this.client = new Client({
user: config.user,
host: config.host,
database: config.dbname,
database: config.dbname || config.database,
password: config.password || '',
port: parseInt(config.port) || 5432,
ssl : config.sslmode === 'require' ? true : false
});

await this.client.connect();

// TODO: figure out how to handle notices
// this.client.on('notice', (notice) => {
// console.log('NOTICE:', notice.message);
// });

if( config.schema || config.pgdm.schema ) {
await this.client.query(`SET search_path TO ${config.schema || config.pgdm.schema},public`);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/source.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const pg = require('./pg');
const Cursor = require('pg-cursor');
const path = require('path');
const config = require('./config');

class SourceController {

Expand All @@ -12,7 +13,7 @@ class SourceController {

getSource(filename, sheetname) {
let name = this.getSourceName(filename, sheetname);
return pg.querySingle(`select * from source where name = $1`, [name]);
return pg.querySingle(`select * from ${config.TABLES.SOURCE} where name = $1`, [name]);
}

/**
Expand All @@ -25,7 +26,7 @@ class SourceController {
// else revision += 1;

return pg.query(
'INSERT INTO source (name, table_view, revision) values ($1, $2, $3)',
'INSERT INTO '+config.TABLES.SOURCE+' (name, table_view, revision) values ($1, $2, $3)',
[this.getSourceName(filename, sheetname), table, revision]
);
}
Expand Down
Loading

0 comments on commit d037c25

Please sign in to comment.