Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
glennmichael123 committed Jun 22, 2024
1 parent d4050dd commit ae7b6fc
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 9 deletions.
12 changes: 12 additions & 0 deletions app/Models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ export default {

factory: () => faker.person.jobTitle(),
},
content: {
required: true,
validation: {
rule: schema.string().minLength(3).maxLength(255),
message: {
minLength: 'Job title must have a minimum of 3 characters',
maxLength: 'Job title must have a maximum of 255 characters',
},
},

factory: () => faker.person.jobTitle(),
},

password: {
required: true,
Expand Down
8 changes: 0 additions & 8 deletions database/migrations/1719031504777-update-users-table.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ export async function up(db: Database<any>) {
await db.schema.alterTable('users')
.addColumn('content', 'varchar(255)', col => col.notNull())
.execute();
await sql`
ALTER TABLE users
MODIFY COLUMN content VARCHAR(255) NOT NULL AFTER job_title;
`.execute(db)

}
26 changes: 25 additions & 1 deletion storage/framework/core/database/src/drivers/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export async function createAlterTableMigration(modelPath: string) {
migrationContent += ``
}

migrationContent += `)\n`
migrationContent += `)\n\n`
}

console.log(migrationContent)
Expand All @@ -268,6 +268,20 @@ export async function createAlterTableMigration(modelPath: string) {
for (const fieldName of fieldsToRemove) migrationContent += ` .dropColumn('${fieldName}')\n`

migrationContent += ` .execute();\n`

for (const fieldName of fieldsToAdd) {
const formattedFieldName = snakeCase(fieldName)

const previousValue = getPreviousValue(Object.keys(currentFields), fieldName)

if (previousValue) {
migrationContent += `await sql\`
ALTER TABLE ${tableName}
MODIFY COLUMN ${formattedFieldName} VARCHAR(255) NOT NULL AFTER ${snakeCase(previousValue)};
\`.execute(db)\n\n`
}
}

migrationContent += `}\n`

const timestamp = new Date().getTime().toString()
Expand All @@ -291,6 +305,16 @@ function pluckChanges(array1: string[], array2: string[]): { added: string[]; re
return { added, removed }
}

function getPreviousValue<T>(array: T[], target: T): T | null {
const index = array.indexOf(target)

if (index === -1 || index === 0) {
return null // Target not found or is the first element
}

return array[index - 1] ?? null
}

export async function fetchMysqlTables(): Promise<string[]> {
const modelFiles = glob.sync(path.userModelsPath('*.ts'))
const tables: string[] = []
Expand Down
12 changes: 12 additions & 0 deletions storage/framework/database/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ export default {

factory: () => faker.person.jobTitle(),
},
content: {
required: true,
validation: {
rule: schema.string().minLength(3).maxLength(255),
message: {
minLength: 'Job title must have a minimum of 3 characters',
maxLength: 'Job title must have a maximum of 255 characters',
},
},

factory: () => faker.person.jobTitle(),
},

password: {
required: true,
Expand Down

0 comments on commit ae7b6fc

Please sign in to comment.