@stamhoofd/backend-statistics-syncer 2.152.0 → 2.160.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.
Files changed (82) hide show
  1. package/LICENSE.md +7 -7
  2. package/README.md +2 -2
  3. package/dist/index.js +17 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/migrations.js +19 -0
  6. package/dist/migrations.js.map +1 -0
  7. package/dist/src/boot.js +75 -0
  8. package/dist/src/boot.js.map +1 -0
  9. package/dist/src/crons.js +39 -0
  10. package/dist/src/crons.js.map +1 -0
  11. package/dist/src/database.js +88 -0
  12. package/dist/src/database.js.map +1 -0
  13. package/dist/src/migrate.js +38 -0
  14. package/dist/src/migrate.js.map +1 -0
  15. package/dist/src/migrations/1785958573-platform-statistics-schema.sql +382 -0
  16. package/dist/src/migrations/1786203367-postal-code-coordinates.sql +1203 -0
  17. package/dist/src/periods.js +86 -0
  18. package/dist/src/periods.js.map +1 -0
  19. package/dist/src/rows.js +209 -0
  20. package/dist/src/rows.js.map +1 -0
  21. package/dist/src/schema.js +28 -0
  22. package/dist/src/schema.js.map +1 -0
  23. package/dist/src/sources.js +10 -0
  24. package/dist/src/sources.js.map +1 -0
  25. package/dist/src/sync-state.js +36 -0
  26. package/dist/src/sync-state.js.map +1 -0
  27. package/dist/src/sync.js +608 -0
  28. package/dist/src/sync.js.map +1 -0
  29. package/dist/src/writer.js +63 -0
  30. package/dist/src/writer.js.map +1 -0
  31. package/dist-types/index.d.ts +2 -0
  32. package/dist-types/index.d.ts.map +1 -0
  33. package/dist-types/migrations.d.ts +2 -0
  34. package/dist-types/migrations.d.ts.map +1 -0
  35. package/dist-types/src/boot.d.ts +8 -0
  36. package/dist-types/src/boot.d.ts.map +1 -0
  37. package/dist-types/src/crons.d.ts +21 -0
  38. package/dist-types/src/crons.d.ts.map +1 -0
  39. package/dist-types/src/database.d.ts +28 -0
  40. package/dist-types/src/database.d.ts.map +1 -0
  41. package/dist-types/src/migrate.d.ts +2 -0
  42. package/dist-types/src/migrate.d.ts.map +1 -0
  43. package/dist-types/src/periods.d.ts +51 -0
  44. package/dist-types/src/periods.d.ts.map +1 -0
  45. package/dist-types/src/rows.d.ts +212 -0
  46. package/dist-types/src/rows.d.ts.map +1 -0
  47. package/dist-types/src/schema.d.ts +9 -0
  48. package/dist-types/src/schema.d.ts.map +1 -0
  49. package/dist-types/src/sources.d.ts +10 -0
  50. package/dist-types/src/sources.d.ts.map +1 -0
  51. package/dist-types/src/sync-state.d.ts +20 -0
  52. package/dist-types/src/sync-state.d.ts.map +1 -0
  53. package/dist-types/src/sync.d.ts +37 -0
  54. package/dist-types/src/sync.d.ts.map +1 -0
  55. package/dist-types/src/writer.d.ts +24 -0
  56. package/dist-types/src/writer.d.ts.map +1 -0
  57. package/index.ts +5 -8
  58. package/migrations.ts +16 -11
  59. package/package.json +63 -50
  60. package/src/boot.ts +7 -3
  61. package/src/crons.test.ts +33 -8
  62. package/src/crons.ts +7 -1
  63. package/src/database.test.ts +48 -11
  64. package/src/database.ts +30 -7
  65. package/src/migrate.ts +1 -1
  66. package/src/periods.ts +5 -2
  67. package/src/rows.test.ts +96 -16
  68. package/src/rows.ts +28 -6
  69. package/src/schema.test.ts +48 -19
  70. package/src/schema.ts +14 -4
  71. package/src/sync-state.test.ts +3 -1
  72. package/src/sync-state.ts +13 -4
  73. package/src/sync.test.ts +145 -32
  74. package/src/sync.ts +422 -91
  75. package/src/writer.ts +32 -10
  76. package/tsconfig.build.json +4 -11
  77. package/tsconfig.json +1 -1
  78. package/tsconfig.test.json +1 -3
  79. package/tsconfig.types.json +7 -0
  80. package/tests/vitest.global.setup.ts +0 -29
  81. package/tests/vitest.setup.ts +0 -24
  82. package/vitest.config.js +0 -18
package/LICENSE.md CHANGED
@@ -5,25 +5,25 @@ Copyright 2020 Codawood BV
5
5
  Permission is hereby granted, free of charge, to any non-profit organization with fewer than 2,000 members (each, a “Licensee”), to use this software and associated documentation files (the “Software”), subject to the following conditions:
6
6
 
7
7
  1. Eligibility:
8
- This license applies only if neither the Licensee nor, if applicable, its parent or umbrella organization, is already subject to an existing license for this project. For the purposes of this license, a “parent or umbrella organization” means any organization with which the Licensee is affiliated, coordinated, or represented as part of a larger collective, including but not limited to national associations or federations.
8
+ This license applies only if neither the Licensee nor, if applicable, its parent or umbrella organization, is already subject to an existing license for this project. For the purposes of this license, a “parent or umbrella organization” means any organization with which the Licensee is affiliated, coordinated, or represented as part of a larger collective, including but not limited to national associations or federations.
9
9
 
10
10
  2. Modifications:
11
- Modifications to the Software are permitted only if they are published publicly under the MIT License, covering only the modifications; the original Software remains under this license and may not be sublicensed without a separate agreement. Any modification made by a Licensee (other than modifications by the original author, its representatives, or the copyright owner) shall be deemed to be distributed under the MIT License. Submission of modifications through publicly accessible channels, including but not limited to pull requests on code repositories such as GitHub, shall be considered an explicit acknowledgment and grant by the Licensee that such modifications are made available under the MIT License. Licensees may not sublicense, transfer, or assign the rights granted under this license except as expressly permitted herein.
11
+ Modifications to the Software are permitted only if they are published publicly under the MIT License, covering only the modifications; the original Software remains under this license and may not be sublicensed without a separate agreement. Any modification made by a Licensee (other than modifications by the original author, its representatives, or the copyright owner) shall be deemed to be distributed under the MIT License. Submission of modifications through publicly accessible channels, including but not limited to pull requests on code repositories such as GitHub, shall be considered an explicit acknowledgment and grant by the Licensee that such modifications are made available under the MIT License. Licensees may not sublicense, transfer, or assign the rights granted under this license except as expressly permitted herein.
12
12
 
13
13
  3. Precedence of Existing Licenses:
14
- If the Licensee, or its parent or umbrella organization, is already subject to an existing license for this project, that license shall take precedence, and no additional free license is granted. Nothing in this license shall be construed to override any pre-existing exclusive license agreement.
14
+ If the Licensee, or its parent or umbrella organization, is already subject to an existing license for this project, that license shall take precedence, and no additional free license is granted. Nothing in this license shall be construed to override any pre-existing exclusive license agreement.
15
15
 
16
16
  4. Data Protection / GDPR Responsibility:
17
- Licensees are solely responsible for ensuring that any processing of personal data using this software complies with applicable data protection laws, including the EU General Data Protection Regulation (GDPR).
17
+ Licensees are solely responsible for ensuring that any processing of personal data using this software complies with applicable data protection laws, including the EU General Data Protection Regulation (GDPR).
18
18
 
19
19
  5. Termination and Revocation:
20
- The copyright holders may revoke this license at any time, at their sole discretion.
20
+ The copyright holders may revoke this license at any time, at their sole discretion.
21
21
 
22
22
  6. Governing Law:
23
- This license shall be governed by and construed in accordance with the laws of Belgium, without regard to conflict of law principles.
23
+ This license shall be governed by and construed in accordance with the laws of Belgium, without regard to conflict of law principles.
24
24
 
25
25
  7. Third-Party Licenses:
26
- Certain files or components included in the Software may be governed by separate licenses, including open-source licenses, or contributed by third parties under different terms. Such files are not automatically covered by this license. Licensees are responsible for reviewing and complying with any additional license terms that apply to these files.
26
+ Certain files or components included in the Software may be governed by separate licenses, including open-source licenses, or contributed by third parties under different terms. Such files are not automatically covered by this license. Licensees are responsible for reviewing and complying with any additional license terms that apply to these files.
27
27
 
28
28
  For the purposes of this license, a “non-profit organization” means an entity recognized as non-profit under applicable law in its jurisdiction, which does not distribute profits to its members or owners.
29
29
 
package/README.md CHANGED
@@ -20,8 +20,8 @@ shared package, so the dependency could only run the wrong way.
20
20
  ## Running
21
21
 
22
22
  ```bash
23
- yarn start # boot the service
24
- yarn migrations # migrate the statistics database (part of `yarn migrate` from the repo root)
23
+ pnpm run start # boot the service
24
+ pnpm run migrations # migrate the statistics database (part of `pnpm stam db migrate` from the repo root)
25
25
  ```
26
26
 
27
27
  Wherever this service runs, it syncs: there is nothing to switch off in configuration, so deploy it
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ import backendEnv from '@stamhoofd/backend-env';
2
+ import { BackendApp } from '@stamhoofd/types/BackendApp';
3
+ import { runApplication } from '@stamhoofd/node-observability';
4
+ process.title = 'stamhoofd-statistics-syncer';
5
+ runApplication('stats', async (lifecycle) => {
6
+ await backendEnv.load({ service: BackendApp.StatisticsSyncer });
7
+ if (STAMHOOFD.environment === 'development') {
8
+ const { run } = await import('./src/migrate.js');
9
+ await run();
10
+ }
11
+ const { boot } = await import('./src/boot.js');
12
+ await boot({ killProcess: true, lifecycle });
13
+ }).catch((error) => {
14
+ console.error('Failed to start the statistics service:', error);
15
+ process.exit(1);
16
+ });
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAE/D,OAAO,CAAC,KAAK,GAAG,6BAA6B,CAAC;AAC9C,cAAc,CAAC,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE;IACxC,MAAM,UAAU,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,UAAU,CAAC,gBAAgB,EAAE,CAAC,CAAC;IAChE,IAAI,SAAS,CAAC,WAAW,KAAK,aAAa,EAAE,CAAC;QAC1C,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;QACjD,MAAM,GAAG,EAAE,CAAC;IAChB,CAAC;IACD,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,eAAe,CAAC,CAAC;IAE/C,MAAM,IAAI,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC;AACjD,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACf,OAAO,CAAC,KAAK,CAAC,yCAAyC,EAAE,KAAK,CAAC,CAAC;IAChE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,19 @@
1
+ import backendEnv from '@stamhoofd/backend-env';
2
+ import { BackendApp } from '@stamhoofd/types/BackendApp';
3
+ process.title = 'stamhoofd-statistics-syncer-migrations';
4
+ backendEnv
5
+ .load({ service: BackendApp.StatisticsSyncer })
6
+ .catch((error) => {
7
+ console.error('Failed to load environment:', error);
8
+ process.exit(1);
9
+ })
10
+ .then(async () => {
11
+ const { run } = await import('./src/migrate.js');
12
+ await run();
13
+ process.exit(0);
14
+ })
15
+ .catch((error) => {
16
+ console.error('Failed to run migrations:', error);
17
+ process.exit(1);
18
+ });
19
+ //# sourceMappingURL=migrations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrations.js","sourceRoot":"","sources":["../migrations.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAEzD,OAAO,CAAC,KAAK,GAAG,wCAAwC,CAAC;AACzD,UAAU;KACL,IAAI,CAAC,EAAE,OAAO,EAAE,UAAU,CAAC,gBAAgB,EAAE,CAAC;KAC9C,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACb,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE,KAAK,CAAC,CAAC;IACpD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC;KACD,IAAI,CAAC,KAAK,IAAI,EAAE;IACb,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;IACjD,MAAM,GAAG,EAAE,CAAC;IACZ,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC;KACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACb,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAC;IAClD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,75 @@
1
+ import { Column, Database } from '@stamhoofd/node-database';
2
+ import { ApplicationLifecycle } from '@stamhoofd/node-observability';
3
+ import { startCrons, stopCrons, waitForCrons } from '@stamhoofd/crons';
4
+ import { loadLogger } from '@stamhoofd/logging';
5
+ import { Version } from '@stamhoofd/structures';
6
+ import { endStatisticsConnection } from './database.js';
7
+ import { statisticsLanguage } from './rows.js';
8
+ process.on('unhandledRejection', (error) => {
9
+ console.error('unhandledRejection');
10
+ console.error(error.message, error.stack);
11
+ process.exit(1);
12
+ });
13
+ // Set version of saved structures
14
+ Column.setJSONVersion(Version);
15
+ // Set timezone to UTC
16
+ process.env.TZ = 'UTC';
17
+ // Quick check
18
+ if (new Date().getTimezoneOffset() !== 0) {
19
+ throw new Error('Process should always run in UTC timezone');
20
+ }
21
+ /**
22
+ * The sync reads models, whose structures reach for the global $t while they decode. Nothing it
23
+ * writes is translated — rows.ts resolves every name it writes itself, never through a structure
24
+ * getter that runs $t — so returning the key is enough to keep decoding from throwing, and this
25
+ * service needs no locales of its own.
26
+ */
27
+ function loadGlobalTranslateFunction() {
28
+ global.$t = (key) => key;
29
+ global.$getLanguage = () => statisticsLanguage;
30
+ global.$getCountry = () => 'BE';
31
+ }
32
+ export const boot = async (options) => {
33
+ const lifecycle = options.lifecycle ?? new ApplicationLifecycle('stats');
34
+ loadLogger();
35
+ console.debug('Started Statistics.');
36
+ loadGlobalTranslateFunction();
37
+ const shutdown = async () => {
38
+ console.log('Shutting down...');
39
+ stopCrons();
40
+ await waitForCrons();
41
+ try {
42
+ await endStatisticsConnection();
43
+ await Database.end();
44
+ }
45
+ catch (error) {
46
+ console.error('Failed to close the database connections:');
47
+ console.error(error);
48
+ }
49
+ // Should not be needed, but added for security as sometimes a promise hangs somewhere
50
+ await lifecycle.close();
51
+ if (options.killProcess) {
52
+ process.exit(0);
53
+ }
54
+ };
55
+ process.on('SIGTERM', () => {
56
+ console.info('SIGTERM signal received.');
57
+ shutdown().catch((error) => {
58
+ console.error(error);
59
+ process.exit(1);
60
+ });
61
+ });
62
+ process.on('SIGINT', () => {
63
+ console.info('SIGINT signal received.');
64
+ shutdown().catch((error) => {
65
+ console.error(error);
66
+ process.exit(1);
67
+ });
68
+ });
69
+ const { statisticsCronOptions } = await import('./crons.js');
70
+ // This service runs no seeds of its own.
71
+ startCrons(statisticsCronOptions);
72
+ await lifecycle.ready();
73
+ return { shutdown };
74
+ };
75
+ //# sourceMappingURL=boot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"boot.js","sourceRoot":"","sources":["../../src/boot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAE/C,OAAO,CAAC,EAAE,CAAC,oBAAoB,EAAE,CAAC,KAAY,EAAE,EAAE;IAC9C,OAAO,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAC;IACpC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IAC1C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC;AAEH,kCAAkC;AAClC,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;AAE/B,sBAAsB;AACtB,OAAO,CAAC,GAAG,CAAC,EAAE,GAAG,KAAK,CAAC;AAEvB,cAAc;AACd,IAAI,IAAI,IAAI,EAAE,CAAC,iBAAiB,EAAE,KAAK,CAAC,EAAE,CAAC;IACvC,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;AACjE,CAAC;AAED;;;;;GAKG;AACH,SAAS,2BAA2B;IAC/B,MAAc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C;YACrB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE;QACtB,OAAO,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;QACxC,QAAQ,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACvB,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACrB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;IAEH,MAAM,EAAE,qBAAqB,EAAE,GAAG,MAAM,MAAM,CAAC,YAAY,CAAC,CAAC;IAE7D,yCAAyC;IACzC,UAAU,CAAC,qBAAqB,CAAC,CAAC;IAElC,MAAM,SAAS,CAAC,KAAK,EAAE,CAAC;IACxB,OAAO,EAAE,QAAQ,EAAE,CAAC;AACxB,CAAC,CAAC"}
@@ -0,0 +1,39 @@
1
+ import { registerCron } from '@stamhoofd/crons';
2
+ import { runStatisticsCycle } from './sync.js';
3
+ registerCron('syncPlatformStatistics', syncPlatformStatistics);
4
+ /**
5
+ * Both passes read the whole administration, which is far more than the five-minute cron cadence is
6
+ * meant for and does not need to be current to the minute: the reports they feed are read by day.
7
+ */
8
+ export function shouldRunStatisticsSync({ now, lastRun, }) {
9
+ if (STAMHOOFD.environment === 'development') {
10
+ return true;
11
+ }
12
+ if (lastRun === null) {
13
+ // Run right away
14
+ return true;
15
+ }
16
+ const hour = now.getHours();
17
+ if (hour < 3 || hour > 5) {
18
+ return false;
19
+ }
20
+ return lastRun === null || lastRun.getTime() <= now.getTime() - 12 * 60 * 60 * 1000;
21
+ }
22
+ let lastRun = null;
23
+ async function syncPlatformStatistics() {
24
+ if (!shouldRunStatisticsSync({ now: new Date(), lastRun })) {
25
+ return;
26
+ }
27
+ await runStatisticsCycle();
28
+ lastRun = new Date();
29
+ }
30
+ /**
31
+ * How the cron loop is started for this service.
32
+ *
33
+ * `allowReadOnly` is on because the syncer runs on a replica, whose MySQL is `read_only`. That flag
34
+ * guards the main database, and this service never writes to it: it reads the administration and
35
+ * writes through the separate connection to the statistics database, whose watermarks live in
36
+ * `stats_sync_state` there. Switching it off stops the sync from ever running where it is deployed.
37
+ */
38
+ export const statisticsCronOptions = { allowReadOnly: true, allowBeforeSeeds: true };
39
+ //# sourceMappingURL=crons.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crons.js","sourceRoot":"","sources":["../../src/crons.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAE/C,YAAY,CAAC,wBAAwB,EAAE,sBAAsB,CAAC,CAAC;AAE/D;;;GAGG;AACH,MAAM,UAAU,uBAAuB,CAAC,EACpC,GAAG,EACH,OAAO,GAIV;IACG,IAAI,SAAS,CAAC,WAAW,KAAK,aAAa,EAAE,CAAC;QAC1C,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACnB,iBAAiB;QACjB,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,MAAM,IAAI,GAAG,GAAG,CAAC,QAAQ,EAAE,CAAC;IAC5B,IAAI,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;QACvB,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,OAAO,OAAO,KAAK,IAAI,IAAI,OAAO,CAAC,OAAO,EAAE,IAAI,GAAG,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACxF,CAAC;AAED,IAAI,OAAO,GAAgB,IAAI,CAAC;AAEhC,KAAK,UAAU,sBAAsB;IACjC,IAAI,CAAC,uBAAuB,CAAC,EAAE,GAAG,EAAE,IAAI,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;QACzD,OAAO;IACX,CAAC;IAED,MAAM,kBAAkB,EAAE,CAAC;IAE3B,OAAO,GAAG,IAAI,IAAI,EAAE,CAAC;AACzB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,EAAE,aAAa,EAAE,IAAI,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC"}
@@ -0,0 +1,88 @@
1
+ import { DatabaseInstance } from '@stamhoofd/node-database';
2
+ const defaultMysqlPort = 3306;
3
+ export function getStatisticsDatabaseConfig() {
4
+ const config = STAMHOOFD.statisticsDatabase;
5
+ if (!config?.DB_DATABASE) {
6
+ throw new Error('STAMHOOFD.statisticsDatabase is not set: configure the platform statistics database before running migrations');
7
+ }
8
+ // The statistics migrations create tables that share their names with the main database
9
+ // (members, registrations, groups, ...). Pointed at the main database they would collide with
10
+ // it, so the one configuration that could damage it is refused outright. Each lives on its own
11
+ // server, so it takes a match on the server as well as on the name to be the same database.
12
+ const main = STAMHOOFD.stamhoofdDatabase;
13
+ if (main &&
14
+ config.DB_DATABASE === main.DB_DATABASE &&
15
+ config.DB_HOST === main.DB_HOST &&
16
+ (config.DB_PORT ?? main.DB_PORT) === main.DB_PORT) {
17
+ throw new Error(`STAMHOOFD.statisticsDatabase is the main database (${config.DB_DATABASE} on ${config.DB_HOST}): the platform statistics database has to be a separate one`);
18
+ }
19
+ return config;
20
+ }
21
+ /**
22
+ * Whether the statistics database is on a different machine than the process reading the
23
+ * administration. It is wherever this is deployed -- the schema lives next to Metabase -- and it is
24
+ * not in development or in the tests, which put both on the same MySQL.
25
+ */
26
+ function isOnAnotherServer(config) {
27
+ return config.DB_HOST !== STAMHOOFD.stamhoofdDatabase?.DB_HOST;
28
+ }
29
+ /**
30
+ * The port of a statistics database that does not name one. Undefined follows the port the process
31
+ * connects to the main database on, which is only the same server in development and in tests: on
32
+ * another server that port means nothing, so there it falls back to the MySQL default instead of
33
+ * silently reaching for whatever happens to be listening.
34
+ */
35
+ function getStatisticsPort(config) {
36
+ if (config.DB_PORT !== undefined) {
37
+ return config.DB_PORT;
38
+ }
39
+ return isOnAnotherServer(config) ? defaultMysqlPort : undefined;
40
+ }
41
+ /**
42
+ * Where to reach the statistics database. It lives next to Metabase rather than on the server this
43
+ * runs on, so every connection detail is spelled out instead of inherited from the main database.
44
+ */
45
+ export function getStatisticsPoolOptions() {
46
+ const config = getStatisticsDatabaseConfig();
47
+ const useSSL = isOnAnotherServer(config);
48
+ // The login is created with REQUIRE X509: MySQL reports a missing client certificate as a plain
49
+ // "access denied", so refuse early with a message that names the actual cause.
50
+ if (useSSL && (!config.DB_CERT || !config.DB_KEY)) {
51
+ throw new Error('STAMHOOFD.statisticsDatabase.DB_CERT and DB_KEY are required to connect to a statistics database on another server');
52
+ }
53
+ return {
54
+ host: config.DB_HOST,
55
+ user: config.DB_USER,
56
+ password: config.DB_PASS,
57
+ port: getStatisticsPort(config),
58
+ // Left off where both databases are on the same MySQL, which is development and the tests.
59
+ useSSL,
60
+ ca: useSSL ? config.DB_CA : undefined,
61
+ cert: useSSL ? config.DB_CERT : undefined,
62
+ key: useSSL ? config.DB_KEY : undefined,
63
+ };
64
+ }
65
+ let connection;
66
+ /**
67
+ * A connection of its own to the platform statistics database.
68
+ *
69
+ * The shared `Database` stays pointed at the main administration, which the sync reads through the
70
+ * models: the statistics database lives on the Metabase server, so the two are not even the same
71
+ * MySQL to point a single connection at.
72
+ */
73
+ export function getStatisticsConnection() {
74
+ connection ??= new DatabaseInstance({
75
+ ...getStatisticsPoolOptions(),
76
+ database: getStatisticsDatabaseConfig().DB_DATABASE,
77
+ });
78
+ return connection;
79
+ }
80
+ export async function endStatisticsConnection() {
81
+ if (!connection) {
82
+ return;
83
+ }
84
+ const ending = connection;
85
+ connection = undefined;
86
+ await ending.end();
87
+ }
88
+ //# sourceMappingURL=database.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.js","sourceRoot":"","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d/node-database';
2
+ import { Version } from '@stamhoofd/structures';
3
+ import { endStatisticsConnection } from './database.js';
4
+ import { runStatisticsMigrations } from './schema.js';
5
+ Column.setJSONVersion(Version);
6
+ process.env.TZ = 'UTC';
7
+ if (new Date().getTimezoneOffset() !== 0) {
8
+ throw new Error('Process should always run in UTC timezone');
9
+ }
10
+ const start = async () => {
11
+ let killSignalReceived = false;
12
+ const handler = () => {
13
+ console.error('Ignoring kill signal during migration');
14
+ killSignalReceived = true;
15
+ };
16
+ process.on('SIGTERM', handler);
17
+ process.on('SIGINT', handler);
18
+ try {
19
+ await runStatisticsMigrations();
20
+ if (killSignalReceived) {
21
+ console.error('Killing process due to received signal during migration');
22
+ process.exit(1);
23
+ }
24
+ }
25
+ finally {
26
+ process.off('SIGTERM', handler);
27
+ process.off('SIGINT', handler);
28
+ // These migrations run on the statistics database only: the main one is never connected to.
29
+ await endStatisticsConnection();
30
+ }
31
+ };
32
+ export async function run() {
33
+ await start().catch((error) => {
34
+ console.error('unhandledRejection', error);
35
+ process.exit(1);
36
+ });
37
+ }
38
+ //# sourceMappingURL=migrate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate.js","sourceRoot":"","sources":["../../src/migrate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAEtD,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,EAAE,GAAG,KAAK,CAAC;AAEvB,IAAI,IAAI,IAAI,EAAE,CAAC,iBAAiB,EAAE,KAAK,CAAC,EAAE,CAAC;IACvC,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;AACjE,CAAC;AAED,MAAM,KAA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