Skip to content

Commit

Permalink
feat(audit-service): fixed package version in lock file, added migrat…
Browse files Browse the repository at this point in the history
…ions

MIGRATION CHANGE:
migration-20210423140543- added migrations for init schema

gh-0
  • Loading branch information
akshatdubeysf committed May 1, 2021
1 parent 7a332d8 commit c8e9540
Show file tree
Hide file tree
Showing 11 changed files with 7,263 additions and 9,108 deletions.
2,048 changes: 689 additions & 1,359 deletions sandbox/audit-ms-example/package-lock.json

Large diffs are not rendered by default.

13,675 changes: 5,942 additions & 7,733 deletions sandbox/workflow-ms-example/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion services/audit-service/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
dist/
coverage/

migrations/
migration.js
.eslintrc.js
Empty file.
22 changes: 22 additions & 0 deletions services/audit-service/database.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"defaultEnv": "master",
"master": {
"driver": "pg",
"host": {
"ENV": "DB_HOST"
},
"port": {
"ENV": "DB_PORT"
},
"user": {
"ENV": "DB_USER"
},
"password": {
"ENV": "DB_PASSWORD"
},
"database": {
"ENV": "DB_DATABASE"
}
},
"sql-file": true
}
72 changes: 72 additions & 0 deletions services/audit-service/migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const dotenv = require('dotenv');
const dotenvExt = require('dotenv-extended');
const fs = require('fs');
const DBMigrate = require('db-migrate');
const path = require('path');
let isLocal = false;
dotenv.config({path: path.join(process.env.INIT_CWD, '.env')});

try {
if (fs.existsSync('.infolder')) {
isLocal = true;
}
} catch (err) {
console.info('\n');
}
if (
isLocal ||
process.env.AUDIT_MIGRATION_SKIP ||
process.env.SOURCELOOP_MIGRATION_SKIP
) {
console.info(`Skipping migrations`);
} else {
dotenvExt.load({
schema: path.join(`.`, `migrations`, `.env.schema`),
path: path.join(process.env.INIT_CWD, '.env'),
errorOnMissing: true,
includeProcessEnv: true,
});
const dbmigrate = DBMigrate.getInstance(true);
dbmigrate.up();
}

if (process.env.SOURCELOOP_MIGRATION_COPY || process.env.AUDIT_MIGRATION_COPY) {
copyFileSync(path.join('.', 'database.json'), process.env.INIT_CWD);
copyFolderRecursiveSync(path.join('.', '/migrations'), process.env.INIT_CWD);
}

function copyFileSync(source, target) {
var targetFile = target;

// If target is a directory, a new file with the same name will be created
if (fs.existsSync(target)) {
if (fs.lstatSync(target).isDirectory()) {
targetFile = path.join(target, path.basename(source));
}
}

fs.writeFileSync(targetFile, fs.readFileSync(source));
}

function copyFolderRecursiveSync(source, target) {
var files = [];

// Check if folder needs to be created or integrated
var targetFolder = path.join(target, path.basename(source));
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}

// Copy
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach(function (file) {
var curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderRecursiveSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
}
50 changes: 50 additions & 0 deletions services/audit-service/migrations/20210423140543-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/

function handleFile(filePath, resolve, reject) {
fs.readFile(filePath, {encoding: 'utf-8'}, function (err, data) {
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
}
exports.setup = function (options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function (db) {
var filePath = path.join(__dirname, 'sqls', '20210423140543-init-up.sql');
return new Promise(function (resolve, reject) {
handleFile(filePath, resolve, reject);
}).then(function (data) {
return db.runSql(data);
});
};

exports.down = function (db) {
var filePath = path.join(__dirname, 'sqls', '20210423140543-init-down.sql');
return new Promise(function (resolve, reject) {
handleFile(filePath, resolve, reject);
}).then(function (data) {
return db.runSql(data);
});
};

exports._meta = {
version: 1,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP SCHEMA IF EXISTS main CASCADE;
27 changes: 27 additions & 0 deletions services/audit-service/migrations/sqls/20210423140543-init-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
DROP SCHEMA IF EXISTS main CASCADE;
CREATE SCHEMA main;

SET search_path TO main,public;
GRANT ALL ON SCHEMA main TO public;

CREATE TABLE main.audit_logs (
id uuid DEFAULT md5(random()::text || clock_timestamp()::text)::uuid NOT NULL ,
"action" text NOT NULL,
acted_at timestamptz NOT NULL,
acted_on text NULL,
action_key text NOT NULL,
entity_id text NOT NULL,
actor text NOT NULL,
"before" text NULL,
"after" text NULL,
action_group text NULL,
CONSTRAINT audit_logs_pkey PRIMARY KEY (id)
);

CREATE TABLE main.todo (
id uuid DEFAULT md5(random()::text || clock_timestamp()::text)::uuid NOT NULL ,
title text NOT NULL,
description text NOT NULL,
items text NULL,
CONSTRAINT todo_pkey PRIMARY KEY (id)
);
Loading

0 comments on commit c8e9540

Please sign in to comment.