This repository has been archived by the owner on May 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrations.ts
138 lines (122 loc) · 3.19 KB
/
migrations.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
import {SqliteConnection, transaction} from './src/repository'
interface IMigration {
name: string
up: string
down: string
}
const migrations: IMigration[] = [
{
name: 'initial',
up: `
create table account (
email text not null,
address text not null,
privateKey text not null,
name text not null,
loginCookie text null,
createdAt timestamp not null default current_timestamp
);
create table recentCommands (
cmd text not null,
createdAt timestamp not null default current_timestamp
);
`,
down: `
drop table if exists account;
drop table if exists recentCommands;
`,
},
{
name: 'localVault',
up: `
create table attestationVault (
id integer primary key autoincrement,
accountId integer not null references account,
createdAt timestamp not null default current_timestamp,
data text not null
);
`,
down: `
drop table if exists attestationVault;
`,
},
{
name: 'sharedCredentials',
up: `
create table sharedCredentials (
id integer primary key autoincrement,
accountId integer not null references account,
createdAt timestamp not null default current_timestamp,
data text not null
);
`,
down: `
drop table if exists sharedCredentials;
`,
},
{
name: 'aeskey',
up: `
alter table account add column aesKey text;
`,
down: `
`,
},
{
name: 'encryptedColumn',
up: `
alter table attestationVault add column encryptedData text;
`,
down: `
`,
},
]
export async function up(file?: string, logs: boolean = true) {
const client = new SqliteConnection(file)
logs && console.log('running migrations')
await client.query(
`create table if not exists migrations (name text primary key);`
)
for (const migration of migrations) {
const [result] = await client.query<{name: string}>(
'select name from migrations where name = ?',
[migration.name]
)
if (result !== undefined) {
continue
}
logs && console.log('running ' + migration.name)
await transaction(client.file, async tx => {
await tx.query(`insert into migrations values ($1);`, [migration.name])
await tx.exec(migration.up)
})
}
}
export async function down(file?: string, logs: boolean = true) {
const client = new SqliteConnection(file)
logs && console.log('reverting migrations')
const reversed = migrations.slice().reverse()
for (const migration of reversed) {
const result = await client.query(
`select name from migrations where name = $1`,
[migration.name]
)
if (result.length === 0) {
continue
}
logs && console.log('reverting ' + migration.name)
await transaction(client.file, async tx => {
await tx.query(`delete from migrations where name = $1;`, [migration.name])
await tx.exec(migration.down)
})
}
}
process.on('unhandledRejection', reason => {
throw reason
})
if (!module.parent) {
// down('sqlite/db.sqlite3').catch(e => {
up().catch(e => {
throw e
})
}