From 059a82abbe42e955fc20bc96a96ab6fa22bc05db Mon Sep 17 00:00:00 2001 From: Olojakpoke Daniel Date: Wed, 25 May 2022 15:49:26 +0100 Subject: [PATCH 1/2] fix: add seed --- api-tests/seed.spec.js | 2 +- database/seed-cli.js | 42 ++++++++------ database/seeds/00_job_database_cleaner.js | 5 ++ database/seeds/01_earnings.js | 67 ++++++++++++++++++++++ database/seeds/data/funders.js | 13 +++++ database/seeds/data/growers.js | 34 +++++++++++ database/{seed/index.js => single-seed.js} | 2 +- knexfile.js | 39 +++++++++++++ package.json | 4 +- 9 files changed, 187 insertions(+), 21 deletions(-) create mode 100644 database/seeds/00_job_database_cleaner.js create mode 100644 database/seeds/01_earnings.js create mode 100644 database/seeds/data/funders.js create mode 100644 database/seeds/data/growers.js rename database/{seed/index.js => single-seed.js} (93%) create mode 100644 knexfile.js diff --git a/api-tests/seed.spec.js b/api-tests/seed.spec.js index ea6a4d4..f84c93e 100644 --- a/api-tests/seed.spec.js +++ b/api-tests/seed.spec.js @@ -2,7 +2,7 @@ require('dotenv').config(); const { expect } = require('chai'); const log = require('loglevel'); const knex = require('../server/infra/database/knex'); -const seed = require('../database/seed/index'); +const seed = require('../database/single-seed'); describe('seed', () => { beforeEach(async () => { diff --git a/database/seed-cli.js b/database/seed-cli.js index 27a63ec..f3132c5 100644 --- a/database/seed-cli.js +++ b/database/seed-cli.js @@ -1,6 +1,6 @@ -const log = require("loglevel"); +const log = require('loglevel'); const { Command } = require('commander'); -const seed = require('./seed/index'); +const seed = require('./single-seed'); const program = new Command(); @@ -9,32 +9,38 @@ program .description('CLI to seed data into DB for testing') .version('0.1.0'); -program.command('earnings') +program + .command('earnings') .description('Seed an earings record into earning db') // .argument('', 'string to split') .requiredOption('-f, --funder_id ', 'set up the funder id') .requiredOption('-g, --grower_id ', 'set up the grower id') .requiredOption('-c, --captures_count ', 'the number of captures') - .requiredOption('-s, --sub_organization ', 'the id of sub organization') + .requiredOption( + '-s, --sub_organization ', + 'the id of sub organization', + ) .action((options) => { // const limit = options.first ? 1 : undefined; // console.log(str.split(options.separator, limit)); - log.warn("seeding...", options); - seed.seed( - options.funder_id, - options.grower_id, - options.captures_count, - options.sub_organization, - ) + log.warn('seeding...', options); + seed + .seed( + options.funder_id, + options.grower_id, + options.captures_count, + options.sub_organization, + ) .then(() => { - log.warn("done!"); + log.warn('done!'); process.exit(0); - }).catch(e => { - log.error("error", e); - log.warn("seed failed!"); - process.exit(1); }) - log.warn("executed..."); + .catch((e) => { + log.error('error', e); + log.warn('seed failed!'); + process.exit(1); + }); + log.warn('executed...'); }); -program.parse(); \ No newline at end of file +program.parse(); diff --git a/database/seeds/00_job_database_cleaner.js b/database/seeds/00_job_database_cleaner.js new file mode 100644 index 0000000..bb2da40 --- /dev/null +++ b/database/seeds/00_job_database_cleaner.js @@ -0,0 +1,5 @@ +exports.seed = async function (knex) { + await knex.raw(` + DELETE FROM earnings; + `); +}; diff --git a/database/seeds/01_earnings.js b/database/seeds/01_earnings.js new file mode 100644 index 0000000..11cdf66 --- /dev/null +++ b/database/seeds/01_earnings.js @@ -0,0 +1,67 @@ +const { v4: uuid } = require('uuid'); +const growers = require('./data/growers'); +const funders = require('./data/funders'); + +const randomIntFromInterval = (min, max) => { + return Math.floor(Math.random() * (max - min + 1) + min); +}; +const maxPayout = 12000; + +exports.seed = async function (knex) { + for (let i = 0; i < 10; i += 1) { + const grower_id = growers.pop().id; + + const captures_count1 = randomIntFromInterval(400, 1100); + + let multiplier1 = (captures_count1 - (captures_count1 % 100)) / 10 / 100; + if (multiplier1 > 1) { + multiplier1 = 1; + } + const amount1 = multiplier1 * maxPayout; + + const captures_count2 = randomIntFromInterval(400, 1100); + + let multiplier2 = (captures_count2 - (captures_count2 % 100)) / 10 / 100; + if (multiplier2 > 1) { + multiplier2 = 1; + } + const amount2 = multiplier2 * maxPayout; + + const funder_id = funders[randomIntFromInterval(0, 2)].id; + const consolidation_rule_id = uuid(); + + await knex('earnings').insert({ + worker_id: grower_id, + amount: amount1, + payment_confirmation_id: null, + payment_method: null, + currency: 'USD', + status: 'calculated', + paid_at: null, + contract_id: '483a1f4e-0c52-4b53-b917-5ff4311ded26', + funder_id, + calculated_at: '02/24/2022', + consolidation_rule_id, + consolidation_period_start: '01/25/2022', + consolidation_period_end: '02/25/2022', + captures_count: captures_count1, + }); + + await knex('earnings').insert({ + worker_id: grower_id, + amount: amount2, + payment_confirmation_id: null, + payment_method: null, + currency: 'USD', + status: 'calculated', + paid_at: null, + contract_id: '483a1f4e-0c52-4b53-b917-5ff4311ded26', + funder_id, + calculated_at: '02/24/2022', + consolidation_rule_id, + consolidation_period_start: '02/25/2022', + consolidation_period_end: '03/25/2022', + captures_count: captures_count2, + }); + } +}; diff --git a/database/seeds/data/funders.js b/database/seeds/data/funders.js new file mode 100644 index 0000000..b757e71 --- /dev/null +++ b/database/seeds/data/funders.js @@ -0,0 +1,13 @@ +const funders = [ + { + id: '792a4eee-8e18-4750-a56f-91bdec383aa6', + }, + { + id: 'b683313a-f082-4829-9127-59a176be916d', + }, + { + id: 'c92189d2-2d55-44bc-a0b4-0dad25dc9f35', + }, +]; + +module.exports = funders; diff --git a/database/seeds/data/growers.js b/database/seeds/data/growers.js new file mode 100644 index 0000000..e8b3047 --- /dev/null +++ b/database/seeds/data/growers.js @@ -0,0 +1,34 @@ +const growers = [ + { + id: '35a23de8-f1ab-4409-be79-3c6a158d5bde', + }, + { + id: '90eef52b-ad55-4953-ace9-6a324ae6cec2', + }, + { + id: '9ae7337d-f3eb-4151-935e-51138ccdc811', + }, + { + id: 'da01db0e-8686-4aaa-833f-eaf8aa85f664', + }, + { + id: '03f8502b-01f9-44a8-b072-1c4638d867c5', + }, + { + id: 'c3d17aa9-b45d-4e62-939d-bf33cf3ef3ed', + }, + { + id: 'a3896425-4263-4f3b-af48-44f7ac07b2f2', + }, + { + id: 'b9ce5741-38f1-4616-9792-dbb9990b0d20', + }, + { + id: 'd7429ba9-7e64-4e84-a774-5b8ea7b4f433', + }, + { + id: 'ed538c0f-73d0-49eb-8410-25efe94b421b', + }, +]; + +module.exports = growers; diff --git a/database/seed/index.js b/database/single-seed.js similarity index 93% rename from database/seed/index.js rename to database/single-seed.js index b7c4ea6..b37a5d1 100644 --- a/database/seed/index.js +++ b/database/single-seed.js @@ -1,6 +1,6 @@ const log = require('loglevel'); const { v4: uuid } = require('uuid'); -const knex = require('../../server/infra/database/knex'); +const knex = require('../server/infra/database/knex'); exports.seed = async ( funder_id, diff --git a/knexfile.js b/knexfile.js new file mode 100644 index 0000000..53cb717 --- /dev/null +++ b/knexfile.js @@ -0,0 +1,39 @@ +require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }); +const path = require('path'); + +const connection = process.env.DATABASE_URL_SEEDER; + +const postgresPattern = /^postgresql:\//; + +if (!postgresPattern.test(connection)) { + throw new Error('invalid database connection url received'); +} + +module.exports = { + development: { + client: 'pg', + connection, + searchPath: [process.env.DATABASE_SCHEMA, 'public'], + pool: { + min: 1, + max: 100, + }, + seeds: { + directory: path.join(__dirname, 'database', 'seeds'), + }, + debug: process.env.NODE_LOG_LEVEL === 'debug', + }, + test: { + client: 'pg', + connection, + searchPath: [process.env.DATABASE_SCHEMA, 'public'], + pool: { + min: 1, + max: 100, + }, + seeds: { + directory: path.join(__dirname, 'database', 'seeds'), + }, + debug: process.env.NODE_LOG_LEVEL === 'debug', + }, +}; diff --git a/package.json b/package.json index 555aac2..40477ac 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,9 @@ "db-migrate-ci": "cd database; db-migrate up", "prepare": "husky install", "db-migrate-dev": "cd database; echo \"Migrating dev\"; db-migrate --env dev up; echo \"Migrating integration\"; db-migrate --env integration up", - "seed-cli": "node ./database/seed-cli.js" + "seed-cli": "node ./database/seed-cli.js", + "seed-dev": "NODE_ENV=development knex seed:run", + "seed-test": "NODE_ENV=test knex seed:run" }, "keywords": [ "ecology" From 3da2309ed670ffad6665661465e5b53c5352d452 Mon Sep 17 00:00:00 2001 From: Olojakpoke Daniel Date: Mon, 11 Jul 2022 23:31:37 +0100 Subject: [PATCH 2/2] fix: update max pool --- knexfile.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/knexfile.js b/knexfile.js index 53cb717..29669fd 100644 --- a/knexfile.js +++ b/knexfile.js @@ -16,7 +16,7 @@ module.exports = { searchPath: [process.env.DATABASE_SCHEMA, 'public'], pool: { min: 1, - max: 100, + max: 10, }, seeds: { directory: path.join(__dirname, 'database', 'seeds'), @@ -29,7 +29,7 @@ module.exports = { searchPath: [process.env.DATABASE_SCHEMA, 'public'], pool: { min: 1, - max: 100, + max: 10, }, seeds: { directory: path.join(__dirname, 'database', 'seeds'),