Skip to content

Commit 0da014a

Browse files
chore: wip
1 parent bfa9a82 commit 0da014a

File tree

7 files changed

+279
-255
lines changed

7 files changed

+279
-255
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export async function resetMysqlDatabase() {
6464

6565
export async function generateMysqlMigration(modelPath: string) {
6666
// check if any files are in the database folder
67-
const files = await fs.readdir(path.userMigrationsPath())
67+
// const files = await fs.readdir(path.userMigrationsPath())
6868

6969
// if (files.length === 0) {
7070
// log.debug('No migrations found in the database folder, deleting all framework/database/*.json files...')

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ async function createTableMigration(modelPath: string) {
143143
for (const [fieldName, options] of arrangeColumns(model.attributes)) {
144144
const fieldOptions = options as Attribute
145145
const fieldNameFormatted = snakeCase(fieldName)
146-
const columnType = mapFieldTypeToColumnType(fieldOptions.validations?.rule)
146+
const columnType = mapFieldTypeToColumnType(fieldOptions.validation?.rule)
147147
migrationContent += ` .addColumn('${fieldNameFormatted}', '${columnType}'`
148148

149149
// Check if there are configurations that require the lambda function
150-
if (fieldOptions.unique || fieldOptions.validations?.rule?.required) {
150+
if (fieldOptions.unique || fieldOptions.validation?.rule?.required) {
151151
migrationContent += `, col => col`
152152
if (fieldOptions.unique) migrationContent += `.unique()`
153-
if (fieldOptions.validations?.rule?.required) migrationContent += `.notNull()`
153+
if (fieldOptions.validation?.rule?.required) migrationContent += `.notNull()`
154154
migrationContent += ``
155155
}
156156

@@ -245,7 +245,7 @@ async function createAlterTableMigration(modelPath: string) {
245245
// Add new fields
246246
for (const fieldName of fieldsToAdd) {
247247
const options = currentFields[fieldName] as Attribute
248-
const columnType = mapFieldTypeToColumnType(options.validations?.rule)
248+
const columnType = mapFieldTypeToColumnType(options.validation?.rule)
249249
migrationContent += ` .addColumn('${fieldName}', '${columnType}')\n`
250250
}
251251

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { Attribute, Attributes, Model } from '@stacksjs/types'
1111
import {
1212
arrangeColumns,
1313
checkPivotMigration,
14+
fetchTables,
1415
findDifferingKeys,
1516
getLastMigrationFields,
1617
hasTableBeenMigrated,
@@ -21,6 +22,13 @@ import {
2122

2223
export async function resetSqliteDatabase() {
2324
await deleteFrameworkModels()
25+
await deleteMigrationFiles()
26+
await dropSqliteTables()
27+
28+
return ok('All tables dropped successfully!')
29+
}
30+
31+
export async function deleteMigrationFiles() {
2432
const files = await fs.readdir(path.userMigrationsPath())
2533

2634
if (files.length) {
@@ -32,26 +40,11 @@ export async function resetSqliteDatabase() {
3240
}
3341
}
3442
}
35-
36-
return ok('All tables dropped successfully!')
3743
}
3844

3945
export async function deleteFrameworkModels() {
40-
const dbPath = fetchSqliteFile()
41-
42-
if (fs.existsSync(dbPath)) await Bun.$`rm ${dbPath}`
43-
4446
const modelFiles = await fs.readdir(path.frameworkPath('database/models'))
4547

46-
const userModelFiles = globSync([path.userModelsPath('*.ts')], { absolute: true })
47-
48-
for (const userModel of userModelFiles) {
49-
const userModelPath = (await import(userModel)).default
50-
const pivotTables = await getPivotTables(userModelPath, userModel)
51-
52-
for (const pivotTable of pivotTables) await db.schema.dropTable(pivotTable.table).ifExists().execute()
53-
}
54-
5548
if (modelFiles.length) {
5649
for (const modelFile of modelFiles) {
5750
if (modelFile.endsWith('.ts')) {
@@ -63,6 +56,22 @@ export async function deleteFrameworkModels() {
6356
}
6457
}
6558

59+
export async function dropSqliteTables() {
60+
const userModelFiles = globSync([path.userModelsPath('*.ts')], { absolute: true })
61+
const tables = await fetchTables()
62+
63+
for (const table of tables) await db.schema.dropTable(table).ifExists().execute()
64+
await db.schema.dropTable('migrations').ifExists().execute()
65+
await db.schema.dropTable('migration_locks').ifExists().execute()
66+
67+
for (const userModel of userModelFiles) {
68+
const userModelPath = (await import(userModel)).default
69+
const pivotTables = await getPivotTables(userModelPath, userModel)
70+
71+
for (const pivotTable of pivotTables) await db.schema.dropTable(pivotTable.table).ifExists().execute()
72+
}
73+
}
74+
6675
export function fetchSqliteFile(): string {
6776
if (app.env === 'testing') {
6877
return fetchTestSqliteFile()

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import { app, database } from '@stacksjs/config'
22
import { log } from '@stacksjs/logging'
33
import type { Database } from '@stacksjs/orm'
4+
import { path } from '@stacksjs/path'
45
import { Kysely, MysqlDialect, PostgresDialect, sql } from 'kysely'
6+
import type { RawBuilder } from 'kysely'
57
import { BunWorkerDialect } from 'kysely-bun-worker'
68
import { createPool } from 'mysql2'
79
import { Pool } from 'pg'
810

911
const appEnv = app.env || 'local'
1012

11-
export function getDialect() {
13+
export function getDialect(): MysqlDialect | PostgresDialect | BunWorkerDialect {
1214
const driver = database.default ?? 'sqlite'
1315

1416
log.debug(`Using database driver: ${driver}`)
1517

1618
if (driver === 'sqlite') {
1719
const defaultName = appEnv !== 'testing' ? 'database/stacks.sqlite' : 'database/stacks_testing.sqlite'
18-
const path = database.connections?.sqlite.database ?? defaultName
20+
const sqliteDbName = database.connections?.sqlite.database ?? defaultName
21+
const dbPath = path.projectPath(sqliteDbName)
1922

2023
return new BunWorkerDialect({
21-
url: path,
24+
url: dbPath,
2225
})
2326
}
2427

@@ -52,8 +55,8 @@ export function getDialect() {
5255
throw new Error(`Unsupported driver: ${driver}`)
5356
}
5457

55-
export const now = sql`now()`
58+
export const now: RawBuilder<any> = sql`now()`
5659

57-
export const db = new Kysely<Database>({
60+
export const db: Kysely<Database> = new Kysely<Database>({
5861
dialect: getDialect(),
5962
})

storage/framework/core/storage/src/glob.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface GlobOptions {
1313
onlyFiles?: boolean
1414
}
1515

16-
export const globSync = (patterns: string | string[], options?: Omit<GlobOptions, 'patterns'>) => {
16+
export const globSync = (patterns: string | string[], options?: Omit<GlobOptions, 'patterns'>): string[] => {
1717
if (typeof patterns === 'string') {
1818
return gs([patterns], options)
1919
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { database } from '@stacksjs/config'
2-
import { copyModelFiles, db, deleteFrameworkModels, fetchTables, runDatabaseMigration, sql } from '@stacksjs/database'
2+
import {
3+
copyModelFiles,
4+
db,
5+
deleteFrameworkModels,
6+
dropSqliteTables,
7+
fetchSqliteFile,
8+
fetchTables,
9+
runDatabaseMigration,
10+
sql,
11+
} from '@stacksjs/database'
312
import { path } from '@stacksjs/path'
4-
import { globSync } from '@stacksjs/storage'
13+
import { fs, globSync } from '@stacksjs/storage'
514

615
const driver = database.default || ''
716

@@ -32,6 +41,11 @@ export async function truncateMysql() {
3241
}
3342

3443
export async function truncateSqlite() {
44+
const sqlitePath = fetchSqliteFile()
45+
46+
if (!fs.existsSync(sqlitePath)) await Bun.$`touch ${sqlitePath}`
47+
48+
await dropSqliteTables()
3549
await deleteFrameworkModels()
3650

3751
const modelFiles = globSync([path.userModelsPath('*.ts')], { absolute: true })

0 commit comments

Comments
 (0)