-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathsmart-dump.ts
343 lines (293 loc) · 12.7 KB
/
smart-dump.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import '../server/env';
import { execSync } from 'child_process';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { cwd } from 'process';
import readline from 'readline';
import { Command } from 'commander';
import { round } from 'lodash';
import moment from 'moment';
import type { Sequelize } from 'sequelize';
import { Model as SequelizeModel, ModelStatic } from 'sequelize';
import { loaders } from '../server/graphql/loaders';
import { getMigrationsHash, traverse } from '../server/lib/import-export/export';
import {
mergeRecords,
MODELS_ARRAY,
populateDefaultValues,
remapPKs,
resetModelsSequences,
} from '../server/lib/import-export/import';
import { PartialRequest } from '../server/lib/import-export/types';
import logger from '../server/lib/logger';
import { md5 } from '../server/lib/utils';
import models, { sequelize } from '../server/models';
const program = new Command();
const exec = cmd => {
try {
execSync(cmd, { stdio: 'inherit' });
} catch (e) {
logger.error(e);
}
};
program.command('dump [recipe] [as_user] [env_file]').action(async (recipe, asUser, env) => {
if (!sequelize.config.username.includes('readonly')) {
logger.error('Remote must be connected with read-only user!');
process.exit(1);
} else if (!asUser) {
logger.error('as_user is required');
process.exit(1);
}
if (!recipe || (recipe && !env)) {
logger.info('Using default recipe...');
recipe = './smart-dump/dev.ts';
}
// Prepare req object
const remoteUser = await models.User.findOne({ include: [{ association: 'collective', where: { slug: asUser } }] });
if (!remoteUser) {
logger.error(`User ${asUser} not found!`);
process.exit(1);
}
await remoteUser.populateRoles();
const req: PartialRequest = { remoteUser, loaders: loaders({ remoteUser }) };
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { entries, defaultDependencies } = require(recipe);
const parsed = {};
const date = new Date().toISOString().substring(0, 10);
const hash = md5(JSON.stringify({ entries, defaultDependencies, date })).slice(0, 5);
const migrationsHash = await getMigrationsHash();
const seenModelRecords: Set<string> = new Set();
const tempDumpDir = fs.mkdtempSync(path.join(os.tmpdir(), `oc-export-${hash}`));
logger.info(`>>> Temp directory: ${tempDumpDir}`);
const gitRevision = execSync('git describe --always --abbrev=0 --match "NOT A TAG" --dirty="*"').toString().trim();
fs.writeFileSync(
`${tempDumpDir}/metadata.json`,
JSON.stringify({
gitRevision,
date,
asUser,
hash,
migrationsHash,
// eslint-disable-next-line @typescript-eslint/no-require-imports
recipe: require(recipe),
}),
);
let start = new Date();
const PG_URL = process.env.PG_URL;
logger.info('>>> Dumping Schema...');
exec(`pg_dump -csOx --if-exists "${PG_URL}" > ${tempDumpDir}/schema.sql`);
exec(
`pg_dump --schema=public --table=public.\\"SequelizeMeta\\" --data-only "${PG_URL}" >> ${tempDumpDir}/migrations.sql`,
);
logger.info(`>>> Schema Dumped in ${moment(start).fromNow(true)}`);
start = new Date();
logger.info(`>>> Dumping... to ${tempDumpDir}/data.jsonl`);
const dumpFile = fs.createWriteStream(`${tempDumpDir}/data.jsonl`);
let maxmemoryused = 0;
const clearCache = setInterval(() => {
const memoryused = round(process.memoryUsage.rss() / 1e6, 2);
maxmemoryused = Math.max(memoryused, maxmemoryused);
logger.info(
`>>> Seen ${seenModelRecords.size} records, using ${memoryused}MB of memory. Max used: ${maxmemoryused}MB`,
);
req.loaders.autogenerated.reset();
}, 30000);
for (const entry of entries) {
logger.info(`>>> Traversing DB for entry ${entries.indexOf(entry) + 1}/${entries.length}...`);
await traverse({ ...entry, defaultDependencies, parsed }, req, async ei => {
const modelRecordKey = `${ei.model}.${ei.id}`;
if (!seenModelRecords.has(modelRecordKey)) {
dumpFile.write(JSON.stringify(ei) + os.EOL);
seenModelRecords.add(modelRecordKey);
}
});
}
clearInterval(clearCache);
dumpFile.close();
logger.info(
`>>> Dumped! ${seenModelRecords.size} records in ${moment(start).fromNow(true)}, max memory used: ${maxmemoryused}MB`,
);
logger.info(`>>> Ziping export to... dbdumps/${date}.${hash}.zip`);
exec(`CUR_DIR=$PWD; cd ${tempDumpDir}; zip -r $CUR_DIR/dbdumps/${date}.${hash}.zip .; cd $CUR_DIR`);
logger.info(`>>> Done! See dbdumps/${date}.${hash}.zip`);
sequelize.close();
});
program.command('restore <file>').action(async file => {
const database = process.env.PG_DATABASE;
const rootUser = process.env.PG_USERNAME || 'postgres';
if (!database) {
logger.error('PG_DATABASE is not set!');
process.exit(1);
} else if (sequelize.config.database !== database) {
logger.error(`Sequelize is not connected to target ${database}!`);
process.exit(1);
}
const importBundleAbsolutePath = path.resolve(cwd(), file);
const tempImportDir = fs.mkdtempSync(path.join(os.tmpdir(), 'oc-import-'));
logger.info(`>>> Temp directory: ${tempImportDir}`);
exec(`CUR_DIR=$PWD; cd ${tempImportDir}; unzip ${importBundleAbsolutePath}; cd $CUR_DIR`);
const importMetadata = JSON.parse(fs.readFileSync(path.join(tempImportDir, 'metadata.json')).toString());
logger.info(
`>>> Import metadata... date: ${importMetadata.date}, hash: ${importMetadata.hash}, gitRevision: ${importMetadata.gitRevision}`,
);
let start = new Date();
logger.info('>>> Recreating DB...');
exec(`dropdb ${database}`);
exec(`createdb ${database}`);
exec(`psql -d ${database} -c 'GRANT ALL PRIVILEGES ON DATABASE ${database} TO opencollective'`);
exec(`psql -h localhost -U ${rootUser} ${database} -c 'GRANT ALL ON SCHEMA public TO opencollective'`);
exec(`psql -h localhost -U opencollective ${database} < ${tempImportDir}/schema.sql`);
exec(`psql -h localhost -U opencollective ${database} < ${tempImportDir}/migrations.sql`);
exec(`psql -h localhost -U ${rootUser} -c 'ALTER DATABASE ${database} OWNER TO opencollective'`);
logger.info(`>>> DB Created! in ${moment(start).fromNow(true)}`);
const transaction = await (sequelize as Sequelize).transaction();
const modelsArray: ModelStatic<SequelizeModel>[] = Object.values(models);
let err;
let count = 0;
try {
for (const model of modelsArray) {
logger.info(`>>> Disabling triggers on table ${model.getTableName()}`);
await sequelize.query(`ALTER TABLE "${model.getTableName()}" DISABLE TRIGGER ALL;`, { transaction });
}
logger.info(`>>> Opening file ${tempImportDir}/schema.sql`);
const dataFile = path.join(tempImportDir, 'data.jsonl');
const fileStream = fs.createReadStream(dataFile);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
start = new Date();
logger.info('>>> Inserting Data...');
for await (const line of rl) {
let row = JSON.parse(line);
// Since we use `raw: true` in `Model.create`, we need to manually set default values when needed
row = populateDefaultValues(row);
const model: ModelStatic<SequelizeModel> = models[row.model];
await model.create(row, {
transaction,
validate: false,
hooks: false,
silent: true,
logging: false,
raw: true,
ignoreDuplicates: true,
returning: false,
});
count++;
}
} catch (e) {
err = e;
} finally {
if (!err) {
logger.info(`>>> Data inserted! ${count} records in ${moment(start).fromNow(true)}`);
for (const model of modelsArray) {
logger.info(`>>> Reenabling triggers on table ${model.getTableName()}`);
await sequelize.query(`ALTER TABLE "${model.getTableName()}" ENABLE TRIGGER ALL;`, { transaction });
}
logger.info(`>>> Commiting transaction`);
await transaction.commit();
} else {
console.error(err);
logger.info(`>>> Rollback transaction`);
transaction.rollback();
}
}
logger.info('>>> Refreshing Materialized Views...');
await sequelize.query(`REFRESH MATERIALIZED VIEW "TransactionBalances"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "CollectiveBalanceCheckpoint"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "CollectiveTransactionStats"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "CollectiveTagStats"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "CollectiveOrderStats"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "ExpenseTagStats"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "HostMonthlyTransactions"`);
await resetModelsSequences(modelsArray);
logger.info('>>> Done!');
sequelize.close();
});
program.command('merge <file>').action(async file => {
const database = process.env.PG_DATABASE;
if (!database) {
logger.error('PG_DATABASE is not set!');
process.exit(1);
} else if (sequelize.config.database !== database) {
logger.error(`Sequelize is not connected to target ${database}!`);
process.exit(1);
}
const importBundleAbsolutePath = path.resolve(cwd(), file);
const tempImportDir = fs.mkdtempSync(path.join(os.tmpdir(), 'oc-import-'));
logger.info(`>>> Temp directory: ${tempImportDir}`);
exec(`CUR_DIR=$PWD; cd ${tempImportDir}; unzip ${importBundleAbsolutePath}; cd $CUR_DIR`);
const importMetadata = JSON.parse(fs.readFileSync(path.join(tempImportDir, 'metadata.json')).toString());
logger.info(
`>>> Import metadata... date: ${importMetadata.date}, hash: ${importMetadata.hash}, gitRevision: ${importMetadata.gitRevision}`,
);
const migrationsHash = await getMigrationsHash();
if (importMetadata.migrationsHash !== migrationsHash) {
logger.error(
`Migrations hash mismatch! Expected existing SequelizeMeta table hash ${migrationsHash} to match exported metadata ${importMetadata.migrationsHash}. Make sure to run migrations before importing.`,
);
process.exit(1);
}
const transaction = await (sequelize as Sequelize).transaction();
let err,
count = 0,
start;
try {
for (const model of MODELS_ARRAY) {
logger.info(`>>> Disabling triggers on table ${model.getTableName()}`);
await sequelize.query(`ALTER TABLE "${model.getTableName()}" DISABLE TRIGGER ALL;`, { transaction });
}
const dataFile = path.join(tempImportDir, 'data.jsonl');
// Create a map of indexes to use in the import
logger.info('>>> Remapping existing IDs...');
start = new Date();
const pkMap = await remapPKs(dataFile);
logger.info(`>>> Remapped IDs in ${moment(start).fromNow(true)}`);
start = new Date();
logger.info('>>> Inserting Data...');
count = await mergeRecords(dataFile, pkMap, transaction);
} catch (e) {
err = e;
} finally {
if (!err) {
logger.info(`>>> Data inserted! ${count} records in ${moment(start).fromNow(true)}`);
for (const model of MODELS_ARRAY) {
logger.info(`>>> Reenabling triggers on table ${model.getTableName()}`);
await sequelize.query(`ALTER TABLE "${model.getTableName()}" ENABLE TRIGGER ALL;`, { transaction });
}
logger.info(`>>> Commiting transaction`);
await transaction.commit();
} else {
console.error(err);
logger.info(`>>> Rollback transaction`);
transaction.rollback();
}
}
logger.info('>>> Refreshing Materialized Views...');
await sequelize.query(`REFRESH MATERIALIZED VIEW "TransactionBalances"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "CollectiveBalanceCheckpoint"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "CollectiveTransactionStats"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "CollectiveTagStats"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "CollectiveOrderStats"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "ExpenseTagStats"`);
await sequelize.query(`REFRESH MATERIALIZED VIEW "HostMonthlyTransactions"`);
logger.info('>>> Done!');
sequelize.close();
});
program.addHelpText(
'after',
`
Use this script to do partial dumps of a DB based on a recipe, to restore a DB from a dump, or to merge data into an existing DB.
Both restore and merge operations require a connection with a superuser role.
Make sure you have a zip and a compatible version of psql (pg_dump, pg_restore, createdb and dropdb) installed.
Examples (defaults to root user postgres):
To export data and DB schema:
$ npm run script scripts/smart-dump.ts dump ./smart-dump/engineering.ts superuser prod
To restore the whole DB ():
$ PG_USERNAME=postgres PG_DATABASE=opencollective_prod_snapshot npm run script scripts/smart-dump.ts restore dbdumps/2023-03-21.c5292.zip
To merge data into an existing DB:
$ PG_USERNAME=postgres PG_DATABASE=opencollective_prod_snapshot npm run script scripts/smart-dump.ts import dbdumps/2023-03-21.c5292.zip
`,
);
program.parse();