Skip to content

Commit

Permalink
feat(in-mail-service): added migration in all services (#179)
Browse files Browse the repository at this point in the history
MIGRATION CHANGE:
migration-20210501132806- added migration for notif service

BREAKING CHANGE:
need to add env in existing projects to skip auto-migrations

gh-124
  • Loading branch information
akshatdubeysf authored May 1, 2021
1 parent 42458b5 commit 10aa077
Show file tree
Hide file tree
Showing 38 changed files with 1,389 additions and 1,943 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

Refer to [Database Migrations | LoopBack Documentation](https://loopback.io/doc/en/lb4/Database-migrations.html) for instructions for handling 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.

### API Documentation

Expand Down
6 changes: 6 additions & 0 deletions services/audit-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=
51 changes: 48 additions & 3 deletions services/audit-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions services/audit-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
"@sourceloop/core": "^1.0.0-alpha.27",
"db-migrate": "^0.11.12",
"db-migrate-pg": "^1.2.2",
"dotenv": "^8.2.0",
"dotenv-extended": "^2.9.0",
"loopback4-authentication": "^4.1.0",
"loopback4-authorization": "^3.2.0",
"prom-client": "^13.1.0",
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` to true before installing the package.
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.

### API Documentation

Expand Down
7 changes: 2 additions & 5 deletions services/authentication-service/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ try {
}
if (
isLocal ||
process.env.WORKFLOW_MIGRATION_SKIP ||
process.env.AUTH_MIGRATION_SKIP ||
process.env.SOURCELOOP_MIGRATION_SKIP
) {
console.info(`Skipping migrations`);
Expand All @@ -30,10 +30,7 @@ if (
dbmigrate.up();
}

if (
process.env.SOURCELOOP_MIGRATION_COPY ||
process.env.WORKFLOW_MIGRATION_COPY
) {
if (process.env.SOURCELOOP_MIGRATION_COPY || process.env.AUTH_MIGRATION_COPY) {
copyFileSync('./database.json', process.env.INIT_CWD);
copyFolderRecursiveSync('./migrations', process.env.INIT_CWD);
}
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` env variable to true before installing the package. If you need the migration files in your project root, use the `WORKFLOW_MIGRATION_COPY` env variable.
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.

### API Documentation

Expand Down
6 changes: 6 additions & 0 deletions services/bpmn-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=
1 change: 1 addition & 0 deletions services/in-mail-service/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ dist/
coverage/

migrations/
migration.js
.eslintrc.js
Empty file.
3 changes: 2 additions & 1 deletion services/in-mail-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ any client application.
## Implementation

### Migration
The project ships with generated migrations for PostgreSQL in the [migrations](migrations) folder. For generating migrations and implementing other data sources, please refer to refer to [Database Migrations | LoopBack Documentation](https://loopback.io/doc/en/lb4/Database-migrations.html) for more information.

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.

### Implementation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
"master": {
"driver": "pg",
"host": {
"ENV": "INMAIL_DB_HOST"
"ENV": "DB_HOST"
},
"port": {
"ENV": "INMAIL_DB_PORT"
"ENV": "DB_PORT"
},
"user": {
"ENV": "INMAIL_DB_USER"
"ENV": "DB_USER"
},
"password": {
"ENV": "INMAIL_DB_PASSWORD"
"ENV": "DB_PASSWORD"
},
"database": {
"ENV": "INMAIL_DB_DATABASE"
"ENV": "DB_DATABASE"
}
},
"sql-file": true
Expand Down
76 changes: 76 additions & 0 deletions services/in-mail-service/migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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.INMAIL_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.INMAIL_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);
}
});
}
}
6 changes: 6 additions & 0 deletions services/in-mail-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=
Loading

0 comments on commit 10aa077

Please sign in to comment.