forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1769901000000-CreateAuditEventsTable.ts
More file actions
232 lines (216 loc) · 6.15 KB
/
Copy path1769901000000-CreateAuditEventsTable.ts
File metadata and controls
232 lines (216 loc) · 6.15 KB
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
import { MigrationInterface, QueryRunner, Table, TableIndex, TableForeignKey } from 'typeorm';
export class CreateAuditEventsTable1769901000000 implements MigrationInterface {
name = 'CreateAuditEventsTable1769901000000';
public async up(queryRunner: QueryRunner): Promise<void> {
// Create the audit_events table
await queryRunner.createTable(
new Table({
name: 'audit_events',
columns: [
{
name: 'id',
type: 'uuid',
isPrimary: true,
generationStrategy: 'uuid',
default: 'uuid_generate_v4()',
},
{
name: 'action',
type: 'varchar',
length: '100',
isNullable: false,
},
{
name: 'resourceType',
type: 'varchar',
length: '50',
isNullable: false,
},
{
name: 'resourceId',
type: 'uuid',
isNullable: false,
},
{
name: 'severity',
type: 'varchar',
length: '20',
default: "'info'",
},
{
name: 'actorId',
type: 'uuid',
isNullable: true,
},
{
name: 'actorEmail',
type: 'varchar',
length: '255',
isNullable: true,
},
{
name: 'actorIp',
type: 'varchar',
length: '45',
isNullable: true,
},
{
name: 'actorUserAgent',
type: 'text',
isNullable: true,
},
{
name: 'sessionId',
type: 'uuid',
isNullable: true,
},
{
name: 'requestMetadata',
type: 'jsonb',
isNullable: true,
},
{
name: 'previousState',
type: 'jsonb',
isNullable: true,
},
{
name: 'newState',
type: 'jsonb',
isNullable: true,
},
{
name: 'description',
type: 'text',
isNullable: true,
},
{
name: 'reason',
type: 'text',
isNullable: true,
},
{
name: 'timestamp',
type: 'timestamptz',
default: 'now()',
},
{
name: 'metadata',
type: 'jsonb',
isNullable: true,
},
{
name: 'reviewed',
type: 'boolean',
default: false,
},
{
name: 'reviewedById',
type: 'uuid',
isNullable: true,
},
{
name: 'reviewedAt',
type: 'timestamptz',
isNullable: true,
},
{
name: 'reviewNote',
type: 'text',
isNullable: true,
},
],
}),
true,
);
// Enable uuid-ossp extension
await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
// Create indexes for common query patterns
await queryRunner.createIndex(
'audit_events',
new TableIndex({
name: 'IDX_audit_timestamp_action',
columnNames: ['timestamp', 'action'],
}),
);
await queryRunner.createIndex(
'audit_events',
new TableIndex({
name: 'IDX_audit_actor_timestamp',
columnNames: ['actorId', 'timestamp'],
}),
);
await queryRunner.createIndex(
'audit_events',
new TableIndex({
name: 'IDX_audit_resource',
columnNames: ['resourceType', 'resourceId'],
}),
);
await queryRunner.createIndex(
'audit_events',
new TableIndex({
name: 'IDX_audit_severity',
columnNames: ['severity'],
}),
);
await queryRunner.createIndex(
'audit_events',
new TableIndex({
name: 'IDX_audit_reviewed',
columnNames: ['reviewed'],
}),
);
// Add foreign key to users table for actor
await queryRunner.createForeignKey(
'audit_events',
new TableForeignKey({
name: 'FK_audit_actor',
columnNames: ['actorId'],
referencedTableName: 'users',
referencedColumnNames: ['id'],
onDelete: 'SET NULL',
}),
);
// Add foreign key to users table for reviewedBy
await queryRunner.createForeignKey(
'audit_events',
new TableForeignKey({
name: 'FK_audit_reviewed_by',
columnNames: ['reviewedById'],
referencedTableName: 'users',
referencedColumnNames: ['id'],
onDelete: 'SET NULL',
}),
);
// Create index for GIN on jsonb columns for searching
await queryRunner.query(`
CREATE INDEX IF NOT EXISTS IDX_audit_request_metadata_gin
ON audit_events USING gin (requestMetadata ginbtee_ops)
`);
await queryRunner.query(`
CREATE INDEX IF NOT EXISTS IDX_audit_previous_state_gin
ON audit_events USING gin (previousState ginbtee_ops)
`);
await queryRunner.query(`
CREATE INDEX IF NOT EXISTS IDX_audit_new_state_gin
ON audit_events USING gin (newState ginbtee_ops)
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
// Drop foreign keys
await queryRunner.dropForeignKey('audit_events', 'FK_audit_actor');
await queryRunner.dropForeignKey('audit_events', 'FK_audit_reviewed_by');
// Drop indexes
await queryRunner.dropIndex('audit_events', 'IDX_audit_timestamp_action');
await queryRunner.dropIndex('audit_events', 'IDX_audit_actor_timestamp');
await queryRunner.dropIndex('audit_events', 'IDX_audit_resource');
await queryRunner.dropIndex('audit_events', 'IDX_audit_severity');
await queryRunner.dropIndex('audit_events', 'IDX_audit_reviewed');
await queryRunner.query('DROP INDEX IF EXISTS IDX_audit_request_metadata_gin');
await queryRunner.query('DROP INDEX IF EXISTS IDX_audit_previous_state_gin');
await queryRunner.query('DROP INDEX IF EXISTS IDX_audit_new_state_gin');
// Drop table
await queryRunner.dropTable('audit_events');
}
}