Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed May 18, 2024
1 parent cb9eb7f commit c5e9b94
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
8 changes: 4 additions & 4 deletions storage/framework/core/database/src/drivers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { log } from '@stacksjs/cli'
import { db } from '@stacksjs/database'
import { path } from '@stacksjs/path'
import { fs } from '@stacksjs/storage'
import { isString } from '@stacksjs/validation'
import type { Attributes, Model, RelationConfig, VineType } from '@stacksjs/types'
import { isString } from '@stacksjs/validation'

export * from './mysql'
export * from './postgres'
export * from './sqlite'

export async function getLastMigrationFields(modelName: string): Promise<Attributes> {
const oldModelPath = path.frameworkPath(`database/models/${modelName}`)
const model = await import(oldModelPath)
const model = (await import(oldModelPath)).default as Model
let fields = {} as Attributes

if (typeof model.default.attributes === 'object') fields = model.default.attributes
else fields = JSON.parse(model.default.attributes) as Attributes
if (typeof model.attributes === 'object') fields = model.attributes
else fields = JSON.parse(model.attributes) as Attributes

return fields
}
Expand Down
18 changes: 8 additions & 10 deletions storage/framework/core/database/src/drivers/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ export async function generatePostgresMigration(modelPath: string) {
}
}

const model = await import(modelPath)
const model = (await import(modelPath)).default as Model
const fileName = path.basename(modelPath)
const tableName = await modelTableName(model)

const fieldsString = JSON.stringify(model.default.attributes, null, 2) // Pretty print the JSON
const fieldsString = JSON.stringify(model.attributes, null, 2) // Pretty print the JSON
const copiedModelPath = path.frameworkPath(`database/models/${fileName}`)

let haveFieldsChanged = false
Expand Down Expand Up @@ -115,18 +115,16 @@ export async function generatePostgresMigration(modelPath: string) {
async function createTableMigration(modelPath: string) {
log.debug('createTableMigration modelPath:', modelPath)

const model = await import(modelPath)
const model = (await import(modelPath)).default as Model
const tableName = await modelTableName(model)

await createPivotTableMigration(model)

const modelFiles = glob.sync(path.userModelsPath('*.ts'))

const otherModelRelations = await fetchOtherModelRelations(model, modelFiles)

const fields = model.default.attributes
const useTimestamps = model.default?.traits?.useTimestamps ?? model.default?.traits?.timestampable
const useSoftDeletes = model.default?.traits?.useSoftDeletes ?? model.default?.traits?.softDeletable
const fields = model.attributes
const useTimestamps = model.traits?.useTimestamps ?? model.traits?.timestampable
const useSoftDeletes = model.traits?.useSoftDeletes ?? model.traits?.softDeletable

let migrationContent = `import type { Database } from '@stacksjs/database'\n`
migrationContent += `import { sql } from '@stacksjs/database'\n\n`
Expand Down Expand Up @@ -240,15 +238,15 @@ async function getPivotTables(
export async function createAlterTableMigration(modelPath: string) {
console.log('createAlterTableMigration')

const model = await import(modelPath)
const model = (await import(modelPath)).default as Model
const modelName = path.basename(modelPath)
const tableName = await modelTableName(model)

// Assuming you have a function to get the fields from the last migration
// For simplicity, this is not implemented here
const lastMigrationFields = await getLastMigrationFields(modelName)
const lastFields = lastMigrationFields ?? {}
const currentFields = model.default.attributes as Attributes
const currentFields = model.attributes

// Determine fields to add and remove
const fieldsToAdd = Object.keys(currentFields)
Expand Down
7 changes: 3 additions & 4 deletions storage/framework/core/database/src/drivers/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,16 @@ export async function generateSqliteMigration(modelPath: string) {
if (modelFiles.length) {
log.debug('No existing model files in framework path...')

for (const file of modelFiles) {
for (const file of modelFiles)
if (file.endsWith('.ts')) await fs.unlink(path.frameworkPath(`database/models/${file}`))
}
}
}

const model = await import(modelPath) as Model
const model = (await import(modelPath)).default as Model
const fileName = path.basename(modelPath)
const tableName = await modelTableName(model)

const fieldsString = JSON.stringify(model.default.attributes, null, 2) // Pretty print the JSON
const fieldsString = JSON.stringify(model.attributes, null, 2) // Pretty print the JSON
const copiedModelPath = path.frameworkPath(`database/models/${fileName}`)

let haveFieldsChanged = false
Expand Down
6 changes: 3 additions & 3 deletions storage/framework/core/database/src/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ export async function lastMigrationDate(): Promise<string | undefined> {
// read the last migration file and extract the fields that were modified.
export async function getLastMigrationFields(modelName: string): Promise<Attribute> {
const oldModelPath = path.frameworkPath(`database/models/${modelName}`)
const model = await import(oldModelPath)
const model = (await import(oldModelPath)).default as Model
let fields = {} as Attributes

if (typeof model.default.attributes === 'object') fields = model.default.attributes
else fields = JSON.parse(model.default.attributes) as Attributes
if (typeof model.attributes === 'object') fields = model.attributes
else fields = JSON.parse(model.attributes) as Attributes

return fields
}
Expand Down
2 changes: 1 addition & 1 deletion storage/framework/core/orm/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function modelTableName(model: Model | ModelPath): Promise<string>
model = (await import(model)).default as Model
}

return model.table ?? snakeCase(plural(model.name ?? modelPath.replace(/.*\/(.*)\.ts$/, '$1')))
return model.table ?? snakeCase(plural(model.name))
}

export async function extractFieldsFromModel(filePath: string) {
Expand Down

0 comments on commit c5e9b94

Please sign in to comment.