Untitled

mail@pastecode.io avatar
unknown
javascript
10 days ago
5.4 kB
2
Indexable
Never
const allureWriter = require('@shelex/cypress-allure-plugin/writer');
const { Client } = require('pg');
const Vertica = require('vertica');
require('dotenv').config();

const sqlQueries = require('../fixtures/sql');

module.exports = (on, config) => {
    // Setup Allure plugin for reporting
    allureWriter(on, config);

    // Register custom tasks
    on('task', {
        async queryDatabase({ dbType, queryName }) {
            let client;
            let query;

            // Fetch the SQL query using the queryName
            if (typeof sqlQueries[queryName] === 'function') {
                query = sqlQueries[queryName]();
                console.log(`Retrieved query for ${queryName}:`, query);
            } else {
                throw new Error(`Query function ${queryName} not found in sqlQueries`);
            }

            try {
                // Configure the database client based on dbType
                if (dbType === 'postgres') {
                    console.log(`Configuring Postgres dbType: ${dbType}`);
                    client = new Client({
                        host: process.env.PGHOST1,
                        user: process.env.PGUSER1,
                        password: process.env.PGPASSWORD1,
                        database: process.env.PGDATABASE1,
                        port: process.env.PGPORT1,
                        ssl: false
                    });

                    // Connect to the database
                    await client.connect();
                } else if (dbType === 'vertica') {
                    console.log(`Configuring Vertica dbType: ${dbType}`);
                    client = new Vertica.Connection({
                        host: process.env.VTHOST,
                        user: process.env.VTUSER,
                        password: process.env.VTPASSWORD,
                        database: process.env.VTDATABASE,
                        port: process.env.VTPORT
                    });

                    // Connect to the database
                    await new Promise((resolve, reject) => {
                        client.connect((err) => {
                            if (err) {
                                console.error('Error connecting to Vertica:', err);
                                reject(err);
                            } else {
                                console.log('Connected to Vertica');
                                resolve();
                            }
                        });
                    });
                } else {
                    throw new Error(`Unsupported dbType: ${dbType}`);
                }

                console.log(`Connected to ${dbType} database`);
                console.log(`Executing query: ${query}`);

                // Execute the query
                const res = await new Promise((resolve, reject) => {
                    client.query(query, (err, result) => {
                        if (err) {
                            console.error('Error executing query:', err);
                            reject(err);
                        } else {
                            console.log('Query executed successfully:', result);
                            resolve(result);
                        }
                    });
                });

                console.log(`Query result: ${JSON.stringify(res.rows)}`);

                // End the connection
                if (dbType === 'postgres') {
                    await client.end();
                } else if (dbType === 'vertica') {
                    await new Promise((resolve, reject) => {
                        client.disconnect((err) => {
                            if (err) {
                                console.error('Error disconnecting from Vertica:', err);
                                reject(err);
                            } else {
                                console.log('Disconnected from Vertica');
                                resolve();
                            }
                        });
                    });
                }

                return res.rows;
            } catch (error) {
                console.error(`Error querying database with dbType: ${dbType}, queryName: ${queryName}`);
                console.error(`Detailed error: ${error.message}`);
                if (client) {
                    try {
                        if (dbType === 'postgres') {
                            await client.end();
                        } else if (dbType === 'vertica') {
                            await new Promise((resolve, reject) => {
                                client.disconnect((err) => {
                                    if (err) {
                                        console.error('Error during disconnect:', err);
                                        reject(err);
                                    } else {
                                        resolve();
                                    }
                                });
                            });
                        }
                    } catch (disconnectError) {
                        console.error('Error closing connection:', disconnectError);
                    }
                }
                throw new AggregateError([error], `Error querying database: ${error.message}`);
            }
        }
    });

    return config;
};
Leave a Comment