· 7 years ago · Oct 17, 2018, 04:52 PM
1class PostgresStorage {
2 /**
3 * @param {Pool} connectionPool Postgres connection pool
4 * @param {string} tableName Name of the migrations meta table
5 */
6 constructor(connectionPool, tableName) {
7 this.pool = connectionPool;
8 this.tableName = tableName;
9 }
10
11 async logMigration(migrationName) {
12 await this.init();
13
14 await this.pool.query(`
15 INSERT INTO ${this.tableName}(name)
16 VALUES ('${migrationName}')
17 ON CONFLICT DO NOTHING
18 `);
19 }
20
21 async unlogMigration(migrationName) {
22 await this.init();
23
24 await this.pool.query(`
25 DELETE FROM ${this.tableName}
26 WHERE name = '${migrationName}'
27 `);
28 }
29
30 async executed() {
31 await this.init();
32
33 const { rows } = await this.pool.query(`
34 SELECT name FROM ${this.tableName}
35 `);
36
37 return rows.map(row => row.name);
38 }
39
40 async init() {
41 await this.pool.query(`
42 CREATE TABLE IF NOT EXISTS ${this.tableName} (
43 name varchar(255) PRIMARY KEY
44 )
45 `);
46 }
47}
48
49module.exports = PostgresStorage;