@stamhoofd/backend-statistics-syncer 2.153.0 → 2.161.1
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.
- package/LICENSE.md +7 -7
- package/README.md +1 -1
- package/dist/index.js +5 -8
- package/dist/index.js.map +1 -0
- package/dist/migrations.js +19 -0
- package/dist/migrations.js.map +1 -0
- package/dist/src/boot.js +75 -0
- package/dist/src/boot.js.map +1 -0
- package/dist/src/crons.js +39 -0
- package/dist/src/crons.js.map +1 -0
- package/dist/src/database.js +88 -0
- package/dist/src/database.js.map +1 -0
- package/dist/src/migrate.js +38 -0
- package/dist/src/migrate.js.map +1 -0
- package/dist/src/migrations/1785958573-platform-statistics-schema.sql +382 -0
- package/dist/src/migrations/1786203367-postal-code-coordinates.sql +1203 -0
- package/dist/src/periods.js +86 -0
- package/dist/src/periods.js.map +1 -0
- package/dist/src/rows.js +209 -0
- package/dist/src/rows.js.map +1 -0
- package/dist/src/schema.js +28 -0
- package/dist/src/schema.js.map +1 -0
- package/dist/src/sources.js +10 -0
- package/dist/src/sources.js.map +1 -0
- package/dist/src/sync-state.js +36 -0
- package/dist/src/sync-state.js.map +1 -0
- package/dist/src/sync.js +608 -0
- package/dist/src/sync.js.map +1 -0
- package/dist/src/writer.js +63 -0
- package/dist/src/writer.js.map +1 -0
- package/dist-types/index.d.ts +2 -0
- package/dist-types/index.d.ts.map +1 -0
- package/dist-types/migrations.d.ts +2 -0
- package/dist-types/migrations.d.ts.map +1 -0
- package/dist-types/src/boot.d.ts +8 -0
- package/dist-types/src/boot.d.ts.map +1 -0
- package/dist-types/src/crons.d.ts +21 -0
- package/dist-types/src/crons.d.ts.map +1 -0
- package/dist-types/src/database.d.ts +28 -0
- package/dist-types/src/database.d.ts.map +1 -0
- package/dist-types/src/migrate.d.ts +2 -0
- package/dist-types/src/migrate.d.ts.map +1 -0
- package/dist-types/src/periods.d.ts +51 -0
- package/dist-types/src/periods.d.ts.map +1 -0
- package/dist-types/src/rows.d.ts +212 -0
- package/dist-types/src/rows.d.ts.map +1 -0
- package/dist-types/src/schema.d.ts +9 -0
- package/dist-types/src/schema.d.ts.map +1 -0
- package/dist-types/src/sources.d.ts +10 -0
- package/dist-types/src/sources.d.ts.map +1 -0
- package/dist-types/src/sync-state.d.ts +20 -0
- package/dist-types/src/sync-state.d.ts.map +1 -0
- package/dist-types/src/sync.d.ts +37 -0
- package/dist-types/src/sync.d.ts.map +1 -0
- package/dist-types/src/writer.d.ts +24 -0
- package/dist-types/src/writer.d.ts.map +1 -0
- package/index.ts +5 -8
- package/migrations.ts +16 -11
- package/package.json +37 -23
- package/src/boot.ts +7 -3
- package/src/crons.test.ts +33 -8
- package/src/crons.ts +7 -1
- package/src/database.test.ts +48 -11
- package/src/database.ts +30 -7
- package/src/migrate.ts +1 -1
- package/src/periods.ts +5 -2
- package/src/rows.test.ts +96 -16
- package/src/rows.ts +28 -6
- package/src/schema.test.ts +48 -19
- package/src/schema.ts +14 -4
- package/src/sync-state.test.ts +3 -1
- package/src/sync-state.ts +13 -4
- package/src/sync.test.ts +145 -32
- package/src/sync.ts +422 -91
- package/src/writer.ts +32 -10
- package/tsconfig.build.json +4 -11
- package/tsconfig.json +1 -1
- package/tsconfig.test.json +1 -3
- package/tsconfig.types.json +7 -0
- package/.gitignore +0 -221
- package/tests/vitest.global.setup.ts +0 -29
- package/tests/vitest.setup.ts +0 -24
- package/vitest.config.js +0 -18
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
-- The platform statistics database: a de-identified copy of the member administration that Metabase
|
|
2
|
+
-- reports on. Rows are kept at their source grain and every figure is aggregated at query time, so
|
|
3
|
+
-- the tables mirror the main database: same table names, same column names, same column types.
|
|
4
|
+
--
|
|
5
|
+
-- What is left out is everything that identifies a natural person by name or lets you contact them:
|
|
6
|
+
-- `members` carries no name, no email address and no phone number, and every json column of the
|
|
7
|
+
-- source is dropped, since `details` and `recordAnswers` hold exactly that. Keep that property when
|
|
8
|
+
-- adding tables here.
|
|
9
|
+
--
|
|
10
|
+
-- Two fields on `members` are deliberate exceptions, both because a report needs them and nothing
|
|
11
|
+
-- coarser will do: the date of birth and the postal code. Together with the unit and tak a
|
|
12
|
+
-- registration already gives, and a gender, they are enough to single out a person. This database is
|
|
13
|
+
-- therefore not anonymous — treat who can reach it as part of protecting it, not only what is in it.
|
|
14
|
+
--
|
|
15
|
+
-- Three columns are not copies of anything in the source:
|
|
16
|
+
--
|
|
17
|
+
-- * `source` says which pipeline wrote a row. The nightly reconciliation checks rows against the
|
|
18
|
+
-- main database, and rows that came from an import have no counterpart there — without this it
|
|
19
|
+
-- would delete every one of them on the first night.
|
|
20
|
+
-- * `registration_periods`.`cutoffAt` freezes a period. Once it has passed, the sync stops writing
|
|
21
|
+
-- anything belonging to that period, so the numbers for that year are settled and later changes
|
|
22
|
+
-- in the administration no longer move them. It is per period because the boundary differs per
|
|
23
|
+
-- platform and per year, and it is what makes room for data imported from a client's own
|
|
24
|
+
-- statistics database: those years are frozen, so nothing overwrites them.
|
|
25
|
+
-- * `registration_periods`.`lockedAt` freezes a period the administration has locked. `locked` is
|
|
26
|
+
-- copied from there and says what is true now; this says the sync has acted on it. It is filled
|
|
27
|
+
-- at the end of a run that came through in full, the incremental pass and the delete
|
|
28
|
+
-- reconciliation both, so the changes and the deletions of the day the lock was set are written
|
|
29
|
+
-- before the period stops being followed -- a run that failed halfway leaves it empty, and the
|
|
30
|
+
-- next one reads that period again. Clearing it follows the administration back: a period
|
|
31
|
+
-- unlocked to correct something is followed once more, unless it ended more than a year ago (see
|
|
32
|
+
-- `releaseWindow` in `src/periods.ts`). Unlike a cutoff it leaves the period's own row alone,
|
|
33
|
+
-- which keeps following the administration: that row carries `locked`, so freezing it would
|
|
34
|
+
-- freeze the one field an unlock has to arrive through.
|
|
35
|
+
--
|
|
36
|
+
-- Freezing only holds if every row belongs to a period. The source keeps no history of its own: a
|
|
37
|
+
-- unit, a tak, a netwerk and a functie are all just what they are called today, and a name read now
|
|
38
|
+
-- would be the name printed against every year that unit or tak was ever counted in. So each of them
|
|
39
|
+
-- is keyed on its own id *and* a period -- one row per year, saying what that thing was in that year
|
|
40
|
+
-- -- and the sync writes them for the years still open only.
|
|
41
|
+
--
|
|
42
|
+
-- The price is that nothing can point at those tables with a foreign key: `id` no longer identifies a
|
|
43
|
+
-- row, and the period a fact belongs to is not always the period its dimension row would be found
|
|
44
|
+
-- under. The reports join them explicitly on both columns instead. Every foreign key that remains
|
|
45
|
+
-- points at `registration_periods`, which is the one table keyed on `id` alone.
|
|
46
|
+
|
|
47
|
+
CREATE TABLE `registration_periods` (
|
|
48
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
|
|
49
|
+
`startDate` datetime NOT NULL,
|
|
50
|
+
`endDate` datetime NOT NULL,
|
|
51
|
+
`locked` tinyint NOT NULL DEFAULT '0',
|
|
52
|
+
`organizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
53
|
+
`previousPeriodId` varchar(36) DEFAULT NULL,
|
|
54
|
+
`nextPeriodId` varchar(36) DEFAULT NULL,
|
|
55
|
+
`customName` varchar(200) DEFAULT NULL,
|
|
56
|
+
`name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT 'The label the reports show for this period, e.g. "2024 - 2025". Derived from customName or the dates, because a Metabase filter can only offer the values of a real column.',
|
|
57
|
+
`createdAt` datetime NOT NULL,
|
|
58
|
+
`updatedAt` datetime NOT NULL,
|
|
59
|
+
`source` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'sync' COMMENT 'Which pipeline produced this row: sync or import',
|
|
60
|
+
`cutoffAt` datetime DEFAULT NULL COMMENT 'From when this period is frozen: the sync stops writing rows belonging to it. Null means it is still live.',
|
|
61
|
+
`lockedAt` datetime DEFAULT NULL COMMENT 'When the sync settled this period after the administration locked it: from then on nothing belonging to it is written. Null means it is still followed.',
|
|
62
|
+
PRIMARY KEY (`id`),
|
|
63
|
+
KEY `organizationId` (`organizationId`),
|
|
64
|
+
KEY `previousPeriodId` (`previousPeriodId`),
|
|
65
|
+
KEY `startDate` (`startDate`),
|
|
66
|
+
KEY `name` (`name`),
|
|
67
|
+
KEY `source` (`source`),
|
|
68
|
+
KEY `cutoffAt` (`cutoffAt`),
|
|
69
|
+
KEY `lockedAt` (`lockedAt`)
|
|
70
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
71
|
+
|
|
72
|
+
-- An organization is a legal entity, not a natural person, so its name and where it is are not
|
|
73
|
+
-- personal data. `postalCode` and `city` are flattened out of the source `address` json: the report
|
|
74
|
+
-- maps units by postal code and lists them by city. The rest of the address is left behind.
|
|
75
|
+
CREATE TABLE `organizations` (
|
|
76
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
77
|
+
`name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
78
|
+
`uri` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
79
|
+
`postalCode` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
80
|
+
`city` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
81
|
+
`periodId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
82
|
+
`active` tinyint(1) NOT NULL DEFAULT '1',
|
|
83
|
+
`createdAt` datetime NOT NULL,
|
|
84
|
+
`updatedAt` datetime NOT NULL,
|
|
85
|
+
`source` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'sync' COMMENT 'Which pipeline produced this row: sync or import',
|
|
86
|
+
PRIMARY KEY (`id`,`periodId`),
|
|
87
|
+
-- The uri is unique per year rather than outright, for the same reason the primary key is.
|
|
88
|
+
UNIQUE KEY `uri` (`uri`,`periodId`) USING BTREE,
|
|
89
|
+
KEY `periodId` (`periodId`),
|
|
90
|
+
KEY `name` (`name`),
|
|
91
|
+
KEY `postalCode` (`postalCode`),
|
|
92
|
+
KEY `city` (`city`),
|
|
93
|
+
KEY `source` (`source`),
|
|
94
|
+
CONSTRAINT `organizations_ibfk_1` FOREIGN KEY (`periodId`) REFERENCES `registration_periods` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
|
|
95
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
96
|
+
|
|
97
|
+
-- Where a postal code is, so the reports can put members and units on a map.
|
|
98
|
+
--
|
|
99
|
+
-- Metabase plots a map from latitude and longitude columns, and nothing in Stamhoofd holds those:
|
|
100
|
+
-- neither the postal codes it stores nor the addresses on its members. This table is therefore not a
|
|
101
|
+
-- copy of anything and the sync never writes it — it is loaded once from a list of postal code
|
|
102
|
+
-- centres. Until it is, the map cards simply have nothing to plot.
|
|
103
|
+
--
|
|
104
|
+
-- A centre point rather than an address: two members in the same postal code get the same
|
|
105
|
+
-- coordinates, so this adds nothing to what the postal code already says about where someone lives.
|
|
106
|
+
CREATE TABLE `postal_codes` (
|
|
107
|
+
`postalCode` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
108
|
+
`city` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
109
|
+
`latitude` decimal(9,6) NOT NULL,
|
|
110
|
+
`longitude` decimal(9,6) NOT NULL,
|
|
111
|
+
PRIMARY KEY (`postalCode`)
|
|
112
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
113
|
+
|
|
114
|
+
-- The platform this database holds the statistics of.
|
|
115
|
+
--
|
|
116
|
+
-- `membershipOrganizationId` points at the organization the platform runs itself, the one that is not
|
|
117
|
+
-- a local group: the jeugdbewegingen report delivers it as the only bovenlokale
|
|
118
|
+
-- ondersteuningsstructuur. Only the pointer is kept, because what that sheet prints -- the name of
|
|
119
|
+
-- that organization in a given werkjaar -- is already in `organizations`. No foreign key on it for
|
|
120
|
+
-- the reason given above: `organizations` is keyed on an id and a period.
|
|
121
|
+
--
|
|
122
|
+
-- `reducedPriceName` is what this platform calls its lower lidgeld -- SOMkort, kansentarief -- which
|
|
123
|
+
-- is a label the platform sets and nothing else in here knows. It is the one every report prints
|
|
124
|
+
-- beside `standaardtarief`, so a member paying the lower one is named the way the koepel names it
|
|
125
|
+
-- rather than in words of this database's own.
|
|
126
|
+
--
|
|
127
|
+
-- Keyed on the id alone rather than per period, unlike the configuration below. Nothing is reported
|
|
128
|
+
-- per year out of this row: it says which platform this is, which organization is its own and what it
|
|
129
|
+
-- calls its lower tarief. Renaming that tarief therefore renames it in the settled years too, which
|
|
130
|
+
-- is a label moving rather than a figure changing.
|
|
131
|
+
CREATE TABLE `platform` (
|
|
132
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
133
|
+
`name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
134
|
+
`membershipOrganizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
135
|
+
`reducedPriceName` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT 'What this platform calls its verlaagd lidgeld, e.g. SOMkort. Null means it names none, and the reports fall back to their own wording.',
|
|
136
|
+
PRIMARY KEY (`id`),
|
|
137
|
+
KEY `membershipOrganizationId` (`membershipOrganizationId`)
|
|
138
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
139
|
+
|
|
140
|
+
-- Platform configuration that lives in json on the platform record in the main database. It is
|
|
141
|
+
-- flattened into tables here so Metabase can join and group on it.
|
|
142
|
+
CREATE TABLE `organization_tags` (
|
|
143
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
144
|
+
`periodId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
145
|
+
`name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
146
|
+
PRIMARY KEY (`id`,`periodId`),
|
|
147
|
+
KEY `periodId` (`periodId`),
|
|
148
|
+
KEY `name` (`name`),
|
|
149
|
+
CONSTRAINT `organization_tags_ibfk_1` FOREIGN KEY (`periodId`) REFERENCES `registration_periods` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
|
|
150
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
151
|
+
|
|
152
|
+
-- Which netwerk an organization belongs to, per period. The source only knows the tags it carries
|
|
153
|
+
-- now, so the sync records them against the period the organization is in; a settled year keeps the
|
|
154
|
+
-- netwerk it was recorded with while it was live, instead of moving along with the organization.
|
|
155
|
+
CREATE TABLE `_organizations_organization_tags` (
|
|
156
|
+
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
|
|
157
|
+
`organizationsId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
158
|
+
`organizationTagsId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
159
|
+
`periodId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
160
|
+
`source` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'sync' COMMENT 'Which pipeline produced this row: sync or import',
|
|
161
|
+
PRIMARY KEY (`id`),
|
|
162
|
+
-- The period before the tag, though the three together are what is unique: the netwerk cards look a
|
|
163
|
+
-- unit up per year, and a column in between would leave the year unusable to that lookup.
|
|
164
|
+
UNIQUE KEY `organizationsId,periodId,organizationTagsId` (`organizationsId`,`periodId`,`organizationTagsId`) USING BTREE,
|
|
165
|
+
KEY `organizationTagsId` (`organizationTagsId`),
|
|
166
|
+
KEY `periodId` (`periodId`),
|
|
167
|
+
KEY `source` (`source`),
|
|
168
|
+
CONSTRAINT `_organizations_organization_tags_ibfk_1` FOREIGN KEY (`periodId`) REFERENCES `registration_periods` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
|
169
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
170
|
+
|
|
171
|
+
-- The takken every organization's groups map onto.
|
|
172
|
+
--
|
|
173
|
+
-- Set it once per tak. The sync writes a row per tak per period so a new werkjaar inherits it rather than
|
|
174
|
+
-- arriving empty. Only blanks are filled, so a correction stays in the year it was made.
|
|
175
|
+
CREATE TABLE `default_age_groups` (
|
|
176
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
177
|
+
`periodId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
178
|
+
`name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
179
|
+
`minAge` int DEFAULT NULL,
|
|
180
|
+
`maxAge` int DEFAULT NULL,
|
|
181
|
+
PRIMARY KEY (`id`,`periodId`),
|
|
182
|
+
KEY `periodId` (`periodId`),
|
|
183
|
+
KEY `minAge` (`minAge`),
|
|
184
|
+
CONSTRAINT `default_age_groups_ibfk_1` FOREIGN KEY (`periodId`) REFERENCES `registration_periods` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
|
|
185
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
186
|
+
|
|
187
|
+
CREATE TABLE `platform_membership_types` (
|
|
188
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
189
|
+
`periodId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
190
|
+
`name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
191
|
+
PRIMARY KEY (`id`,`periodId`),
|
|
192
|
+
KEY `periodId` (`periodId`),
|
|
193
|
+
KEY `name` (`name`),
|
|
194
|
+
CONSTRAINT `platform_membership_types_ibfk_1` FOREIGN KEY (`periodId`) REFERENCES `registration_periods` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
|
|
195
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
196
|
+
|
|
197
|
+
-- Which functie a member holds. This is what separates leiding from the members they lead.
|
|
198
|
+
CREATE TABLE `responsibilities` (
|
|
199
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
200
|
+
`periodId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
201
|
+
`name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
202
|
+
PRIMARY KEY (`id`,`periodId`),
|
|
203
|
+
KEY `periodId` (`periodId`),
|
|
204
|
+
KEY `name` (`name`),
|
|
205
|
+
CONSTRAINT `responsibilities_ibfk_1` FOREIGN KEY (`periodId`) REFERENCES `registration_periods` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
|
|
206
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
207
|
+
|
|
208
|
+
-- `name` is flattened out of the source `settings` json, which also holds descriptions and prices
|
|
209
|
+
-- that the reports have no use for.
|
|
210
|
+
CREATE TABLE `groups` (
|
|
211
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
212
|
+
`type` varchar(36) NOT NULL DEFAULT 'Membership',
|
|
213
|
+
`name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
214
|
+
`organizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
215
|
+
`periodId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
216
|
+
`defaultAgeGroupId` varchar(36) DEFAULT NULL,
|
|
217
|
+
`status` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'Open',
|
|
218
|
+
`deletedAt` datetime DEFAULT NULL,
|
|
219
|
+
`createdAt` datetime NOT NULL,
|
|
220
|
+
`updatedAt` datetime NOT NULL,
|
|
221
|
+
`source` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'sync' COMMENT 'Which pipeline produced this row: sync or import',
|
|
222
|
+
PRIMARY KEY (`id`),
|
|
223
|
+
KEY `organizationId` (`organizationId`),
|
|
224
|
+
KEY `periodId` (`periodId`),
|
|
225
|
+
KEY `defaultAgeGroupId` (`defaultAgeGroupId`),
|
|
226
|
+
KEY `source` (`source`),
|
|
227
|
+
CONSTRAINT `groups_ibfk_1` FOREIGN KEY (`periodId`) REFERENCES `registration_periods` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
|
|
228
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
229
|
+
|
|
230
|
+
-- One row per member per period, carrying only what the reports group by: what was true of this
|
|
231
|
+
-- member in that year.
|
|
232
|
+
--
|
|
233
|
+
-- A gender, a postal code and even a birth date are answers as they stand today, and they change --
|
|
234
|
+
-- someone moves, or fills in a gender the year after. Keyed on the member alone, one year's answer
|
|
235
|
+
-- would stand in for every year that member was ever counted in, and a correction made now would
|
|
236
|
+
-- reach back into years that were settled long ago. The period is also what lets the sync leave a
|
|
237
|
+
-- settled year alone, the same way it does for every other table here.
|
|
238
|
+
--
|
|
239
|
+
-- `birthDate` and `postalCode` are the two fields here that describe a natural person. The report
|
|
240
|
+
-- charts members by age and by birth year and maps them by postal code, and an age is only exact
|
|
241
|
+
-- with the date behind it. Stored as a real date rather than the source's `varchar(10)`, so age is a
|
|
242
|
+
-- date calculation instead of string arithmetic.
|
|
243
|
+
--
|
|
244
|
+
-- Those two together with the unit and tak a registration already gives, plus a gender, are enough to
|
|
245
|
+
-- single out a person in a small tak. This database is therefore not anonymous: treat who can reach
|
|
246
|
+
-- it as part of protecting it, not only what is in it.
|
|
247
|
+
--
|
|
248
|
+
-- Members are never deleted from here, even when their source row is gone: their row is what the
|
|
249
|
+
-- facts of a settled year join to for demographics, and without it every registration it carried
|
|
250
|
+
-- drops out of that year's numbers.
|
|
251
|
+
CREATE TABLE `members` (
|
|
252
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
253
|
+
`periodId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
254
|
+
`birthDate` date DEFAULT NULL,
|
|
255
|
+
`gender` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
256
|
+
`postalCode` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
257
|
+
`organizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
258
|
+
`createdAt` datetime NOT NULL,
|
|
259
|
+
`updatedAt` datetime NOT NULL,
|
|
260
|
+
`lastRegisteredAt` datetime DEFAULT NULL,
|
|
261
|
+
`source` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'sync' COMMENT 'Which pipeline produced this row: sync or import',
|
|
262
|
+
-- The member first: the reports look one up across the years, which reads the key left to right.
|
|
263
|
+
PRIMARY KEY (`id`,`periodId`),
|
|
264
|
+
KEY `periodId` (`periodId`),
|
|
265
|
+
KEY `organizationId` (`organizationId`),
|
|
266
|
+
KEY `birthDate` (`birthDate`),
|
|
267
|
+
KEY `gender` (`gender`),
|
|
268
|
+
KEY `postalCode` (`postalCode`),
|
|
269
|
+
KEY `source` (`source`),
|
|
270
|
+
-- Restrict, like the registrations: dropping a period would take the member rows of a settled year with it.
|
|
271
|
+
CONSTRAINT `members_ibfk_1` FOREIGN KEY (`periodId`) REFERENCES `registration_periods` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
|
|
272
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
273
|
+
|
|
274
|
+
-- One row per member per group, so a member registered for several groups keeps one row per group.
|
|
275
|
+
-- Retention is answered by comparing these rows across periods, which is why they are kept at this
|
|
276
|
+
-- grain instead of being counted up front.
|
|
277
|
+
CREATE TABLE `registrations` (
|
|
278
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
279
|
+
`organizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
280
|
+
`memberId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
281
|
+
`groupId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
282
|
+
`periodId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
283
|
+
`registeredAt` datetime DEFAULT NULL COMMENT 'Date of the registration or renewal. This is null if the registration is not yet active (e.g. during payment)',
|
|
284
|
+
`startDate` datetime DEFAULT NULL,
|
|
285
|
+
`endDate` datetime DEFAULT NULL,
|
|
286
|
+
`trialUntil` datetime DEFAULT NULL,
|
|
287
|
+
`deactivatedAt` datetime DEFAULT NULL COMMENT 'Set if the registration was canceled or deactivated during the group cycle. Keep this null at the end of the group cycle.',
|
|
288
|
+
`createdAt` datetime NOT NULL,
|
|
289
|
+
`updatedAt` datetime NOT NULL,
|
|
290
|
+
`source` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'sync' COMMENT 'Which pipeline produced this row: sync or import',
|
|
291
|
+
PRIMARY KEY (`id`),
|
|
292
|
+
-- Every figure in the report starts here, at `report/includes/facts.sql`: the live registrations of
|
|
293
|
+
-- one year, and the member, groep and eenheid each of them points at. Those six columns are the
|
|
294
|
+
-- whole of what that reads, so the scan is answered from this index without touching a row. It
|
|
295
|
+
-- leads on the period because that is what the scoutsjaar filter fixes, and it stands in for a
|
|
296
|
+
-- plain index on `periodId`.
|
|
297
|
+
KEY `facts` (`periodId`,`deactivatedAt`,`registeredAt`,`memberId`,`groupId`,`organizationId`),
|
|
298
|
+
KEY `memberId` (`memberId`) USING BTREE,
|
|
299
|
+
KEY `groupId` (`groupId`),
|
|
300
|
+
KEY `organizationId` (`organizationId`),
|
|
301
|
+
KEY `registeredAt` (`registeredAt`),
|
|
302
|
+
KEY `source` (`source`),
|
|
303
|
+
CONSTRAINT `registrations_ibfk_1` FOREIGN KEY (`groupId`) REFERENCES `groups` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
304
|
+
CONSTRAINT `registrations_ibfk_2` FOREIGN KEY (`periodId`) REFERENCES `registration_periods` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
|
|
305
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
306
|
+
|
|
307
|
+
-- Which lidgeld a member holds in a period. The price columns of the source are dropped: these
|
|
308
|
+
-- reports count members, not money.
|
|
309
|
+
--
|
|
310
|
+
-- `reducedPrice` is the exception, and it is a tarief rather than a price: whether this lidgeld was
|
|
311
|
+
-- charged at the platform's lower tarief instead of the standard one. That is what the reports split
|
|
312
|
+
-- the lidgelden by -- the aansluiting a member holds says what they are aangesloten for, and says
|
|
313
|
+
-- nothing about what they paid for it.
|
|
314
|
+
--
|
|
315
|
+
-- Nothing in the source records it per lidgeld: the price is worked out from whether the member has
|
|
316
|
+
-- financiële ondersteuning or an active UITPAS at the moment it is calculated, and only the resulting
|
|
317
|
+
-- amount is stored. The sync therefore reads that same status off the member, which is the input the
|
|
318
|
+
-- price was made from. It is a status about a person and a sensitive one at that, kept here as a
|
|
319
|
+
-- single flag and no more -- see the note at the top of this file on who may reach this database.
|
|
320
|
+
CREATE TABLE `member_platform_memberships` (
|
|
321
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
|
|
322
|
+
`memberId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
323
|
+
`membershipTypeId` varchar(36) NOT NULL,
|
|
324
|
+
`reducedPrice` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Whether this lidgeld was charged at the platform reduced tarief rather than the standard one',
|
|
325
|
+
`organizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
326
|
+
`periodId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
327
|
+
`startDate` datetime NOT NULL,
|
|
328
|
+
`endDate` datetime NOT NULL,
|
|
329
|
+
`expireDate` datetime DEFAULT NULL,
|
|
330
|
+
`trialUntil` datetime DEFAULT NULL,
|
|
331
|
+
`deletedAt` datetime DEFAULT NULL,
|
|
332
|
+
`createdAt` datetime NOT NULL,
|
|
333
|
+
`updatedAt` datetime NOT NULL,
|
|
334
|
+
`source` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'sync' COMMENT 'Which pipeline produced this row: sync or import',
|
|
335
|
+
PRIMARY KEY (`id`),
|
|
336
|
+
KEY `memberId` (`memberId`),
|
|
337
|
+
KEY `membershipTypeId` (`membershipTypeId`),
|
|
338
|
+
KEY `organizationId` (`organizationId`),
|
|
339
|
+
-- The lidgeld cards ask for the memberships of one year held by a set of members, in that order.
|
|
340
|
+
KEY `periodId,memberId` (`periodId`,`memberId`),
|
|
341
|
+
KEY `source` (`source`),
|
|
342
|
+
CONSTRAINT `member_platform_memberships_ibfk_1` FOREIGN KEY (`periodId`) REFERENCES `registration_periods` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
|
343
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
344
|
+
|
|
345
|
+
-- One row per period a record runs through, which is what makes "wie had deze functie in dit jaar"
|
|
346
|
+
-- answerable: a record carries a date range rather than a year, and a functie held for three years
|
|
347
|
+
-- is held in each of them.
|
|
348
|
+
CREATE TABLE `member_responsibility_records` (
|
|
349
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
|
|
350
|
+
`periodId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
351
|
+
`memberId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
352
|
+
`groupId` varchar(36) DEFAULT NULL,
|
|
353
|
+
`organizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
354
|
+
`responsibilityId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
355
|
+
`startDate` datetime NOT NULL,
|
|
356
|
+
`endDate` datetime DEFAULT NULL,
|
|
357
|
+
`source` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'sync' COMMENT 'Which pipeline produced this row: sync or import',
|
|
358
|
+
PRIMARY KEY (`id`,`periodId`),
|
|
359
|
+
KEY `memberId,organizationId` (`memberId`,`organizationId`) USING BTREE,
|
|
360
|
+
KEY `responsibilityId` (`responsibilityId`) USING BTREE,
|
|
361
|
+
KEY `periodId` (`periodId`),
|
|
362
|
+
KEY `organizationId` (`organizationId`),
|
|
363
|
+
KEY `groupId` (`groupId`),
|
|
364
|
+
KEY `source` (`source`),
|
|
365
|
+
CONSTRAINT `member_responsibility_records_ibfk_1` FOREIGN KEY (`groupId`) REFERENCES `groups` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
366
|
+
CONSTRAINT `member_responsibility_records_ibfk_2` FOREIGN KEY (`periodId`) REFERENCES `registration_periods` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
|
|
367
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
368
|
+
|
|
369
|
+
-- Bookkeeping for the sync job. One row per synced table, so every table advances on its own and a
|
|
370
|
+
-- table that fails keeps retrying from where it got to without holding up the others.
|
|
371
|
+
--
|
|
372
|
+
-- `watermark` is the highest `updatedAt` of the source rows that made it in. The next run reads from
|
|
373
|
+
-- slightly before it (see the overlap in the sync), which is what makes a crashed or half-finished
|
|
374
|
+
-- run harmless: rows are written again rather than skipped.
|
|
375
|
+
CREATE TABLE `stats_sync_state` (
|
|
376
|
+
`tableName` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
377
|
+
`watermark` datetime DEFAULT NULL COMMENT 'Highest updatedAt of the source rows synced so far. Null means nothing was synced yet, which is also how a full backfill starts.',
|
|
378
|
+
`lastSucceededAt` datetime DEFAULT NULL COMMENT 'End of the last incremental run that completed without an error',
|
|
379
|
+
`lastReconciledAt` datetime DEFAULT NULL COMMENT 'End of the last run that reconciled deletes against the source table',
|
|
380
|
+
`updatedAt` datetime NOT NULL,
|
|
381
|
+
PRIMARY KEY (`tableName`)
|
|
382
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|