Skip to content

Commit a620c76

Browse files
chore: wip
1 parent 56c2edc commit a620c76

File tree

4 files changed

+50
-47
lines changed

4 files changed

+50
-47
lines changed

storage/framework/core/database/src/drivers/defaults/traits.ts

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { hasMigrationBeenCreated } from '../index'
66
export function getTraitTables(): string[] {
77
return [
88
'tags',
9-
'taggable_models',
9+
'taggables',
1010
'categorizables',
1111
'comments',
1212
'commentables',
@@ -87,7 +87,7 @@ export async function createPostgresPasskeyMigration(): Promise<void> {
8787
}
8888

8989
export async function createTaggableTable(): Promise<void> {
90-
const hasBeenMigrated = await hasMigrationBeenCreated('taggable')
90+
const hasBeenMigrated = await hasMigrationBeenCreated('tags')
9191

9292
if (hasBeenMigrated)
9393
return
@@ -109,19 +109,19 @@ export async function createTaggableTable(): Promise<void> {
109109
migrationContent += ` .execute()\n\n`
110110

111111
migrationContent += ` await db.schema\n`
112-
migrationContent += ` .createIndex('idx_tag_slug')\n`
112+
migrationContent += ` .createIndex('idx_tags_slug')\n`
113113
migrationContent += ` .on('tags')\n`
114114
migrationContent += ` .column('slug')\n`
115115
migrationContent += ` .execute()\n\n`
116116

117117
migrationContent += ` await db.schema\n`
118-
migrationContent += ` .createIndex('idx_tag_type')\n`
118+
migrationContent += ` .createIndex('idx_tags_type')\n`
119119
migrationContent += ` .on('tags')\n`
120120
migrationContent += ` .column('type')\n`
121121
migrationContent += ` .execute()\n\n`
122122

123123
migrationContent += ` await db.schema\n`
124-
migrationContent += ` .createIndex('idx_tag_name')\n`
124+
migrationContent += ` .createIndex('idx_tags_name')\n`
125125
migrationContent += ` .on('tags')\n`
126126
migrationContent += ` .column('name')\n`
127127
migrationContent += ` .execute()\n\n`
@@ -136,11 +136,11 @@ export async function createTaggableTable(): Promise<void> {
136136

137137
log.success(`Created migration: ${italic(migrationFileName)}`)
138138

139-
await createTaggableModelsTable()
139+
await createTaggablesTable()
140140
}
141141

142142
export async function createPostgresTaggableTable(): Promise<void> {
143-
const hasBeenMigrated = await hasMigrationBeenCreated('taggable')
143+
const hasBeenMigrated = await hasMigrationBeenCreated('tags')
144144

145145
if (hasBeenMigrated)
146146
return
@@ -188,6 +188,8 @@ export async function createPostgresTaggableTable(): Promise<void> {
188188
Bun.write(migrationFilePath, migrationContent)
189189

190190
log.success(`Created migration: ${italic(migrationFileName)}`)
191+
192+
await createPostgresTaggablesTable()
191193
}
192194

193195
// SQLite/MySQL version
@@ -399,7 +401,7 @@ export async function dropCommonTables(): Promise<void> {
399401
await db.schema.dropTable('categorizables').ifExists().execute()
400402
await db.schema.dropTable('commenteable_upvotes').ifExists().execute()
401403
await db.schema.dropTable('tags').ifExists().execute()
402-
await db.schema.dropTable('taggable_models').ifExists().execute()
404+
await db.schema.dropTable('taggables').ifExists().execute()
403405
await db.schema.dropTable('categorizable_models').ifExists().execute()
404406
await db.schema.dropTable('commentables').ifExists().execute()
405407
await db.schema.dropTable('comments').ifExists().execute()
@@ -576,8 +578,8 @@ export async function createPostgresCommentablesPivotTable(): Promise<void> {
576578
log.success(`Created migration: ${italic(migrationFileName)}`)
577579
}
578580

579-
export async function createTaggableModelsTable(): Promise<void> {
580-
const hasBeenMigrated = await hasMigrationBeenCreated('taggable_models')
581+
export async function createTaggablesTable(): Promise<void> {
582+
const hasBeenMigrated = await hasMigrationBeenCreated('taggables')
581583

582584
if (hasBeenMigrated)
583585
return
@@ -586,7 +588,7 @@ export async function createTaggableModelsTable(): Promise<void> {
586588
migrationContent += `import { sql } from '@stacksjs/database'\n\n`
587589
migrationContent += `export async function up(db: Database<any>) {\n`
588590
migrationContent += ` await db.schema\n`
589-
migrationContent += ` .createTable('taggable_models')\n`
591+
migrationContent += ` .createTable('taggables')\n`
590592
migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())\n`
591593
migrationContent += ` .addColumn('tag_id', 'integer', col => col.notNull())\n`
592594
migrationContent += ` .addColumn('taggable_id', 'integer', col => col.notNull())\n`
@@ -597,42 +599,42 @@ export async function createTaggableModelsTable(): Promise<void> {
597599

598600
// Add foreign key constraint to tags table
599601
migrationContent += ` await db.schema\n`
600-
migrationContent += ` .alterTable('taggable_models')\n`
601-
migrationContent += ` .addForeignKeyConstraint('taggable_models_tag_id_foreign', ['tag_id'], 'tags', ['id'], (cb) => cb.onDelete('cascade'))\n`
602+
migrationContent += ` .alterTable('taggables')\n`
603+
migrationContent += ` .addForeignKeyConstraint('taggables_tag_id_foreign', ['tag_id'], 'tags', ['id'], (cb) => cb.onDelete('cascade'))\n`
602604
migrationContent += ` .execute()\n\n`
603605

604606
migrationContent += ` await db.schema\n`
605-
migrationContent += ` .createIndex('idx_taggable_models_tag')\n`
606-
migrationContent += ` .on('taggable_models')\n`
607+
migrationContent += ` .createIndex('idx_taggables_tag')\n`
608+
migrationContent += ` .on('taggables')\n`
607609
migrationContent += ` .column('tag_id')\n`
608610
migrationContent += ` .execute()\n\n`
609611

610612
migrationContent += ` await db.schema\n`
611-
migrationContent += ` .createIndex('idx_taggable_models_polymorphic')\n`
612-
migrationContent += ` .on('taggable_models')\n`
613+
migrationContent += ` .createIndex('idx_taggables_polymorphic')\n`
614+
migrationContent += ` .on('taggables')\n`
613615
migrationContent += ` .columns(['taggable_id', 'taggable_type'])\n`
614616
migrationContent += ` .execute()\n\n`
615617

616618
migrationContent += ` await db.schema\n`
617-
migrationContent += ` .createIndex('idx_taggable_models_unique')\n`
618-
migrationContent += ` .on('taggable_models')\n`
619+
migrationContent += ` .createIndex('idx_taggables_unique')\n`
620+
migrationContent += ` .on('taggables')\n`
619621
migrationContent += ` .columns(['tag_id', 'taggable_id', 'taggable_type'])\n`
620622
migrationContent += ` .unique()\n`
621623
migrationContent += ` .execute()\n\n`
622624

623625
migrationContent += `}\n`
624626

625627
const timestamp = new Date().getTime().toString()
626-
const migrationFileName = `${timestamp}-create-taggable-models-table.ts`
628+
const migrationFileName = `${timestamp}-create-taggables-table.ts`
627629
const migrationFilePath = path.userMigrationsPath(migrationFileName)
628630

629631
Bun.write(migrationFilePath, migrationContent)
630632

631633
log.success(`Created migration: ${italic(migrationFileName)}`)
632634
}
633635

634-
export async function createPostgresTaggableModelsTable(): Promise<void> {
635-
const hasBeenMigrated = await hasMigrationBeenCreated('taggable_models')
636+
export async function createPostgresTaggablesTable(): Promise<void> {
637+
const hasBeenMigrated = await hasMigrationBeenCreated('taggables')
636638

637639
if (hasBeenMigrated)
638640
return
@@ -641,7 +643,7 @@ export async function createPostgresTaggableModelsTable(): Promise<void> {
641643
migrationContent += `import { sql } from '@stacksjs/database'\n\n`
642644
migrationContent += `export async function up(db: Database<any>) {\n`
643645
migrationContent += ` await db.schema\n`
644-
migrationContent += ` .createTable('taggable_models')\n`
646+
migrationContent += ` .createTable('taggables')\n`
645647
migrationContent += ` .addColumn('id', 'serial', col => col.primaryKey())\n`
646648
migrationContent += ` .addColumn('tag_id', 'integer', col => col.notNull())\n`
647649
migrationContent += ` .addColumn('taggable_id', 'integer', col => col.notNull())\n`
@@ -652,33 +654,33 @@ export async function createPostgresTaggableModelsTable(): Promise<void> {
652654

653655
// Add foreign key constraint to tags table
654656
migrationContent += ` await db.schema\n`
655-
migrationContent += ` .alterTable('taggable_models')\n`
656-
migrationContent += ` .addForeignKeyConstraint('taggable_models_tag_id_foreign', ['tag_id'], 'tags', ['id'], (cb) => cb.onDelete('cascade'))\n`
657+
migrationContent += ` .alterTable('taggables')\n`
658+
migrationContent += ` .addForeignKeyConstraint('taggables_tag_id_foreign', ['tag_id'], 'tags', ['id'], (cb) => cb.onDelete('cascade'))\n`
657659
migrationContent += ` .execute()\n\n`
658660

659661
migrationContent += ` await db.schema\n`
660-
migrationContent += ` .createIndex('idx_taggable_models_tag')\n`
661-
migrationContent += ` .on('taggable_models')\n`
662+
migrationContent += ` .createIndex('idx_taggables_tag')\n`
663+
migrationContent += ` .on('taggables')\n`
662664
migrationContent += ` .column('tag_id')\n`
663665
migrationContent += ` .execute()\n\n`
664666

665667
migrationContent += ` await db.schema\n`
666-
migrationContent += ` .createIndex('idx_taggable_models_polymorphic')\n`
667-
migrationContent += ` .on('taggable_models')\n`
668+
migrationContent += ` .createIndex('idx_taggables_polymorphic')\n`
669+
migrationContent += ` .on('taggables')\n`
668670
migrationContent += ` .columns(['taggable_id', 'taggable_type'])\n`
669671
migrationContent += ` .execute()\n\n`
670672

671673
migrationContent += ` await db.schema\n`
672-
migrationContent += ` .createIndex('idx_taggable_models_unique')\n`
673-
migrationContent += ` .on('taggable_models')\n`
674+
migrationContent += ` .createIndex('idx_taggables_unique')\n`
675+
migrationContent += ` .on('taggables')\n`
674676
migrationContent += ` .columns(['tag_id', 'taggable_id', 'taggable_type'])\n`
675677
migrationContent += ` .unique()\n`
676678
migrationContent += ` .execute()\n\n`
677679

678680
migrationContent += `}\n`
679681

680682
const timestamp = new Date().getTime().toString()
681-
const migrationFileName = `${timestamp}-create-taggable-models-table.ts`
683+
const migrationFileName = `${timestamp}-create-taggables-table.ts`
682684
const migrationFilePath = path.userMigrationsPath(migrationFileName)
683685

684686
Bun.write(migrationFilePath, migrationContent)

storage/framework/core/database/src/drivers/mysql.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
pluckChanges,
2525
} from '.'
2626

27-
import { createCategorizableTable, createCommentablesTable, createCommentUpvoteMigration, createPasskeyMigration, createQueryLogsTable, createTaggableTable, dropCommonTables } from './defaults/traits'
27+
import { createCategorizableTable, createCommentablesTable, createCommentUpvoteMigration, createPasskeyMigration, createQueryLogsTable, createTaggableTable, createTaggablesTable, dropCommonTables } from './defaults/traits'
2828

2929
export async function resetMysqlDatabase(): Promise<Ok<string, never>> {
3030
await dropMysqlTables()
@@ -117,6 +117,7 @@ export async function generateMysqlTraitMigrations(): Promise<void> {
117117
await createCategorizableTable(),
118118
await createCommentablesTable(),
119119
await createTaggableTable(),
120+
await createTaggablesTable(),
120121
await createPasswordResetsTable(),
121122
await createPasskeyMigration(),
122123
await createQueryLogsTable(),

storage/framework/core/database/src/drivers/postgres.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
createPostgresCommentUpvoteMigration,
3030
createPostgresPasskeyMigration,
3131
createPostgresQueryLogsTable,
32-
createPostgresTaggableModelsTable,
32+
createPostgresTaggablesTable,
3333
createPostgresTaggableTable,
3434
truncateMigrationTables,
3535
} from './defaults/traits'
@@ -58,7 +58,7 @@ async function dropCommonPostgresTables(): Promise<void> {
5858
await db.schema.dropTable('commentables').cascade().ifExists().execute()
5959
await db.schema.dropTable('comments').cascade().ifExists().execute()
6060
await db.schema.dropTable('tags').cascade().ifExists().execute()
61-
await db.schema.dropTable('taggable_models').cascade().ifExists().execute()
61+
await db.schema.dropTable('taggables').cascade().ifExists().execute()
6262
await db.schema.dropTable('commenteable_upvotes').cascade().ifExists().execute()
6363
}
6464

@@ -67,7 +67,7 @@ export async function generatePostgresTraitMigrations(): Promise<void> {
6767
await createPostgresCategorizableTable(),
6868
await createPostgresCommentsTable(),
6969
await createPostgresTaggableTable(),
70-
await createPostgresTaggableModelsTable(),
70+
await createPostgresTaggablesTable(),
7171
await createPostgresCommentUpvoteMigration(),
7272
await createPostgresPasskeyMigration(),
7373
await createPostgresQueryLogsTable(),

storage/framework/core/database/src/seeder.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,20 @@ async function seedModel(name: string, modelPath: string, model: Model) {
5555
}
5656
}
5757

58-
// if (otherRelations?.length) {
59-
// for (let j = 0; j < otherRelations.length; j++) {
60-
// const relationElement = otherRelations[j] as RelationConfig
58+
if (otherRelations?.length) {
59+
for (let j = 0; j < otherRelations.length; j++) {
60+
const relationElement = otherRelations[j] as RelationConfig
6161

62-
// const relationType = getRelationType(relationElement.relationship)
62+
const relationType = getRelationType(relationElement.relationship)
6363

64-
// if (relationElement.relationship === 'belongsToMany') {
65-
// await seedPivotRelation(relationElement)
66-
// }
64+
if (relationElement.relationship === 'belongsToMany') {
65+
await seedPivotRelation(relationElement)
66+
}
6767

68-
// if (relationType === 'hasType')
69-
// record[relationElement?.foreignKey] = await seedModelRelation(relationElement?.relationModel as string)
70-
// }
71-
// }
68+
if (relationType === 'hasType')
69+
record[relationElement?.foreignKey] = await seedModelRelation(relationElement?.relationModel as string)
70+
}
71+
}
7272

7373
if (useUuid)
7474
record.uuid = Bun.randomUUIDv7()

0 commit comments

Comments
 (0)