Merge branch 'test' of https://gitea.verdnatura.es/verdnatura/salix
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
commit
96f500d161
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE bucket
|
INTO TABLE `edi`.`bucket`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8, @col9, @col10, @col11, @col12)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8, @col9, @col10, @col11, @col12)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE bucket_type
|
INTO TABLE `edi`.`bucket_type`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE `feature`
|
INTO TABLE `edi`.`feature`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE genus
|
INTO TABLE `edi`.`genus`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE item
|
INTO TABLE `edi`.`item`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8, @col9, @col10, @col11, @col12)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8, @col9, @col10, @col11, @col12)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE `item_feature`
|
INTO TABLE `edi`.`item_feature`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE item_group
|
INTO TABLE `edi`.`item_group`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE plant
|
INTO TABLE `edi`.`plant`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8, @col9)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8, @col9)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE specie
|
INTO TABLE `edi`.`specie`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE edi.supplier
|
INTO TABLE `edi`.`supplier`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8, @col9, @col10, @col11, @col12, @col13, @col14, @col15, @col16, @col17, @col18, @col19, @col20)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8, @col9, @col10, @col11, @col12, @col13, @col14, @col15, @col16, @col17, @col18, @col19, @col20)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE `type`
|
INTO TABLE `edi`.`type`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
LOAD DATA LOCAL INFILE ?
|
LOAD DATA LOCAL INFILE ?
|
||||||
INTO TABLE `value`
|
INTO TABLE `edi`.`value`
|
||||||
FIELDS TERMINATED BY ';'
|
FIELDS TERMINATED BY ';'
|
||||||
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7)
|
LINES TERMINATED BY '\n' (@col1, @col2, @col3, @col4, @col5, @col6, @col7)
|
||||||
SET
|
SET
|
||||||
|
|
|
@ -22,6 +22,9 @@ module.exports = Self => {
|
||||||
const container = await models.TempContainer.container('edi');
|
const container = await models.TempContainer.container('edi');
|
||||||
const tempPath = path.join(container.client.root, container.name);
|
const tempPath = path.join(container.client.root, container.name);
|
||||||
|
|
||||||
|
// Temporary file clean
|
||||||
|
await fs.rmdir(`${tempPath}/*`, {recursive: true});
|
||||||
|
|
||||||
const [ftpConfig] = await Self.rawSql('SELECT host, user, password FROM edi.ftpConfig');
|
const [ftpConfig] = await Self.rawSql('SELECT host, user, password FROM edi.ftpConfig');
|
||||||
console.debug(`Openning FTP connection to ${ftpConfig.host}...\n`);
|
console.debug(`Openning FTP connection to ${ftpConfig.host}...\n`);
|
||||||
|
|
||||||
|
@ -48,6 +51,12 @@ module.exports = Self => {
|
||||||
tempDir = `${tempPath}/${fileName}`;
|
tempDir = `${tempPath}/${fileName}`;
|
||||||
tempFile = `${tempPath}/${fileName}.zip`;
|
tempFile = `${tempPath}/${fileName}.zip`;
|
||||||
|
|
||||||
|
// if (fs.existsSync(tempFile))
|
||||||
|
// await fs.unlink(tempFile);
|
||||||
|
|
||||||
|
// if (fs.existsSync(tempDir))
|
||||||
|
// await fs.rmdir(tempDir, {recursive: true});
|
||||||
|
|
||||||
await extractFile({
|
await extractFile({
|
||||||
ftpClient: ftpClient,
|
ftpClient: ftpClient,
|
||||||
file: file,
|
file: file,
|
||||||
|
@ -61,7 +70,6 @@ module.exports = Self => {
|
||||||
if (fs.existsSync(tempFile))
|
if (fs.existsSync(tempFile))
|
||||||
await fs.unlink(tempFile);
|
await fs.unlink(tempFile);
|
||||||
|
|
||||||
await fs.rmdir(tempDir, {recursive: true});
|
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,9 +94,6 @@ module.exports = Self => {
|
||||||
|
|
||||||
zip.extractAllTo(paths.tempDir, false);
|
zip.extractAllTo(paths.tempDir, false);
|
||||||
|
|
||||||
if (fs.existsSync(paths.tempFile))
|
|
||||||
await fs.unlink(paths.tempFile);
|
|
||||||
|
|
||||||
await dumpData({file, entries, paths});
|
await dumpData({file, entries, paths});
|
||||||
|
|
||||||
await fs.rmdir(paths.tempDir, {recursive: true});
|
await fs.rmdir(paths.tempDir, {recursive: true});
|
||||||
|
@ -99,57 +104,59 @@ module.exports = Self => {
|
||||||
const toTable = file.toTable;
|
const toTable = file.toTable;
|
||||||
const baseName = file.fileName;
|
const baseName = file.fileName;
|
||||||
|
|
||||||
for (const zipEntry of entries) {
|
const tx = await Self.beginTransaction({});
|
||||||
const entryName = zipEntry.entryName;
|
|
||||||
console.log(`Reading file ${entryName}...`);
|
|
||||||
|
|
||||||
const startIndex = (entryName.length - 10);
|
try {
|
||||||
const endIndex = (entryName.length - 4);
|
const options = {transaction: tx};
|
||||||
const dateString = entryName.substring(startIndex, endIndex);
|
|
||||||
const lastUpdated = new Date();
|
|
||||||
|
|
||||||
// Format string date to a date object
|
const tableName = `edi.${toTable}`;
|
||||||
let updated = null;
|
await Self.rawSql(`DELETE FROM ??`, [tableName], options);
|
||||||
if (file.updated) {
|
|
||||||
updated = new Date(file.updated);
|
|
||||||
updated.setHours(0, 0, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
lastUpdated.setFullYear(`20${dateString.substring(4, 6)}`);
|
for (const zipEntry of entries) {
|
||||||
lastUpdated.setMonth(parseInt(dateString.substring(2, 4)) - 1);
|
const entryName = zipEntry.entryName;
|
||||||
lastUpdated.setDate(dateString.substring(0, 2));
|
console.log(`Reading file ${entryName}...`);
|
||||||
lastUpdated.setHours(0, 0, 0, 0);
|
|
||||||
|
|
||||||
if (updated && lastUpdated <= updated) {
|
const startIndex = (entryName.length - 10);
|
||||||
console.debug(`Table ${toTable} already updated, skipping...`);
|
const endIndex = (entryName.length - 4);
|
||||||
continue;
|
const dateString = entryName.substring(startIndex, endIndex);
|
||||||
}
|
const lastUpdated = new Date();
|
||||||
|
|
||||||
console.log('Dumping data...');
|
// Format string date to a date object
|
||||||
const templatePath = path.join(__dirname, `./sql/${toTable}.sql`);
|
let updated = null;
|
||||||
const sqlTemplate = fs.readFileSync(templatePath, 'utf8');
|
if (file.updated) {
|
||||||
|
updated = new Date(file.updated);
|
||||||
|
updated.setHours(0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
const rawPath = path.join(paths.tempDir, entryName);
|
lastUpdated.setFullYear(`20${dateString.substring(4, 6)}`);
|
||||||
|
lastUpdated.setMonth(parseInt(dateString.substring(2, 4)) - 1);
|
||||||
|
lastUpdated.setDate(dateString.substring(0, 2));
|
||||||
|
lastUpdated.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
try {
|
if (updated && lastUpdated <= updated) {
|
||||||
const tx = await Self.beginTransaction({});
|
console.debug(`Table ${toTable} already updated, skipping...`);
|
||||||
const options = {transaction: tx};
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Dumping data...');
|
||||||
|
const templatePath = path.join(__dirname, `./sql/${toTable}.sql`);
|
||||||
|
const sqlTemplate = fs.readFileSync(templatePath, 'utf8');
|
||||||
|
|
||||||
|
const rawPath = path.join(paths.tempDir, entryName);
|
||||||
|
|
||||||
await Self.rawSql(`DELETE FROM edi.${toTable}`, null, options);
|
|
||||||
await Self.rawSql(sqlTemplate, [rawPath], options);
|
await Self.rawSql(sqlTemplate, [rawPath], options);
|
||||||
await Self.rawSql(`
|
await Self.rawSql(`
|
||||||
UPDATE edi.fileConfig
|
UPDATE edi.fileConfig
|
||||||
SET updated = ?
|
SET updated = ?
|
||||||
WHERE fileName = ?
|
WHERE fileName = ?
|
||||||
`, [lastUpdated, baseName], options);
|
`, [lastUpdated, baseName], options);
|
||||||
|
|
||||||
tx.commit();
|
|
||||||
} catch (error) {
|
|
||||||
tx.rollback();
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Updated table ${toTable}\n`);
|
tx.commit();
|
||||||
|
} catch (error) {
|
||||||
|
tx.rollback();
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
|
console.log(`Updated table ${toTable}\n`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue