Brandon Presley
6d46fe959c
I am struggling to remember why I started doing this. I can't think of any real benefit.
45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import {MigrationInterface, QueryRunner, Table, TableColumn} from 'typeorm'
|
|
import {AppDataSource} from '../data-source'
|
|
import {planRepo} from '../db'
|
|
import {PlanWorkout} from '../plan-workout'
|
|
import {Workout} from '../workout'
|
|
|
|
export class addWorkouts1670477728181 implements MigrationInterface {
|
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
|
await queryRunner.createTable(
|
|
new Table({
|
|
name: 'workouts',
|
|
columns: [
|
|
new TableColumn({
|
|
name: 'id',
|
|
type: 'int',
|
|
isPrimary: true,
|
|
isGenerated: true,
|
|
}),
|
|
new TableColumn({name: 'name', type: 'text', isNullable: false}),
|
|
],
|
|
}),
|
|
)
|
|
await queryRunner.createTable(
|
|
new Table({
|
|
name: 'plans_workouts_workouts',
|
|
columns: [
|
|
new TableColumn({name: 'plansId', type: 'int'}),
|
|
new TableColumn({name: 'workoutsId', type: 'int'}),
|
|
],
|
|
}),
|
|
)
|
|
const plans = await AppDataSource.query('SELECT * FROM plans')
|
|
const repo = AppDataSource.getRepository(PlanWorkout)
|
|
for (const plan of plans) {
|
|
const workouts: PlanWorkout[] = plan.workouts
|
|
.split(',')
|
|
.map((workout: string) => ({plansId: plan.id}))
|
|
repo.save()
|
|
}
|
|
await queryRunner.dropColumn('plans', 'workouts')
|
|
}
|
|
|
|
public async down(queryRunner: QueryRunner): Promise<void> {}
|
|
}
|