Skip to content

Commit 8b066bb

Browse files
feat(all-services): Automate Migration Execution on Installation with Optional MySQL or PostgreSQL Migration Selection via Flag (#2209)
* feat(all-services): Automate Migration Execution on Installation with Optional MySQL or PostgreSQL Migration Selection via Flag Automate Migration Execution on Installation with Optional MySQL or PostgreSQL Migration Selection via Flag 2207 * docs(all-services): update the readme of all services update the readme of all services 2207 * fix(all-services): remove sonar remove sonar 2207 * fix(all-services): lint fix lint fix 2207 --------- Co-authored-by: yeshamavani <[email protected]>
1 parent e34e826 commit 8b066bb

File tree

30 files changed

+329
-162
lines changed

30 files changed

+329
-162
lines changed

services/audit-service/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ export class AuditDbDataSource
128128

129129
The migrations required for this service are processed during the installation automatically if you set the `AUDIT_MIGRATION` or `SOURCELOOP_MIGRATION` env variable. The migrations use [`db-migrate`](https://www.npmjs.com/package/db-migrate) with [`db-migrate-pg`](https://www.npmjs.com/package/db-migrate-pg) driver for migrations, so you will have to install these packages to use auto-migration. Please note that if you are using some pre-existing migrations or databases, they may be affected. In such a 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.
130130

131+
132+
This migration script supports both MySQL and PostgreSQL databases, controlled by environment variables. By setting MYSQL_MIGRATION to 'true', the script runs migrations using MySQL configuration files; otherwise, it defaults to PostgreSQL. .
133+
134+
131135
Additionally, there is now an option to choose between SQL migration or PostgreSQL migration.
132136
NOTE : for [`@sourceloop/cli`](https://www.npmjs.com/package/@sourceloop/cli?activeTab=readme) users, this choice can be specified during the scaffolding process by selecting the "type of datasource" option.
133137

services/audit-service/migration.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ const dotenvExt = require('dotenv-extended');
22
const fs = require('fs');
33
const path = require('path');
44
let isLocal = false;
5+
6+
// Load environment variables
57
dotenvExt.load({
68
path: path.join(process.env.INIT_CWD ?? '.', '.env'),
79
defaults: path.join(process.env.INIT_CWD ?? '.', '.env.defaults'),
810
errorOnMissing: false,
911
includeProcessEnv: true,
1012
});
13+
1114
const type = 'AUDIT';
15+
const isMysqlMigration = process.env.MYSQL_MIGRATION === 'true'; // Check for MySQL migration flag
1216

1317
try {
1418
if (fs.existsSync('.infolder')) {
@@ -17,14 +21,14 @@ try {
1721
} catch (err) {
1822
console.info('\n');
1923
}
24+
2025
if (isLocal) {
2126
console.info(`Skipping migrations`);
2227
} else if (
2328
!(process.env[`${type}_MIGRATION`] || process.env.SOURCELOOP_MIGRATION)
2429
) {
2530
console.warn(
26-
`${type}_MIGRATION or SOURCELOOP_MIGRATION variables not found in the environment,
27-
skipping automigration.`,
31+
`${type}_MIGRATION or SOURCELOOP_MIGRATION variables not found in the environment, skipping automigration.`,
2832
);
2933
} else {
3034
const DBMigrate = require('db-migrate');
@@ -34,50 +38,46 @@ if (isLocal) {
3438
errorOnMissing: true,
3539
includeProcessEnv: true,
3640
});
37-
// Initialize db-migrate with correct migration directory and database config path
41+
42+
// Set migration directory and database config path based on MYSQL_MIGRATION flag
43+
const migrationDir = isMysqlMigration ? 'mysql' : 'pg';
3844
const dbmigrate = DBMigrate.getInstance(true, {
39-
config: path.join('migrations', 'pg', 'database.json'),
40-
cwd: path.resolve(process.cwd(), 'migrations', 'pg'),
45+
config: path.join('migrations', migrationDir, 'database.json'),
46+
cwd: path.resolve(process.cwd(), 'migrations', migrationDir),
4147
});
48+
4249
dbmigrate.up();
4350
}
4451

52+
// Copy migration files if COPY flag is set
4553
if (
4654
process.env.SOURCELOOP_MIGRATION_COPY ||
4755
process.env[`${type}_MIGRATION_COPY`]
4856
) {
57+
const migrationDir = isMysqlMigration ? 'mysql' : 'pg';
4958
copyFolderRecursiveSync(
50-
path.join('.', 'migrations', 'pg', 'migrations'),
59+
path.join('.', 'migrations', migrationDir, 'migrations'),
5160
process.env.INIT_CWD,
5261
);
5362
}
5463

64+
// Utility functions for copying files and folders
5565
function copyFileSync(source, target) {
5666
let targetFile = target;
57-
58-
// If target is a directory, a new file with the same name will be created
59-
if (fs.existsSync(target)) {
60-
if (fs.lstatSync(target).isDirectory()) {
61-
targetFile = path.join(target, path.basename(source));
62-
}
67+
if (fs.existsSync(target) && fs.lstatSync(target).isDirectory()) {
68+
targetFile = path.join(target, path.basename(source));
6369
}
64-
6570
fs.writeFileSync(targetFile, fs.readFileSync(source));
6671
}
6772

6873
function copyFolderRecursiveSync(source, target) {
69-
let files = [];
70-
71-
// Check if folder needs to be created or integrated
72-
let targetFolder = path.join(target, path.basename(source));
74+
const targetFolder = path.join(target, path.basename(source));
7375
if (!fs.existsSync(targetFolder)) {
7476
fs.mkdirSync(targetFolder);
7577
}
76-
77-
// Copy
7878
if (fs.lstatSync(source).isDirectory()) {
79-
files = fs.readdirSync(source);
80-
files.forEach(function (file) {
79+
const files = fs.readdirSync(source);
80+
files.forEach(file => {
8181
const curSource = path.join(source, file);
8282
if (fs.lstatSync(curSource).isDirectory()) {
8383
copyFolderRecursiveSync(curSource, targetFolder);

services/authentication-service/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ export class AuthenticationDbDataSource
485485

486486
The migrations required for this service are processed during the installation automatically if you set the `AUTH_MIGRATION` or `SOURCELOOP_MIGRATION` env variable. The migrations use [`db-migrate`](https://www.npmjs.com/package/db-migrate) with [`db-migrate-pg`](https://www.npmjs.com/package/db-migrate-pg) driver for migrations, so you will have to install these packages to use auto-migration. Please note that if you are using some pre-existing migrations or databases, they may be affected. In such a 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.
487487

488+
489+
This migration script supports both MySQL and PostgreSQL databases, controlled by environment variables. By setting MYSQL_MIGRATION to 'true', the script runs migrations using MySQL configuration files; otherwise, it defaults to PostgreSQL. .
490+
488491
Additionally, there is now an option to choose between SQL migration or PostgreSQL migration.
489492
NOTE : For [`@sourceloop/cli`](https://www.npmjs.com/package/@sourceloop/cli?activeTab=readme) users, this choice can be specified during the scaffolding process by selecting the "type of datasource" option.
490493

services/authentication-service/migration.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ const dotenvExt = require('dotenv-extended');
22
const fs = require('fs');
33
const path = require('path');
44
let isLocal = false;
5+
6+
// Load environment variables
57
dotenvExt.load({
68
path: path.join(process.env.INIT_CWD ?? '.', '.env'),
79
defaults: path.join(process.env.INIT_CWD ?? '.', '.env.defaults'),
810
errorOnMissing: false,
911
includeProcessEnv: true,
1012
});
13+
1114
const type = 'AUTH';
15+
const isMysqlMigration = process.env.MYSQL_MIGRATION === 'true'; // Check for MySQL migration flag
1216

1317
try {
1418
if (fs.existsSync('.infolder')) {
@@ -17,6 +21,7 @@ try {
1721
} catch (err) {
1822
console.info('\n');
1923
}
24+
2025
if (isLocal) {
2126
console.info(`Skipping migrations`);
2227
} else if (
@@ -33,51 +38,46 @@ if (isLocal) {
3338
errorOnMissing: true,
3439
includeProcessEnv: true,
3540
});
36-
// Initialize db-migrate with correct migration directory and database config path
41+
42+
// Set migration directory and database config path based on MYSQL_MIGRATION flag
43+
const migrationDir = isMysqlMigration ? 'mysql' : 'pg';
3744
const dbmigrate = DBMigrate.getInstance(true, {
38-
config: path.join('migrations', 'pg', 'database.json'),
39-
cwd: path.resolve(process.cwd(), 'migrations', 'pg'),
45+
config: path.join('migrations', migrationDir, 'database.json'),
46+
cwd: path.resolve(process.cwd(), 'migrations', migrationDir),
4047
});
4148

4249
dbmigrate.up();
4350
}
4451

52+
// Copy migration files if COPY flag is set
4553
if (
4654
process.env.SOURCELOOP_MIGRATION_COPY ||
4755
process.env[`${type}_MIGRATION_COPY`]
4856
) {
57+
const migrationDir = isMysqlMigration ? 'mysql' : 'pg';
4958
copyFolderRecursiveSync(
50-
path.join('.', 'migrations', 'pg', 'migrations'),
59+
path.join('.', 'migrations', migrationDir, 'migrations'),
5160
process.env.INIT_CWD,
5261
);
5362
}
5463

64+
// Utility functions for copying files and folders
5565
function copyFileSync(source, target) {
5666
let targetFile = target;
57-
58-
// If target is a directory, a new file with the same name will be created
59-
if (fs.existsSync(target)) {
60-
if (fs.lstatSync(target).isDirectory()) {
61-
targetFile = path.join(target, path.basename(source));
62-
}
67+
if (fs.existsSync(target) && fs.lstatSync(target).isDirectory()) {
68+
targetFile = path.join(target, path.basename(source));
6369
}
64-
6570
fs.writeFileSync(targetFile, fs.readFileSync(source));
6671
}
6772

6873
function copyFolderRecursiveSync(source, target) {
69-
let files = [];
70-
71-
// Check if folder needs to be created or integrated
7274
const targetFolder = path.join(target, path.basename(source));
7375
if (!fs.existsSync(targetFolder)) {
7476
fs.mkdirSync(targetFolder);
7577
}
76-
77-
// Copy
7878
if (fs.lstatSync(source).isDirectory()) {
79-
files = fs.readdirSync(source);
80-
files.forEach(function (file) {
79+
const files = fs.readdirSync(source);
80+
files.forEach(file => {
8181
const curSource = path.join(source, file);
8282
if (fs.lstatSync(curSource).isDirectory()) {
8383
copyFolderRecursiveSync(curSource, targetFolder);

services/bpmn-service/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ A sample implementation of a `DataSource` using environment variables and Postgr
195195

196196
The migrations required for this service are processed during the installation automatically if you set the `WORKFLOW_MIGRATION` or `SOURCELOOP_MIGRATION` env variable. The migrations use [`db-migrate`](https://www.npmjs.com/package/db-migrate) with [`db-migrate-pg`](https://www.npmjs.com/package/db-migrate-pg) driver for migrations, so you will have to install these packages to use auto-migration. Please note that if you are using some pre-existing migrations or databases, they may be affected. In such a 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.
197197

198+
199+
This migration script supports both MySQL and PostgreSQL databases, controlled by environment variables. By setting MYSQL_MIGRATION to 'true', the script runs migrations using MySQL configuration files; otherwise, it defaults to PostgreSQL. .
200+
198201
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/).
199202

200203
Additionally, there is now an option to choose between SQL migration or PostgreSQL migration.

services/bpmn-service/migration.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ const dotenvExt = require('dotenv-extended');
22
const fs = require('fs');
33
const path = require('path');
44
let isLocal = false;
5+
6+
// Load environment variables
57
dotenvExt.load({
68
path: path.join(process.env.INIT_CWD ?? '.', '.env'),
79
defaults: path.join(process.env.INIT_CWD ?? '.', '.env.defaults'),
810
errorOnMissing: false,
911
includeProcessEnv: true,
1012
});
13+
1114
const type = 'WORKFLOW';
15+
const isMysqlMigration = process.env.MYSQL_MIGRATION === 'true'; // Check for MySQL migration flag
1216

1317
try {
1418
if (fs.existsSync('.infolder')) {
@@ -17,6 +21,7 @@ try {
1721
} catch (err) {
1822
console.info('\n');
1923
}
24+
2025
if (isLocal) {
2126
console.info(`Skipping migrations`);
2227
} else if (
@@ -33,50 +38,46 @@ if (isLocal) {
3338
errorOnMissing: true,
3439
includeProcessEnv: true,
3540
});
36-
// Initialize db-migrate with correct migration directory and database config path
41+
42+
// Set migration directory and database config path based on MYSQL_MIGRATION flag
43+
const migrationDir = isMysqlMigration ? 'mysql' : 'pg';
3744
const dbmigrate = DBMigrate.getInstance(true, {
38-
config: path.join('migrations', 'pg', 'database.json'),
39-
cwd: path.resolve(process.cwd(), 'migrations', 'pg'),
45+
config: path.join('migrations', migrationDir, 'database.json'),
46+
cwd: path.resolve(process.cwd(), 'migrations', migrationDir),
4047
});
48+
4149
dbmigrate.up();
4250
}
4351

52+
// Copy migration files if COPY flag is set
4453
if (
4554
process.env.SOURCELOOP_MIGRATION_COPY ||
4655
process.env[`${type}_MIGRATION_COPY`]
4756
) {
57+
const migrationDir = isMysqlMigration ? 'mysql' : 'pg';
4858
copyFolderRecursiveSync(
49-
path.join('.', 'migrations', 'pg', 'migrations'),
59+
path.join('.', 'migrations', migrationDir, 'migrations'),
5060
process.env.INIT_CWD,
5161
);
5262
}
5363

64+
// Utility functions for copying files and folders
5465
function copyFileSync(source, target) {
5566
let targetFile = target;
56-
57-
// If target is a directory, a new file with the same name will be created
58-
if (fs.existsSync(target)) {
59-
if (fs.lstatSync(target).isDirectory()) {
60-
targetFile = path.join(target, path.basename(source));
61-
}
67+
if (fs.existsSync(target) && fs.lstatSync(target).isDirectory()) {
68+
targetFile = path.join(target, path.basename(source));
6269
}
63-
6470
fs.writeFileSync(targetFile, fs.readFileSync(source));
6571
}
6672

6773
function copyFolderRecursiveSync(source, target) {
68-
let files = [];
69-
70-
// Check if folder needs to be created or integrated
7174
const targetFolder = path.join(target, path.basename(source));
7275
if (!fs.existsSync(targetFolder)) {
7376
fs.mkdirSync(targetFolder);
7477
}
75-
76-
// Copy
7778
if (fs.lstatSync(source).isDirectory()) {
78-
files = fs.readdirSync(source);
79-
files.forEach(function (file) {
79+
const files = fs.readdirSync(source);
80+
files.forEach(file => {
8081
const curSource = path.join(source, file);
8182
if (fs.lstatSync(curSource).isDirectory()) {
8283
copyFolderRecursiveSync(curSource, targetFolder);

services/chat-service/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ export class ChatDataSource
147147

148148
The migrations required for this service are processed during the installation automatically if you set the `CHAT_MIGRATION` or `SOURCELOOP_MIGRATION` env variable. The migrations use [`db-migrate`](https://www.npmjs.com/package/db-migrate) with [`db-migrate-pg`](https://www.npmjs.com/package/db-migrate-pg) driver for migrations, so you will have to install these packages to use auto-migration. Please note that if you are using some pre-existing migrations or databasea, they may be affected. In such a scenario, it is advised that you copy the migration files in your project root, using the `CHAT_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.
149149

150+
151+
This migration script supports both MySQL and PostgreSQL databases, controlled by environment variables. By setting MYSQL_MIGRATION to 'true', the script runs migrations using MySQL configuration files; otherwise, it defaults to PostgreSQL. .
152+
150153
Additionally, there is now an option to choose between SQL migration or PostgreSQL migration.
151154
NOTE: For [`@sourceloop/cli`](https://www.npmjs.com/package/@sourceloop/cli?activeTab=readme) users, this choice can be specified during the scaffolding process by selecting the "type of datasource" option.
152155

0 commit comments

Comments
 (0)