Skip to content

Commit

Permalink
fix(authentication-service): fixed auto-migration issue in all servic…
Browse files Browse the repository at this point in the history
…es (#184)

MIGRATION CHANGE:
migration-20210423140543- removed main schema deletion
migration-20210318100600- removed main schema deletion
migration-20210416135254- removed main schema deletion
migration-20200805162829- remove main schema deletion
migration-20210501132806- removed main schema deletion
migration-20200520222139- removed main schema deletion
  • Loading branch information
akshatdubeysf authored May 5, 2021
1 parent 6d77c9a commit dd7bea2
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 75 deletions.
2 changes: 1 addition & 1 deletion services/audit-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class AuditDbDataSource extends juggler.DataSource

### Migrations

The migrations required for this service are processed during the installation automatically, if you plan to use the [Loopback Database Migrations] (https://loopback.io/doc/en/lb4/Database-migrations.html) or your own custom migrations, you can disable the auto-migration script by setting the `AUDIT_MIGRATION_SKIP` or `SOURCELOOP_MIGRATION_SKIP` env variable to true before installing the package. If you need the migration files in your project root, use the `AUDIT_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variable.
The migrations required for this service are processed during the installation automatically if you set the `AUDIT_MIGRATION` or `SOURCELOOP_MIGRATION` env variable. Please note that if you are using some pre-existing migrations or database, they may be effected. In such scenario, it is advised that you copy the migration files in your project root, using the `AUDIT_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variables. You can customize or cherry-pick the migrations in the copied files according to your specific requirements and then apply them to the DB.

### API Documentation

Expand Down
23 changes: 14 additions & 9 deletions services/audit-service/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const DBMigrate = require('db-migrate');
const path = require('path');
let isLocal = false;
dotenv.config({path: path.join(process.env.INIT_CWD, '.env')});
const type = 'AUDIT';

try {
if (fs.existsSync('.infolder')) {
Expand All @@ -13,15 +14,17 @@ try {
} catch (err) {
console.info('\n');
}
if (
isLocal ||
process.env.AUDIT_MIGRATION_SKIP ||
process.env.SOURCELOOP_MIGRATION_SKIP
) {
if (isLocal) {
console.info(`Skipping migrations`);
} else if (
!(process.env[`${type}_MIGRATION`] || process.env.SOURCELOOP_MIGRATION)
) {
console.warn(
`${type}_MIGRATION or SOURCELOOP_MIGRATION variables not found in the environment, skipping automigration.`,
);
} else {
dotenvExt.load({
schema: path.join(`.`, `migrations`, `.env.schema`),
schema: path.join('.', 'migrations', '.env.schema'),
path: path.join(process.env.INIT_CWD, '.env'),
errorOnMissing: true,
includeProcessEnv: true,
Expand All @@ -30,9 +33,11 @@ if (
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);
if (
process.env.SOURCELOOP_MIGRATION_COPY ||
process.env[`${type}_MIGRATION_COPY`]
) {
copyFolderRecursiveSync(path.join('.', 'migrations'), process.env.INIT_CWD);
}

function copyFileSync(source, target) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
DROP SCHEMA IF EXISTS main CASCADE;
CREATE SCHEMA main;

SET search_path TO main,public;
Expand Down
2 changes: 1 addition & 1 deletion services/authentication-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class AuthenticationDbDataSource

### Migrations

The migrations required for this service are processed during the installation automatically, if you plan to use the [Loopback Database Migrations] (https://loopback.io/doc/en/lb4/Database-migrations.html) or your own custom migrations, you can disable the auto-migration script by setting the `AUTH_MIGRATION_SKIP` or `SOURCELOOP_MIGRATION_SKIP` env variable to true before installing the package. If you need the migration files in your project root, use the `AUTH_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variable.
The migrations required for this service are processed during the installation automatically if you set the `AUTH_MIGRATION` or `SOURCELOOP_MIGRATION` env variable. Please note that if you are using some pre-existing migrations or database, they may be effected. In such scenario, it is advised that you copy the migration files in your project root, using the `AUTH_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variables. You can customize or cherry-pick the migrations in the copied files according to your specific requirements and then apply them to the DB.

### API Documentation

Expand Down
27 changes: 16 additions & 11 deletions services/authentication-service/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const fs = require('fs');
const DBMigrate = require('db-migrate');
const path = require('path');
let isLocal = false;
dotenv.config({path: `${process.env.INIT_CWD}/.env`});
dotenv.config({path: path.join(process.env.INIT_CWD, '.env')});
const type = 'AUTH';

try {
if (fs.existsSync('.infolder')) {
Expand All @@ -13,26 +14,30 @@ try {
} catch (err) {
console.info('\n');
}
if (
isLocal ||
process.env.AUTH_MIGRATION_SKIP ||
process.env.SOURCELOOP_MIGRATION_SKIP
) {
if (isLocal) {
console.info(`Skipping migrations`);
} else if (
!(process.env[`${type}_MIGRATION`] || process.env.SOURCELOOP_MIGRATION)
) {
console.warn(
`${type}_MIGRATION or SOURCELOOP_MIGRATION variables not found in the environment, skipping automigration.`,
);
} else {
dotenvExt.load({
schema: `./migrations/.env.schema`,
path: `${process.env.INIT_CWD}/.env`,
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.AUTH_MIGRATION_COPY) {
copyFileSync('./database.json', process.env.INIT_CWD);
copyFolderRecursiveSync('./migrations', process.env.INIT_CWD);
if (
process.env.SOURCELOOP_MIGRATION_COPY ||
process.env[`${type}_MIGRATION_COPY`]
) {
copyFolderRecursiveSync(path.join('.', 'migrations'), process.env.INIT_CWD);
}

function copyFileSync(source, target) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
DROP SCHEMA IF EXISTS main CASCADE;
CREATE SCHEMA main;

SET search_path TO main,public;
Expand Down
2 changes: 1 addition & 1 deletion services/bpmn-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Your workers are automatically initiated once a workflow is executed, to provide

### Migrations

The migrations required for this service are processed during the installation automatically, if you plan to use the [Loopback Database Migrations] (https://loopback.io/doc/en/lb4/Database-migrations.html) or your own custom migrations, you can disable the auto-migration script by setting the `WORKFLOW_MIGRATION_SKIP` or `SOURCELOOP_MIGRATION_SKIP` env variable to true before installing the package. If you need the migration files in your project root, use the `WORKFLOW_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variable.
The migrations required for this service are processed during the installation automatically if you set the `WORKFLOW_MIGRATION` or `SOURCELOOP_MIGRATION` env variable. Please note that if you are using some pre-existing migrations or database, they may be effected. In such scenario, it is advised that you copy the migration files in your project root, using the `WORKFLOW_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variables. You can customize or cherry-pick the migrations in the copied files according to your specific requirements and then apply them to the DB.

This project includes no migrations to seed your BPMN engine. If you are using Camunda BPM Run, you can use either the `resources` folder to seed a model, or you can config it to use a custom DB where you can seed your data. The steps to config Platform Run are given [here](https://camunda.com/blog/2020/03/introducing-camunda-bpm-run/).

Expand Down
24 changes: 13 additions & 11 deletions services/bpmn-service/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const fs = require('fs');
const DBMigrate = require('db-migrate');
const path = require('path');
let isLocal = false;
dotenv.config({path: `${process.env.INIT_CWD}/.env`});
dotenv.config({path: path.join(process.env.INIT_CWD, '.env')});
const type = 'WORKFLOW';

try {
if (fs.existsSync('.infolder')) {
Expand All @@ -13,16 +14,18 @@ try {
} catch (err) {
console.info('\n');
}
if (
isLocal ||
process.env.WORKFLOW_MIGRATION_SKIP ||
process.env.SOURCELOOP_MIGRATION_SKIP
) {
if (isLocal) {
console.info(`Skipping migrations`);
} else if (
!(process.env[`${type}_MIGRATION`] || process.env.SOURCELOOP_MIGRATION)
) {
console.warn(
`${type}_MIGRATION or SOURCELOOP_MIGRATION variables not found in the environment, skipping automigration.`,
);
} else {
dotenvExt.load({
schema: `./migrations/.env.schema`,
path: `${process.env.INIT_CWD}/.env`,
schema: path.join('.', 'migrations', '.env.schema'),
path: path.join(process.env.INIT_CWD, '.env'),
errorOnMissing: true,
includeProcessEnv: true,
});
Expand All @@ -32,10 +35,9 @@ if (

if (
process.env.SOURCELOOP_MIGRATION_COPY ||
process.env.WORKFLOW_MIGRATION_COPY
process.env[`${type}_MIGRATION_COPY`]
) {
copyFileSync('./database.json', process.env.INIT_CWD);
copyFolderRecursiveSync('./migrations', process.env.INIT_CWD);
copyFolderRecursiveSync(path.join('.', 'migrations'), process.env.INIT_CWD);
}

function copyFileSync(source, target) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
DROP SCHEMA IF EXISTS main CASCADE;
CREATE SCHEMA main;
GRANT ALL ON SCHEMA main TO public;

Expand Down
2 changes: 1 addition & 1 deletion services/in-mail-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ any client application.

### Migration

The migrations required for this service are processed during the installation automatically, if you plan to use the [Loopback Database Migrations] (https://loopback.io/doc/en/lb4/Database-migrations.html) or your own custom migrations, you can disable the auto-migration script by setting the `INMAIL_MIGRATION_SKIP` or `SOURCELOOP_MIGRATION_SKIP` env variable to true before installing the package. If you need the migration files in your project root, use the `INMAIL_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variable.
The migrations required for this service are processed during the installation automatically if you set the `INMAIL_MIGRATION` or `SOURCELOOP_MIGRATION` env variable. Please note that if you are using some pre-existing migrations or database, they may be effected. In such scenario, it is advised that you copy the migration files in your project root, using the `INMAIL_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variables. You can customize or cherry-pick the migrations in the copied files according to your specific requirements and then apply them to the DB.

### Implementation

Expand Down
21 changes: 11 additions & 10 deletions services/in-mail-service/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const DBMigrate = require('db-migrate');
const path = require('path');
let isLocal = false;
dotenv.config({path: path.join(process.env.INIT_CWD, '.env')});
const type = 'INMAIL';

try {
if (fs.existsSync('.infolder')) {
Expand All @@ -13,16 +14,17 @@ try {
} catch (err) {
console.info('\n');
}

if (
isLocal ||
process.env.INMAIL_MIGRATION_SKIP ||
process.env.SOURCELOOP_MIGRATION_SKIP
) {
if (isLocal) {
console.info(`Skipping migrations`);
} else if (
!(process.env[`${type}_MIGRATION`] || process.env.SOURCELOOP_MIGRATION)
) {
console.warn(
`${type}_MIGRATION or SOURCELOOP_MIGRATION variables not found in the environment, skipping automigration.`,
);
} else {
dotenvExt.load({
schema: path.join(`.`, `migrations`, `.env.schema`),
schema: path.join('.', 'migrations', '.env.schema'),
path: path.join(process.env.INIT_CWD, '.env'),
errorOnMissing: true,
includeProcessEnv: true,
Expand All @@ -33,10 +35,9 @@ if (

if (
process.env.SOURCELOOP_MIGRATION_COPY ||
process.env.INMAIL_MIGRATION_COPY
process.env[`${type}_MIGRATION_COPY`]
) {
copyFileSync(path.join('.', 'database.json'), process.env.INIT_CWD);
copyFolderRecursiveSync(path.join('.', '/migrations'), process.env.INIT_CWD);
copyFolderRecursiveSync(path.join('.', 'migrations'), process.env.INIT_CWD);
}

function copyFileSync(source, target) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
DROP SCHEMA IF EXISTS main
CASCADE;
CREATE SCHEMA main;
SET search_path
TO main, public, logs;
Expand Down
2 changes: 1 addition & 1 deletion services/notification-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class NotificationDbDataSource extends juggler.DataSource

### Migrations

The migrations required for this service are processed during the installation automatically, if you plan to use the [Loopback Database Migrations] (https://loopback.io/doc/en/lb4/Database-migrations.html) or your own custom migrations, you can disable the auto-migration script by setting the `NOTIF_MIGRATION_SKIP` or `SOURCELOOP_MIGRATION_SKIP` env variable to true before installing the package. If you need the migration files in your project root, use the `NOTIF_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variable.
The migrations required for this service are processed during the installation automatically if you set the `NOTIF_MIGRATION` or `SOURCELOOP_MIGRATION` env variable. Please note that if you are using some pre-existing migrations or database, they may be effected. In such scenario, it is advised that you copy the migration files in your project root, using the `NOTIF_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variables. You can customize or cherry-pick the migrations in the copied files according to your specific requirements and then apply them to the DB.

### API Documentation

Expand Down
27 changes: 16 additions & 11 deletions services/notification-service/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const fs = require('fs');
const DBMigrate = require('db-migrate');
const path = require('path');
let isLocal = false;
dotenv.config({path: `${process.env.INIT_CWD}/.env`});
dotenv.config({path: path.join(process.env.INIT_CWD, '.env')});
const type = 'NOTIF';

try {
if (fs.existsSync('.infolder')) {
Expand All @@ -13,26 +14,30 @@ try {
} catch (err) {
console.info('\n');
}
if (
isLocal ||
process.env.NOTIF_MIGRATION_SKIP ||
process.env.SOURCELOOP_MIGRATION_SKIP
) {
if (isLocal) {
console.info(`Skipping migrations`);
} else if (
!(process.env[`${type}_MIGRATION`] || process.env.SOURCELOOP_MIGRATION)
) {
console.warn(
`${type}_MIGRATION or SOURCELOOP_MIGRATION variables not found in the environment, skipping automigration.`,
);
} else {
dotenvExt.load({
schema: `./migrations/.env.schema`,
path: `${process.env.INIT_CWD}/.env`,
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.NOTIF_MIGRATION_COPY) {
copyFileSync('./database.json', process.env.INIT_CWD);
copyFolderRecursiveSync('./migrations', process.env.INIT_CWD);
if (
process.env.SOURCELOOP_MIGRATION_COPY ||
process.env[`${type}_MIGRATION_COPY`]
) {
copyFolderRecursiveSync(path.join('.', 'migrations'), process.env.INIT_CWD);
}

function copyFileSync(source, target) {
Expand Down
6 changes: 6 additions & 0 deletions services/notification-service/migrations/.env.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DB_HOST=
DB_PORT=
DB_USER=
DB_PASSWORD=
DB_DATABASE=
DB_SCHEMA=
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
DROP SCHEMA IF EXISTS main CASCADE;
CREATE SCHEMA main;

SET search_path TO main,public;
Expand Down
2 changes: 1 addition & 1 deletion services/scheduler-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class SchedulerServiceApplication extends BootMixin(

## DB migrations

Since database migrations run using db-migrate package, we need to supply .env file to provide database configuration parameters. All other configurations parameters will be injected to and from context.
The migrations required for this service are processed during the installation automatically if you set the `SCHEDULAR_MIGRATION` or `SOURCELOOP_MIGRATION` env variable. Please note that if you are using some pre-existing migrations or database, they may be effected. In such scenario, it is advised that you copy the migration files in your project root, using the `SCHEDULAR_MIGRATION_COPY` or `SOURCELOOP_MIGRATION_COPY` env variables. You can customize or cherry-pick the migrations in the copied files according to your specific requirements and then apply them to the DB.

#### Database Model

Expand Down
20 changes: 11 additions & 9 deletions services/scheduler-service/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const DBMigrate = require('db-migrate');
const path = require('path');
let isLocal = false;
dotenv.config({path: path.join(process.env.INIT_CWD, '.env')});
const type = 'SCHEDULAR';

try {
if (fs.existsSync('.infolder')) {
Expand All @@ -13,15 +14,17 @@ try {
} catch (err) {
console.info('\n');
}
if (
isLocal ||
process.env.SCHEDULER_MIGRATION_SKIP ||
process.env.SOURCELOOP_MIGRATION_SKIP
) {
if (isLocal) {
console.info(`Skipping migrations`);
} else if (
!(process.env[`${type}_MIGRATION`] || process.env.SOURCELOOP_MIGRATION)
) {
console.warn(
`${type}_MIGRATION or SOURCELOOP_MIGRATION variables not found in the environment, skipping automigration.`,
);
} else {
dotenvExt.load({
schema: path.join(`.`, `migrations`, `.env.schema`),
schema: path.join('.', 'migrations', '.env.schema'),
path: path.join(process.env.INIT_CWD, '.env'),
errorOnMissing: true,
includeProcessEnv: true,
Expand All @@ -32,10 +35,9 @@ if (

if (
process.env.SOURCELOOP_MIGRATION_COPY ||
process.env.SCHEDULER_MIGRATION_COPY
process.env[`${type}_MIGRATION_COPY`]
) {
copyFileSync(path.join('.', 'database.json'), process.env.INIT_CWD);
copyFolderRecursiveSync(path.join('.', '/migrations'), process.env.INIT_CWD);
copyFolderRecursiveSync(path.join('.', 'migrations'), process.env.INIT_CWD);
}

function copyFileSync(source, target) {
Expand Down
6 changes: 6 additions & 0 deletions services/scheduler-service/migrations/.env.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DB_HOST=
DB_PORT=
DB_USER=
DB_PASSWORD=
DB_DATABASE=
DB_SCHEMA=
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* Replace with your SQL commands */
DROP SCHEMA IF EXISTS scheduler CASCADE;

CREATE SCHEMA scheduler;

GRANT ALL ON SCHEMA scheduler TO public;
Expand Down

0 comments on commit dd7bea2

Please sign in to comment.