@stacksjs/database 0.70.255 → 0.70.257

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.
@@ -1,1153 +1,29 @@
1
- import { log } from "@stacksjs/logging";
2
- function italic(str) {
3
- return `\x1B[3m${str}\x1B[23m`;
4
- }
5
- import { path } from "@stacksjs/path";
6
1
  import { db } from "../../utils";
7
- import { hasMigrationBeenCreated } from "../helpers";
8
- export function getTraitTables() {
2
+ import { dialectCapabilities } from "../../dialect";
3
+ import { traitTableNames } from "../../trait-tables";
4
+ import { env as envVars } from "@stacksjs/env";
5
+ function getDbDriver() {
6
+ return process.env.DB_CONNECTION || envVars.DB_CONNECTION || "sqlite";
7
+ }
8
+ export function commonTableNames() {
9
9
  return [
10
- "tags",
11
- "taggables",
12
- "categorizables",
13
- "comments",
14
- "commentables",
15
- "commentable_upvotes",
10
+ ...traitTableNames(),
16
11
  "passkeys",
17
12
  "password_resets",
18
13
  "query_logs",
19
- "migrations"
14
+ "tags",
15
+ "comments",
16
+ "categories_models",
17
+ "activities"
20
18
  ];
21
19
  }
22
- export async function createPasskeyMigration() {
23
- if (await hasMigrationBeenCreated("users"))
24
- return;
25
- let migrationContent = `import type { Database } from '@stacksjs/database'
26
- import { sql } from '@stacksjs/database'
27
-
28
- `;
29
- migrationContent += `export async function up(db: Database<any>) {
30
- `;
31
- migrationContent += ` await db.schema
32
- `;
33
- migrationContent += ` .createTable('passkeys')
34
- `;
35
- migrationContent += ` .addColumn('id', 'text')
36
- `;
37
- migrationContent += ` .addColumn('cred_public_key', 'text')
38
- `;
39
- migrationContent += ` .addColumn('webauthn_user_id', 'varchar(255)')
40
- `;
41
- migrationContent += ` .addColumn('counter', 'integer', col => col.defaultTo(0))
42
- `;
43
- migrationContent += ` .addColumn('device_type', 'varchar(255)')
44
- `;
45
- migrationContent += ` .addColumn('credential_type', 'varchar(255)')
46
- `;
47
- migrationContent += ` .addColumn('backup_eligible', 'boolean', col => col.defaultTo(false))
48
- `;
49
- migrationContent += ` .addColumn('backup_status', 'boolean', col => col.defaultTo(false))
50
- `;
51
- migrationContent += ` .addColumn('transports', 'varchar(255)')
52
- `;
53
- migrationContent += ` .addColumn('last_used_at', 'text')
54
- `;
55
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
56
- `;
57
- migrationContent += ` .execute()
58
- `;
59
- migrationContent += ` }
60
- `;
61
- const migrationFileName = `${new Date().getTime().toString()}-create-passkeys-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
62
- await Bun.write(migrationFilePath, migrationContent);
63
- log.success(`Created migration: ${italic(migrationFileName)}`);
64
- }
65
- export async function createPostgresPasskeyMigration() {
66
- if (await hasMigrationBeenCreated("users"))
67
- return;
68
- let migrationContent = `import type { Database } from '@stacksjs/database'
69
- import { sql } from '@stacksjs/database'
70
-
71
- `;
72
- migrationContent += `export async function up(db: Database<any>) {
73
- `;
74
- migrationContent += ` await db.schema
75
- `;
76
- migrationContent += ` .createTable('passkeys')
77
- `;
78
- migrationContent += ` .addColumn('id', 'uuid', col => col.primaryKey())
79
- `;
80
- migrationContent += ` .addColumn('cred_public_key', 'text')
81
- `;
82
- migrationContent += ` .addColumn('webauthn_user_id', 'varchar(255)')
83
- `;
84
- migrationContent += ` .addColumn('counter', 'integer', col => col.defaultTo(0))
85
- `;
86
- migrationContent += ` .addColumn('device_type', 'varchar(255)')
87
- `;
88
- migrationContent += ` .addColumn('credential_type', 'varchar(255)')
89
- `;
90
- migrationContent += ` .addColumn('backup_eligible', 'boolean', col => col.defaultTo(false))
91
- `;
92
- migrationContent += ` .addColumn('backup_status', 'boolean', col => col.defaultTo(false))
93
- `;
94
- migrationContent += ` .addColumn('transports', 'varchar(255)')
95
- `;
96
- migrationContent += ` .addColumn('last_used_at', 'timestamp')
97
- `;
98
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
99
- `;
100
- migrationContent += ` .execute()
101
-
102
- `;
103
- migrationContent += `}
104
- `;
105
- const migrationFileName = `${new Date().getTime().toString()}-create-passkeys-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
106
- await Bun.write(migrationFilePath, migrationContent);
107
- log.success(`Created migration: ${italic(migrationFileName)}`);
108
- }
109
- export async function createTaggableTable() {
110
- if (await hasMigrationBeenCreated("tags"))
111
- return;
112
- let migrationContent = `import type { Database } from '@stacksjs/database'
113
- `;
114
- migrationContent += `import { sql } from '@stacksjs/database'
115
-
116
- `;
117
- migrationContent += `export async function up(db: Database<any>) {
118
- `;
119
- migrationContent += ` await db.schema
120
- `;
121
- migrationContent += ` .createTable('tags')
122
- `;
123
- migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
124
- `;
125
- migrationContent += ` .addColumn('name', 'varchar(255)', col => col.notNull())
126
- `;
127
- migrationContent += ` .addColumn('slug', 'varchar(255)', col => col.notNull().unique())
128
- `;
129
- migrationContent += ` .addColumn('type', 'varchar(255)')
130
- `;
131
- migrationContent += ` .addColumn('color', 'varchar(255)')
132
- `;
133
- migrationContent += ` .addColumn('description', 'text')
134
- `;
135
- migrationContent += ` .addColumn('is_active', 'boolean', col => col.defaultTo(true))
136
- `;
137
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
138
- `;
139
- migrationContent += ` .addColumn('updated_at', 'timestamp')
140
- `;
141
- migrationContent += ` .execute()
142
-
143
- `;
144
- migrationContent += ` await db.schema
145
- `;
146
- migrationContent += ` .createIndex('idx_tags_slug')
147
- `;
148
- migrationContent += ` .on('tags')
149
- `;
150
- migrationContent += ` .column('slug')
151
- `;
152
- migrationContent += ` .execute()
153
-
154
- `;
155
- migrationContent += ` await db.schema
156
- `;
157
- migrationContent += ` .createIndex('idx_tags_type')
158
- `;
159
- migrationContent += ` .on('tags')
160
- `;
161
- migrationContent += ` .column('type')
162
- `;
163
- migrationContent += ` .execute()
164
-
165
- `;
166
- migrationContent += ` await db.schema
167
- `;
168
- migrationContent += ` .createIndex('idx_tags_name')
169
- `;
170
- migrationContent += ` .on('tags')
171
- `;
172
- migrationContent += ` .column('name')
173
- `;
174
- migrationContent += ` .execute()
175
-
176
- `;
177
- migrationContent += `}
178
- `;
179
- const migrationFileName = `${new Date().getTime().toString()}-create-tags-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
180
- await Bun.write(migrationFilePath, migrationContent);
181
- log.success(`Created migration: ${italic(migrationFileName)}`);
182
- await new Promise((resolve) => setTimeout(resolve, 2));
183
- await createTaggablesTable();
184
- }
185
- export async function createPostgresTagsTable() {
186
- if (await hasMigrationBeenCreated("tags"))
187
- return;
188
- let migrationContent = `import type { Database } from '@stacksjs/database'
189
- `;
190
- migrationContent += `import { sql } from '@stacksjs/database'
191
-
192
- `;
193
- migrationContent += `export async function up(db: Database<any>) {
194
- `;
195
- migrationContent += ` await db.schema
196
- `;
197
- migrationContent += ` .createTable('tags')
198
- `;
199
- migrationContent += ` .addColumn('id', 'serial', col => col.primaryKey())
200
- `;
201
- migrationContent += ` .addColumn('name', 'varchar(255)', col => col.notNull())
202
- `;
203
- migrationContent += ` .addColumn('slug', 'varchar(255)', col => col.notNull().unique())
204
- `;
205
- migrationContent += ` .addColumn('type', 'varchar(255)')
206
- `;
207
- migrationContent += ` .addColumn('color', 'varchar(255)')
208
- `;
209
- migrationContent += ` .addColumn('description', 'text')
210
- `;
211
- migrationContent += ` .addColumn('is_active', 'boolean', col => col.defaultTo(true))
212
- `;
213
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
214
- `;
215
- migrationContent += ` .addColumn('updated_at', 'timestamp')
216
- `;
217
- migrationContent += ` .execute()
218
-
219
- `;
220
- migrationContent += ` await db.schema
221
- `;
222
- migrationContent += ` .createIndex('idx_tags_slug')
223
- `;
224
- migrationContent += ` .on('tags')
225
- `;
226
- migrationContent += ` .column('slug')
227
- `;
228
- migrationContent += ` .execute()
229
-
230
- `;
231
- migrationContent += ` await db.schema
232
- `;
233
- migrationContent += ` .createIndex('idx_tags_type')
234
- `;
235
- migrationContent += ` .on('tags')
236
- `;
237
- migrationContent += ` .column('type')
238
- `;
239
- migrationContent += ` .execute()
240
-
241
- `;
242
- migrationContent += ` await db.schema
243
- `;
244
- migrationContent += ` .createIndex('idx_tags_name')
245
- `;
246
- migrationContent += ` .on('tags')
247
- `;
248
- migrationContent += ` .column('name')
249
- `;
250
- migrationContent += ` .execute()
251
-
252
- `;
253
- migrationContent += `}
254
- `;
255
- const migrationFileName = `${new Date().getTime().toString()}-create-tags-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
256
- await Bun.write(migrationFilePath, migrationContent);
257
- log.success(`Created migration: ${italic(migrationFileName)}`);
258
- await new Promise((resolve) => setTimeout(resolve, 2));
259
- await createPostgresTaggablesTable();
260
- }
261
- export async function createCategorizableTable() {
262
- if (await hasMigrationBeenCreated("categorizable"))
263
- return;
264
- let migrationContent = `import type { Database } from '@stacksjs/database'
265
- `;
266
- migrationContent += `import { sql } from '@stacksjs/database'
267
-
268
- `;
269
- migrationContent += `export async function up(db: Database<any>) {
270
- `;
271
- migrationContent += ` await db.schema
272
- `;
273
- migrationContent += ` .createTable('categorizables')
274
- `;
275
- migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
276
- `;
277
- migrationContent += ` .addColumn('name', 'varchar(255)', col => col.notNull())
278
- `;
279
- migrationContent += ` .addColumn('slug', 'varchar(255)', col => col.notNull().unique())
280
- `;
281
- migrationContent += ` .addColumn('description', 'text')
282
- `;
283
- migrationContent += ` .addColumn('is_active', 'boolean', col => col.defaultTo(true))
284
- `;
285
- migrationContent += ` .addColumn('categorizable_type', 'varchar(255)', col => col.notNull())
286
- `;
287
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
288
- `;
289
- migrationContent += ` .addColumn('updated_at', 'timestamp')
290
- `;
291
- migrationContent += ` .execute()
292
-
293
- `;
294
- migrationContent += ` await db.schema
295
- `;
296
- migrationContent += ` .createIndex('categorizables_id_index')
297
- `;
298
- migrationContent += ` .on('categorizables')
299
- `;
300
- migrationContent += ` .column('id')
301
- `;
302
- migrationContent += ` .execute()
303
-
304
- `;
305
- migrationContent += ` await db.schema
306
- `;
307
- migrationContent += ` .createIndex('categorizables_slug_index')
308
- `;
309
- migrationContent += ` .on('categorizables')
310
- `;
311
- migrationContent += ` .column('slug')
312
- `;
313
- migrationContent += ` .execute()
314
-
315
- `;
316
- migrationContent += ` await db.schema
317
- `;
318
- migrationContent += ` .createIndex('categorizables_polymorphic_index')
319
- `;
320
- migrationContent += ` .on('categorizables')
321
- `;
322
- migrationContent += ` .columns(['categorizable_type'])
323
- `;
324
- migrationContent += ` .execute()
325
-
326
- `;
327
- migrationContent += ` await db.schema
328
- `;
329
- migrationContent += ` .createIndex('categorizables_is_active_index')
330
- `;
331
- migrationContent += ` .on('categorizables')
332
- `;
333
- migrationContent += ` .column('is_active')
334
- `;
335
- migrationContent += ` .execute()
336
- `;
337
- migrationContent += `}
338
- `;
339
- const migrationFileName = `${new Date().getTime().toString()}-create-categorizables-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
340
- await Bun.write(migrationFilePath, migrationContent);
341
- log.success(`Created migration: ${italic(migrationFileName)}`);
342
- }
343
- export async function createPostgresCategorizableTable() {
344
- if (await hasMigrationBeenCreated("categorizable"))
345
- return;
346
- let migrationContent = `import type { Database } from '@stacksjs/database'
347
- `;
348
- migrationContent += `import { sql } from '@stacksjs/database'
349
-
350
- `;
351
- migrationContent += `export async function up(db: Database<any>) {
352
- `;
353
- migrationContent += ` await db.schema
354
- `;
355
- migrationContent += ` .createTable('categorizables')
356
- `;
357
- migrationContent += ` .addColumn('id', 'serial', col => col.primaryKey())
358
- `;
359
- migrationContent += ` .addColumn('name', 'varchar(255)', col => col.notNull())
360
- `;
361
- migrationContent += ` .addColumn('slug', 'varchar(255)', col => col.notNull().unique())
362
- `;
363
- migrationContent += ` .addColumn('description', 'text')
364
- `;
365
- migrationContent += ` .addColumn('order', 'integer', col => col.defaultTo(0))
366
- `;
367
- migrationContent += ` .addColumn('is_active', 'boolean', col => col.defaultTo(true))
368
- `;
369
- migrationContent += ` .addColumn('categorizable_type', 'varchar(255)', col => col.notNull())
370
- `;
371
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
372
- `;
373
- migrationContent += ` .addColumn('updated_at', 'timestamp')
374
- `;
375
- migrationContent += ` .execute()
376
-
377
- `;
378
- migrationContent += ` await db.schema
379
- `;
380
- migrationContent += ` .createIndex('idx_categorizables_slug')
381
- `;
382
- migrationContent += ` .on('categorizables')
383
- `;
384
- migrationContent += ` .column('slug')
385
- `;
386
- migrationContent += ` .execute()
387
-
388
- `;
389
- migrationContent += ` await db.schema
390
- `;
391
- migrationContent += ` .createIndex('idx_categorizables_polymorphic')
392
- `;
393
- migrationContent += ` .on('categorizables')
394
- `;
395
- migrationContent += ` .columns(['categorizable_type'])
396
- `;
397
- migrationContent += ` .execute()
398
-
399
- `;
400
- migrationContent += ` await db.schema
401
- `;
402
- migrationContent += ` .createIndex('idx_categorizables_order')
403
- `;
404
- migrationContent += ` .on('categorizables')
405
- `;
406
- migrationContent += ` .column('order')
407
- `;
408
- migrationContent += ` .execute()
409
-
410
- `;
411
- migrationContent += ` await db.schema
412
- `;
413
- migrationContent += ` .createIndex('idx_categorizables_is_active')
414
- `;
415
- migrationContent += ` .on('categorizables')
416
- `;
417
- migrationContent += ` .column('is_active')
418
- `;
419
- migrationContent += ` .execute()
420
- `;
421
- migrationContent += `}
422
- `;
423
- const migrationFileName = `${new Date().getTime().toString()}-create-categorizables-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
424
- await Bun.write(migrationFilePath, migrationContent);
425
- log.success(`Created migration: ${italic(migrationFileName)}`);
426
- }
427
- export async function createCommentablesTable(options = {}) {
428
- if (await hasMigrationBeenCreated("comments"))
429
- return;
430
- let migrationContent = `import type { Database } from '@stacksjs/database'
431
- `;
432
- migrationContent += `import { sql } from '@stacksjs/database'
433
-
434
- `;
435
- migrationContent += `export async function up(db: Database<any>) {
436
- `;
437
- migrationContent += ` await db.schema
438
- `;
439
- migrationContent += ` .createTable('comments')
440
- `;
441
- migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
442
- `;
443
- migrationContent += ` .addColumn('title', 'varchar(255)', col => col.notNull())
444
- `;
445
- migrationContent += ` .addColumn('body', 'text', col => col.notNull())
446
- `;
447
- migrationContent += ` .addColumn('content', 'text')
448
- `;
449
- migrationContent += ` .addColumn('status', 'varchar(50)', col => col.notNull().defaultTo('${options.requiresApproval ? "pending" : "approved"}'))
450
- `;
451
- migrationContent += ` .addColumn('author_name', 'varchar(100)')
452
- `;
453
- migrationContent += ` .addColumn('author_email', 'varchar(255)')
454
- `;
455
- migrationContent += ` .addColumn('post_title', 'varchar(255)')
456
- `;
457
- migrationContent += ` .addColumn('commentables_id', 'integer', col => col.notNull())
458
- `;
459
- migrationContent += ` .addColumn('commentables_type', 'varchar(255)', col => col.notNull())
460
- `;
461
- migrationContent += ` .addColumn('ip_address', 'varchar(45)')
462
- `;
463
- migrationContent += ` .addColumn('user_agent', 'varchar(500)')
464
- `;
465
- migrationContent += ` .addColumn('is_approved', 'integer', col => col.defaultTo(0))
466
- `;
467
- migrationContent += ` .addColumn('approved_at', 'integer')
468
- `;
469
- migrationContent += ` .addColumn('rejected_at', 'integer')
470
- `;
471
- if (options.reportable) {
472
- migrationContent += ` .addColumn('reports_count', 'integer', col => col.defaultTo(0))
473
- `;
474
- migrationContent += ` .addColumn('reported_at', 'integer')
475
- `;
476
- }
477
- if (options.requiresAuth)
478
- migrationContent += ` .addColumn('user_id', 'integer', col => col.notNull())
479
- `;
480
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
481
- `;
482
- migrationContent += ` .addColumn('updated_at', 'timestamp')
483
- `;
484
- migrationContent += ` .execute()
485
-
486
- `;
487
- migrationContent += ` await db.schema.createIndex('idx_comments_status').on('comments').column('status').execute()
488
- `;
489
- migrationContent += ` await db.schema.createIndex('idx_comments_created_at').on('comments').column('created_at').execute()
490
- `;
491
- migrationContent += `}
492
- `;
493
- const migrationFileName = `${new Date().getTime().toString()}-create-comments-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
494
- await Bun.write(migrationFilePath, migrationContent);
495
- log.success(`Created migration: ${italic(migrationFileName)}`);
496
- }
497
- export async function createPostgresCommentsTable() {
498
- if (await hasMigrationBeenCreated("comments"))
499
- return;
500
- let migrationContent = `import type { Database } from '@stacksjs/database'
501
- `;
502
- migrationContent += `import { sql } from '@stacksjs/database'
503
-
504
- `;
505
- migrationContent += `export async function up(db: Database<any>) {
506
- `;
507
- migrationContent += ` await db.schema
508
- `;
509
- migrationContent += ` .createTable('comments')
510
- `;
511
- migrationContent += ` .addColumn('id', 'serial', col => col.primaryKey())
512
- `;
513
- migrationContent += ` .addColumn('title', 'varchar(255)', col => col.notNull())
514
- `;
515
- migrationContent += ` .addColumn('body', 'text', col => col.notNull())
516
- `;
517
- migrationContent += ` .addColumn('content', 'text')
518
- `;
519
- migrationContent += ` .addColumn('commentables_id', 'integer', col => col.notNull())
520
- `;
521
- migrationContent += ` .addColumn('commentables_type', 'varchar(255)', col => col.notNull())
522
- `;
523
- migrationContent += ` .addColumn('status', 'varchar(50)', col => col.notNull().defaultTo('pending'))
524
- `;
525
- migrationContent += ` .addColumn('author_name', 'varchar(100)')
526
- `;
527
- migrationContent += ` .addColumn('author_email', 'varchar(255)')
528
- `;
529
- migrationContent += ` .addColumn('post_title', 'varchar(255)')
530
- `;
531
- migrationContent += ` .addColumn('ip_address', 'varchar(45)')
532
- `;
533
- migrationContent += ` .addColumn('user_agent', 'varchar(500)')
534
- `;
535
- migrationContent += ` .addColumn('is_approved', 'integer', col => col.defaultTo(0))
536
- `;
537
- migrationContent += ` .addColumn('approved_at', 'integer')
538
- `;
539
- migrationContent += ` .addColumn('rejected_at', 'integer')
540
- `;
541
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
542
- `;
543
- migrationContent += ` .addColumn('updated_at', 'timestamp')
544
- `;
545
- migrationContent += ` .execute()
546
-
547
- `;
548
- migrationContent += ` await db.schema.createIndex('idx_comments_status').on('comments').column('status').execute()
549
- `;
550
- migrationContent += ` await db.schema.createIndex('idx_comments_created_at').on('comments').column('created_at').execute()
551
- `;
552
- migrationContent += `}
553
- `;
554
- const migrationFileName = `${new Date().getTime().toString()}-create-comments-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
555
- await Bun.write(migrationFilePath, migrationContent);
556
- log.success(`Created migration: ${italic(migrationFileName)}`);
557
- }
558
20
  export async function dropCommonTables() {
559
- await db.unsafe("DROP TABLE IF EXISTS passkeys").execute();
560
- await db.unsafe("DROP TABLE IF EXISTS password_resets").execute();
561
- await db.unsafe("DROP TABLE IF EXISTS query_logs").execute();
562
- await db.unsafe("DROP TABLE IF EXISTS categorizables").execute();
563
- await db.unsafe("DROP TABLE IF EXISTS commentable_upvotes").execute();
564
- await db.unsafe("DROP TABLE IF EXISTS tags").execute();
565
- await db.unsafe("DROP TABLE IF EXISTS taggables").execute();
566
- await db.unsafe("DROP TABLE IF EXISTS categorizable_models").execute();
567
- await db.unsafe("DROP TABLE IF EXISTS commentables").execute();
568
- await db.unsafe("DROP TABLE IF EXISTS comments").execute();
569
- await db.unsafe("DROP TABLE IF EXISTS categories_models").execute();
570
- await db.unsafe("DROP TABLE IF EXISTS activities").execute();
571
- }
572
- export async function truncateMigrationTables() {
573
- await db.unsafe("DELETE FROM migrations").execute();
574
- await db.unsafe("DELETE FROM migration_locks").execute();
21
+ const { identifierQuote: q, wire } = dialectCapabilities(getDbDriver()), cascade = wire === "postgres" ? " CASCADE" : "";
22
+ for (const table of commonTableNames())
23
+ await db.unsafe(`DROP TABLE IF EXISTS ${q}${table}${q}${cascade}`).execute();
575
24
  }
576
25
  export async function dropMigrationTables() {
577
- await db.unsafe("DROP TABLE IF EXISTS migrations").execute();
578
- await db.unsafe("DROP TABLE IF EXISTS migration_locks").execute();
579
- }
580
- export async function createCommentUpvoteMigration() {
581
- if (await hasMigrationBeenCreated("commentable_upvotes"))
582
- return;
583
- let migrationContent = `import type { Database } from '@stacksjs/database'
584
- import { sql } from '@stacksjs/database'
585
-
586
- `;
587
- migrationContent += `export async function up(db: Database<any>) {
588
- `;
589
- migrationContent += ` await db.schema
590
- `;
591
- migrationContent += ` .createTable('commentable_upvotes')
592
- `;
593
- migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
594
- `;
595
- migrationContent += ` .addColumn('upvoteable_id', 'integer', col => col.notNull())
596
- `;
597
- migrationContent += ` .addColumn('upvoteable_type', 'varchar(255)', col => col.notNull())
598
- `;
599
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
600
- `;
601
- migrationContent += ` .execute()
602
-
603
- `;
604
- migrationContent += ` await db.schema.createIndex('idx_commentable_upvotes_upvoteable').on('commentable_upvotes').columns(['upvoteable_id', 'upvoteable_type']).execute()
605
- `;
606
- migrationContent += `}
607
- `;
608
- const migrationFileName = `${new Date().getTime().toString()}-create-commentable_upvotes-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
609
- await Bun.write(migrationFilePath, migrationContent);
610
- log.success(`Created migration: ${italic(migrationFileName)}`);
611
- }
612
- export async function createPostgresCommentUpvoteMigration() {
613
- if (await hasMigrationBeenCreated("commentable_upvotes"))
614
- return;
615
- let migrationContent = `import type { Database } from '@stacksjs/database'
616
- import { sql } from '@stacksjs/database'
617
-
618
- `;
619
- migrationContent += `export async function up(db: Database<any>) {
620
- `;
621
- migrationContent += ` await db.schema
622
- `;
623
- migrationContent += ` .createTable('commentable_upvotes')
624
- `;
625
- migrationContent += ` .addColumn('id', 'serial', col => col.primaryKey())
626
- `;
627
- migrationContent += ` .addColumn('upvoteable_id', 'integer', col => col.notNull())
628
- `;
629
- migrationContent += ` .addColumn('upvoteable_type', 'varchar(255)', col => col.notNull())
630
- `;
631
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
632
- `;
633
- migrationContent += ` .execute()
634
-
635
- `;
636
- migrationContent += ` await db.schema.createIndex('idx_commentable_upvotes_upvoteable').on('commentable_upvotes').columns(['upvoteable_id', 'upvoteable_type']).execute()
637
- `;
638
- migrationContent += `}
639
- `;
640
- const migrationFileName = `${new Date().getTime().toString()}-create-commentable_upvotes-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
641
- await Bun.write(migrationFilePath, migrationContent);
642
- log.success(`Created migration: ${italic(migrationFileName)}`);
643
- }
644
- export async function createCommentablesPivotTable() {
645
- if (await hasMigrationBeenCreated("commentables"))
646
- return;
647
- let migrationContent = `import type { Database } from '@stacksjs/database'
648
- `;
649
- migrationContent += `import { sql } from '@stacksjs/database'
650
-
651
- `;
652
- migrationContent += `export async function up(db: Database<any>) {
653
- `;
654
- migrationContent += ` await db.schema
655
- `;
656
- migrationContent += ` .createTable('commentables')
657
- `;
658
- migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
659
- `;
660
- migrationContent += ` .addColumn('comment_id', 'integer', col => col.notNull())
661
- `;
662
- migrationContent += ` .addColumn('commentable_id', 'integer', col => col.notNull())
663
- `;
664
- migrationContent += ` .addColumn('commentable_type', 'varchar(255)', col => col.notNull())
665
- `;
666
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
667
- `;
668
- migrationContent += ` .addColumn('updated_at', 'timestamp')
669
- `;
670
- migrationContent += ` .execute()
671
-
672
- `;
673
- migrationContent += ` await db.schema
674
- `;
675
- migrationContent += ` .createIndex('idx_commentables_comment')
676
- `;
677
- migrationContent += ` .on('commentables')
678
- `;
679
- migrationContent += ` .column('comment_id')
680
- `;
681
- migrationContent += ` .execute()
682
-
683
- `;
684
- migrationContent += ` await db.schema
685
- `;
686
- migrationContent += ` .createIndex('idx_commentables_polymorphic')
687
- `;
688
- migrationContent += ` .on('commentables')
689
- `;
690
- migrationContent += ` .columns(['commentable_id', 'commentable_type'])
691
- `;
692
- migrationContent += ` .execute()
693
-
694
- `;
695
- migrationContent += ` await db.schema
696
- `;
697
- migrationContent += ` .createIndex('idx_commentables_unique')
698
- `;
699
- migrationContent += ` .on('commentables')
700
- `;
701
- migrationContent += ` .columns(['comment_id', 'commentable_id', 'commentable_type'])
702
- `;
703
- migrationContent += ` .unique()
704
- `;
705
- migrationContent += ` .execute()
706
-
707
- `;
708
- migrationContent += `}
709
- `;
710
- const migrationFileName = `${new Date().getTime().toString()}-create-commentables-pivot-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
711
- await Bun.write(migrationFilePath, migrationContent);
712
- log.success(`Created migration: ${italic(migrationFileName)}`);
713
- }
714
- export async function createPostgresCommentablesPivotTable() {
715
- if (await hasMigrationBeenCreated("commentables_pivot"))
716
- return;
717
- let migrationContent = `import type { Database } from '@stacksjs/database'
718
- `;
719
- migrationContent += `import { sql } from '@stacksjs/database'
720
-
721
- `;
722
- migrationContent += `export async function up(db: Database<any>) {
723
- `;
724
- migrationContent += ` await db.schema
725
- `;
726
- migrationContent += ` .createTable('commentables')
727
- `;
728
- migrationContent += ` .addColumn('id', 'serial', col => col.primaryKey())
729
- `;
730
- migrationContent += ` .addColumn('comment_id', 'integer', col => col.notNull())
731
- `;
732
- migrationContent += ` .addColumn('commentable_id', 'integer', col => col.notNull())
733
- `;
734
- migrationContent += ` .addColumn('commentable_type', 'varchar(255)', col => col.notNull())
735
- `;
736
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
737
- `;
738
- migrationContent += ` .addColumn('updated_at', 'timestamp')
739
- `;
740
- migrationContent += ` .execute()
741
-
742
- `;
743
- migrationContent += ` await db.schema
744
- `;
745
- migrationContent += ` .alterTable('commentables')
746
- `;
747
- migrationContent += ` .addForeignKeyConstraint('commentables_comment_id_foreign', ['comment_id'], 'comments', ['id'], (cb) => cb.onDelete('cascade'))
748
- `;
749
- migrationContent += ` .execute()
750
-
751
- `;
752
- migrationContent += ` await db.schema
753
- `;
754
- migrationContent += ` .createIndex('idx_commentables_comment')
755
- `;
756
- migrationContent += ` .on('commentables')
757
- `;
758
- migrationContent += ` .column('comment_id')
759
- `;
760
- migrationContent += ` .execute()
761
-
762
- `;
763
- migrationContent += ` await db.schema
764
- `;
765
- migrationContent += ` .createIndex('idx_commentables_polymorphic')
766
- `;
767
- migrationContent += ` .on('commentables')
768
- `;
769
- migrationContent += ` .columns(['commentable_id', 'commentable_type'])
770
- `;
771
- migrationContent += ` .execute()
772
-
773
- `;
774
- migrationContent += ` await db.schema
775
- `;
776
- migrationContent += ` .createIndex('idx_commentables_unique')
777
- `;
778
- migrationContent += ` .on('commentables')
779
- `;
780
- migrationContent += ` .columns(['comment_id', 'commentable_id', 'commentable_type'])
781
- `;
782
- migrationContent += ` .unique()
783
- `;
784
- migrationContent += ` .execute()
785
-
786
- `;
787
- migrationContent += `}
788
- `;
789
- const migrationFileName = `${new Date().getTime().toString()}-create-commentables-pivot-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
790
- await Bun.write(migrationFilePath, migrationContent);
791
- log.success(`Created migration: ${italic(migrationFileName)}`);
792
- }
793
- export async function createTaggablesTable() {
794
- if (await hasMigrationBeenCreated("taggables"))
795
- return;
796
- let migrationContent = `import type { Database } from '@stacksjs/database'
797
- `;
798
- migrationContent += `import { sql } from '@stacksjs/database'
799
-
800
- `;
801
- migrationContent += `export async function up(db: Database<any>) {
802
- `;
803
- migrationContent += ` await db.schema
804
- `;
805
- migrationContent += ` .createTable('taggables')
806
- `;
807
- migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
808
- `;
809
- migrationContent += ` .addColumn('tag_id', 'integer', col => col.notNull())
810
- `;
811
- migrationContent += ` .addColumn('taggable_id', 'integer', col => col.notNull())
812
- `;
813
- migrationContent += ` .addColumn('taggable_type', 'varchar(255)', col => col.notNull())
814
- `;
815
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
816
- `;
817
- migrationContent += ` .addColumn('updated_at', 'timestamp')
818
- `;
819
- migrationContent += ` .execute()
820
-
821
- `;
822
- migrationContent += ` await db.schema
823
- `;
824
- migrationContent += ` .createIndex('idx_taggables_tag')
825
- `;
826
- migrationContent += ` .on('taggables')
827
- `;
828
- migrationContent += ` .column('tag_id')
829
- `;
830
- migrationContent += ` .execute()
831
-
832
- `;
833
- migrationContent += ` await db.schema
834
- `;
835
- migrationContent += ` .createIndex('idx_taggables_polymorphic')
836
- `;
837
- migrationContent += ` .on('taggables')
838
- `;
839
- migrationContent += ` .columns(['taggable_id', 'taggable_type'])
840
- `;
841
- migrationContent += ` .execute()
842
-
843
- `;
844
- migrationContent += ` await db.schema
845
- `;
846
- migrationContent += ` .createIndex('idx_taggables_unique')
847
- `;
848
- migrationContent += ` .on('taggables')
849
- `;
850
- migrationContent += ` .columns(['tag_id', 'taggable_id', 'taggable_type'])
851
- `;
852
- migrationContent += ` .unique()
853
- `;
854
- migrationContent += ` .execute()
855
-
856
- `;
857
- migrationContent += `}
858
- `;
859
- const migrationFileName = `${new Date().getTime().toString()}-create-taggables-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
860
- await Bun.write(migrationFilePath, migrationContent);
861
- log.success(`Created migration: ${italic(migrationFileName)}`);
862
- }
863
- export async function createPostgresTaggablesTable() {
864
- if (await hasMigrationBeenCreated("taggables"))
865
- return;
866
- let migrationContent = `import type { Database } from '@stacksjs/database'
867
- `;
868
- migrationContent += `import { sql } from '@stacksjs/database'
869
-
870
- `;
871
- migrationContent += `export async function up(db: Database<any>) {
872
- `;
873
- migrationContent += ` await db.schema
874
- `;
875
- migrationContent += ` .createTable('taggables')
876
- `;
877
- migrationContent += ` .addColumn('id', 'serial', col => col.primaryKey())
878
- `;
879
- migrationContent += ` .addColumn('tag_id', 'integer', col => col.notNull())
880
- `;
881
- migrationContent += ` .addColumn('taggable_id', 'integer', col => col.notNull())
882
- `;
883
- migrationContent += ` .addColumn('taggable_type', 'varchar(255)', col => col.notNull())
884
- `;
885
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
886
- `;
887
- migrationContent += ` .addColumn('updated_at', 'timestamp')
888
- `;
889
- migrationContent += ` .execute()
890
-
891
- `;
892
- migrationContent += ` await db.schema
893
- `;
894
- migrationContent += ` .alterTable('taggables')
895
- `;
896
- migrationContent += ` .addForeignKeyConstraint('taggables_tag_id_foreign', ['tag_id'], 'tags', ['id'], (cb) => cb.onDelete('cascade'))
897
- `;
898
- migrationContent += ` .execute()
899
-
900
- `;
901
- migrationContent += ` await db.schema
902
- `;
903
- migrationContent += ` .createIndex('idx_taggables_tag')
904
- `;
905
- migrationContent += ` .on('taggables')
906
- `;
907
- migrationContent += ` .column('tag_id')
908
- `;
909
- migrationContent += ` .execute()
910
-
911
- `;
912
- migrationContent += ` await db.schema
913
- `;
914
- migrationContent += ` .createIndex('idx_taggables_polymorphic')
915
- `;
916
- migrationContent += ` .on('taggables')
917
- `;
918
- migrationContent += ` .columns(['taggable_id', 'taggable_type'])
919
- `;
920
- migrationContent += ` .execute()
921
-
922
- `;
923
- migrationContent += ` await db.schema
924
- `;
925
- migrationContent += ` .createIndex('idx_taggables_unique')
926
- `;
927
- migrationContent += ` .on('taggables')
928
- `;
929
- migrationContent += ` .columns(['tag_id', 'taggable_id', 'taggable_type'])
930
- `;
931
- migrationContent += ` .unique()
932
- `;
933
- migrationContent += ` .execute()
934
-
935
- `;
936
- migrationContent += `}
937
- `;
938
- const migrationFileName = `${new Date().getTime().toString()}-create-taggables-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
939
- await Bun.write(migrationFilePath, migrationContent);
940
- log.success(`Created migration: ${italic(migrationFileName)}`);
941
- }
942
- export async function createQueryLogsTable() {
943
- if (await hasMigrationBeenCreated("query_logs"))
944
- return;
945
- let migrationContent = `import type { Database } from '@stacksjs/database'
946
- `;
947
- migrationContent += `import { sql } from '@stacksjs/database'
948
-
949
- `;
950
- migrationContent += `export async function up(db: Database<any>) {
951
- `;
952
- migrationContent += ` await db.schema
953
- `;
954
- migrationContent += ` .createTable('query_logs')
955
- `;
956
- migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
957
- `;
958
- migrationContent += ` .addColumn('query', 'text', col => col.notNull())
959
- `;
960
- migrationContent += ` .addColumn('normalized_query', 'text')
961
- `;
962
- migrationContent += ` .addColumn('duration', 'integer')
963
- `;
964
- migrationContent += ` .addColumn('connection', 'varchar(255)')
965
- `;
966
- migrationContent += ` .addColumn('status', 'varchar(50)')
967
- `;
968
- migrationContent += ` .addColumn('error', 'text')
969
- `;
970
- migrationContent += ` .addColumn('executed_at', 'timestamp', col => col.notNull())
971
- `;
972
- migrationContent += ` .addColumn('trace', 'text')
973
- `;
974
- migrationContent += ` .addColumn('model', 'varchar(255)')
975
- `;
976
- migrationContent += ` .addColumn('method', 'varchar(255)')
977
- `;
978
- migrationContent += ` .addColumn('file', 'text')
979
- `;
980
- migrationContent += ` .addColumn('line', 'integer')
981
- `;
982
- migrationContent += ` .addColumn('memory_usage', 'real')
983
- `;
984
- migrationContent += ` .addColumn('rows_affected', 'integer')
985
- `;
986
- migrationContent += ` .addColumn('transaction_id', 'varchar(255)')
987
- `;
988
- migrationContent += ` .addColumn('optimization_suggestions', 'json')
989
- `;
990
- migrationContent += ` .addColumn('affected_tables', 'json')
991
- `;
992
- migrationContent += ` .addColumn('indexes_used', 'json')
993
- `;
994
- migrationContent += ` .addColumn('missing_indexes', 'json')
995
- `;
996
- migrationContent += ` .addColumn('explain_plan', 'json')
997
- `;
998
- migrationContent += ` .addColumn('tags', 'json')
999
- `;
1000
- migrationContent += ` .addColumn('bindings', 'json')
1001
- `;
1002
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
1003
- `;
1004
- migrationContent += ` .addColumn('updated_at', 'timestamp')
1005
- `;
1006
- migrationContent += ` .execute()
1007
-
1008
- `;
1009
- migrationContent += ` await db.schema
1010
- `;
1011
- migrationContent += ` .createIndex('idx_query_logs_executed_at')
1012
- `;
1013
- migrationContent += ` .on('query_logs')
1014
- `;
1015
- migrationContent += ` .column('executed_at')
1016
- `;
1017
- migrationContent += ` .execute()
1018
-
1019
- `;
1020
- migrationContent += ` await db.schema
1021
- `;
1022
- migrationContent += ` .createIndex('idx_query_logs_status')
1023
- `;
1024
- migrationContent += ` .on('query_logs')
1025
- `;
1026
- migrationContent += ` .column('status')
1027
- `;
1028
- migrationContent += ` .execute()
1029
-
1030
- `;
1031
- migrationContent += ` await db.schema
1032
- `;
1033
- migrationContent += ` .createIndex('idx_query_logs_duration')
1034
- `;
1035
- migrationContent += ` .on('query_logs')
1036
- `;
1037
- migrationContent += ` .column('duration')
1038
- `;
1039
- migrationContent += ` .execute()
1040
-
1041
- `;
1042
- migrationContent += `}
1043
- `;
1044
- const migrationFileName = `${new Date().getTime().toString()}-create-query-logs-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
1045
- await Bun.write(migrationFilePath, migrationContent);
1046
- log.success(`Created migration: ${italic(migrationFileName)}`);
1047
- }
1048
- export async function createPostgresQueryLogsTable() {
1049
- if (await hasMigrationBeenCreated("query_logs"))
1050
- return;
1051
- let migrationContent = `import type { Database } from '@stacksjs/database'
1052
- `;
1053
- migrationContent += `import { sql } from '@stacksjs/database'
1054
-
1055
- `;
1056
- migrationContent += `export async function up(db: Database<any>) {
1057
- `;
1058
- migrationContent += ` await db.schema
1059
- `;
1060
- migrationContent += ` .createTable('query_logs')
1061
- `;
1062
- migrationContent += ` .addColumn('id', 'serial', col => col.primaryKey())
1063
- `;
1064
- migrationContent += ` .addColumn('query', 'text', col => col.notNull())
1065
- `;
1066
- migrationContent += ` .addColumn('normalized_query', 'text')
1067
- `;
1068
- migrationContent += ` .addColumn('duration', 'integer')
1069
- `;
1070
- migrationContent += ` .addColumn('connection', 'varchar(255)')
1071
- `;
1072
- migrationContent += ` .addColumn('status', 'varchar(50)')
1073
- `;
1074
- migrationContent += ` .addColumn('error', 'text')
1075
- `;
1076
- migrationContent += ` .addColumn('executed_at', 'timestamp', col => col.notNull())
1077
- `;
1078
- migrationContent += ` .addColumn('trace', 'text')
1079
- `;
1080
- migrationContent += ` .addColumn('model', 'varchar(255)')
1081
- `;
1082
- migrationContent += ` .addColumn('method', 'varchar(255)')
1083
- `;
1084
- migrationContent += ` .addColumn('file', 'text')
1085
- `;
1086
- migrationContent += ` .addColumn('line', 'integer')
1087
- `;
1088
- migrationContent += ` .addColumn('memory_usage', 'double precision')
1089
- `;
1090
- migrationContent += ` .addColumn('rows_affected', 'integer')
1091
- `;
1092
- migrationContent += ` .addColumn('transaction_id', 'varchar(255)')
1093
- `;
1094
- migrationContent += ` .addColumn('optimization_suggestions', 'jsonb')
1095
- `;
1096
- migrationContent += ` .addColumn('affected_tables', 'jsonb')
1097
- `;
1098
- migrationContent += ` .addColumn('indexes_used', 'jsonb')
1099
- `;
1100
- migrationContent += ` .addColumn('missing_indexes', 'jsonb')
1101
- `;
1102
- migrationContent += ` .addColumn('explain_plan', 'jsonb')
1103
- `;
1104
- migrationContent += ` .addColumn('tags', 'jsonb')
1105
- `;
1106
- migrationContent += ` .addColumn('bindings', 'jsonb')
1107
- `;
1108
- migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
1109
- `;
1110
- migrationContent += ` .addColumn('updated_at', 'timestamp')
1111
- `;
1112
- migrationContent += ` .execute()
1113
-
1114
- `;
1115
- migrationContent += ` await db.schema
1116
- `;
1117
- migrationContent += ` .createIndex('idx_query_logs_executed_at')
1118
- `;
1119
- migrationContent += ` .on('query_logs')
1120
- `;
1121
- migrationContent += ` .column('executed_at')
1122
- `;
1123
- migrationContent += ` .execute()
1124
-
1125
- `;
1126
- migrationContent += ` await db.schema
1127
- `;
1128
- migrationContent += ` .createIndex('idx_query_logs_status')
1129
- `;
1130
- migrationContent += ` .on('query_logs')
1131
- `;
1132
- migrationContent += ` .column('status')
1133
- `;
1134
- migrationContent += ` .execute()
1135
-
1136
- `;
1137
- migrationContent += ` await db.schema
1138
- `;
1139
- migrationContent += ` .createIndex('idx_query_logs_duration')
1140
- `;
1141
- migrationContent += ` .on('query_logs')
1142
- `;
1143
- migrationContent += ` .column('duration')
1144
- `;
1145
- migrationContent += ` .execute()
1146
-
1147
- `;
1148
- migrationContent += `}
1149
- `;
1150
- const migrationFileName = `${new Date().getTime().toString()}-create-query-logs-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
1151
- await Bun.write(migrationFilePath, migrationContent);
1152
- log.success(`Created migration: ${italic(migrationFileName)}`);
26
+ const { identifierQuote: q, wire } = dialectCapabilities(getDbDriver()), cascade = wire === "postgres" ? " CASCADE" : "";
27
+ for (const table of ["migrations", "migration_locks"])
28
+ await db.unsafe(`DROP TABLE IF EXISTS ${q}${table}${q}${cascade}`).execute();
1153
29
  }