@crowdin/app-project-module 0.101.0 → 0.102.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,7 +24,7 @@ function getInputSchemaHandler(automationAction) {
24
24
  req: req.body,
25
25
  });
26
26
  }
27
- res.json(result);
27
+ res.json({ data: result });
28
28
  }
29
29
  catch (error) {
30
30
  req.logError(error);
@@ -70,7 +70,7 @@ function initialize(config) {
70
70
  dumpDirectory = config.dbFolder;
71
71
  createDumpForMigration(config);
72
72
  }
73
- storage = new postgre_1.PostgreStorage(config.postgreConfig, dumpDirectory);
73
+ storage = new postgre_1.PostgreStorage(config, dumpDirectory);
74
74
  }
75
75
  else if (config.mysqlConfig) {
76
76
  (0, logger_1.log)('Using MySQL database');
@@ -1,6 +1,6 @@
1
1
  import { Client } from 'pg';
2
2
  import { Storage } from '.';
3
- import { CrowdinCredentials } from '../types';
3
+ import { Config, UnauthorizedConfig, CrowdinCredentials } from '../types';
4
4
  import { IntegrationConfig, IntegrationCredentials, IntegrationFilesSnapshot, IntegrationSyncSettings, IntegrationWebhooks } from '../modules/integration/types';
5
5
  import { CreateJobParams, GetActiveJobsParams, GetJobParams, GetFileTranslationCacheByLanguageParams, Job, TranslationCache, UpdateJobParams, UpdateTranslationCacheParams, GetFileTranslationCache, UnsyncedFiles, GetUnsyncedFiles, IntegrationSyncedData, GetAllJobsParams } from '../modules/integration/util/types';
6
6
  import { UserErrors } from './types';
@@ -24,6 +24,7 @@ interface TableIndexes {
24
24
  export declare class PostgreStorage implements Storage {
25
25
  private config;
26
26
  private directoryPath;
27
+ private migrationLockId;
27
28
  private _res?;
28
29
  private _rej?;
29
30
  private dbPromise;
@@ -42,7 +43,8 @@ export declare class PostgreStorage implements Storage {
42
43
  synced_data: string;
43
44
  };
44
45
  tableIndexes: TableIndexes;
45
- constructor(config: PostgreStorageConfig, directoryPath: string | null);
46
+ constructor(config: Config | UnauthorizedConfig, directoryPath: string | null);
47
+ private generateLockId;
46
48
  executeQuery<T>(command: (client: Client) => Promise<T>): Promise<T>;
47
49
  private hasDumpFiles;
48
50
  migrate(): Promise<void>;
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.PostgreStorage = void 0;
18
18
  const pg_1 = require("pg");
19
19
  const uuid_1 = require("uuid");
20
+ const crypto_1 = require("crypto");
20
21
  const types_1 = require("../types");
21
22
  const types_2 = require("../modules/integration/util/types");
22
23
  const util_1 = require("../util");
@@ -171,8 +172,15 @@ class PostgreStorage {
171
172
  idx_crowdin: '(crowdin_id)',
172
173
  },
173
174
  };
174
- this.config = config;
175
+ this.config = config.postgreConfig;
175
176
  this.directoryPath = directoryPath;
177
+ this.migrationLockId = this.generateLockId(config.name);
178
+ }
179
+ generateLockId(str) {
180
+ // Create MD5 hash of the string
181
+ const hash = (0, crypto_1.createHash)('md5').update(str).digest('hex');
182
+ // Take first 8 hex characters and convert to integer
183
+ return parseInt(hash.substring(0, 8), 16);
176
184
  }
177
185
  executeQuery(command) {
178
186
  return __awaiter(this, void 0, void 0, function* () {
@@ -210,10 +218,19 @@ class PostgreStorage {
210
218
  if (this.directoryPath && this.hasDumpFiles(this.directoryPath)) {
211
219
  yield this.migrateFromSqlite(this.directoryPath);
212
220
  }
213
- yield this.executeQuery((client) => this.addTables(client));
221
+ // Use advisory lock to prevent concurrent migrations
222
+ yield this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
223
+ yield client.query('SELECT pg_advisory_lock($1)', [this.migrationLockId]);
224
+ try {
225
+ yield this.addTables(client);
226
+ // TODO: temporary code
227
+ yield this.alterTables(client);
228
+ }
229
+ finally {
230
+ yield client.query('SELECT pg_advisory_unlock($1)', [this.migrationLockId]);
231
+ }
232
+ }));
214
233
  this._res && this._res();
215
- // TODO: temporary code
216
- yield this.executeQuery((client) => this.alterTables(client));
217
234
  }
218
235
  catch (e) {
219
236
  console.error(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "0.101.0",
3
+ "version": "0.102.0",
4
4
  "description": "Module that generates for you all common endpoints for serving standalone Crowdin App",
5
5
  "main": "out/index.js",
6
6
  "types": "out/index.d.ts",