Skip to content

Commit

Permalink
fix(db): fix seed and migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kutay-celebi committed Jun 8, 2023
1 parent 7f99644 commit d33f711
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
18 changes: 12 additions & 6 deletions src/main/db/migrations/0001-add_col.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { DataTypes, QueryInterface } from 'sequelize'
import log from 'electron-log'

export const migration0001 = {
name: '0001-add_notes_column_to_entry',
up: async ({ context }: { context: QueryInterface }): Promise<void> => {
await context.addColumn('entry', 'notes', {
type: DataTypes.STRING,
allowNull: true
await context.describeTable('entry').then(async (value) => {
if (value.notes) {
log.info('notes column is already exists')
return Promise.resolve()
}

await context.addColumn('entry', 'notes', {
type: DataTypes.STRING,
allowNull: true
})
})
},
down: async ({ context }): Promise<void> => {
await context.removeColumn('entry', 'notes')
}
down: async (): Promise<void> => {}
}
12 changes: 9 additions & 3 deletions src/main/db/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const seed = async (db: Sequelize): Promise<void> => {
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},

date: {
type: DataTypes.DATE,
allowNull: false
Expand All @@ -44,8 +43,15 @@ export const seed = async (db: Sequelize): Promise<void> => {
{ sequelize: db, tableName: 'entry_timelog', freezeTableName: true, modelName: 'entry_timelog' }
)

Entry.hasMany(EntryTimelog, { foreignKey: 'entry_id', as: 'timelogs' })
EntryTimelog.belongsTo(Entry, { foreignKey: 'entry_id' })
Entry.hasMany(EntryTimelog, {
as: 'timelogs',
onDelete: 'CASCADE',
hooks: true,
foreignKey: 'entry_id'
})
EntryTimelog.belongsTo(Entry, {
foreignKey: 'entry_id'
})

Todo.init(
{
Expand Down
7 changes: 1 addition & 6 deletions src/main/db/types/Entry.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { BaseDO, BaseEntity } from './BaseEntity'
import { Association, HasManyCreateAssociationMixin, NonAttribute } from 'sequelize'
import { NonAttribute } from 'sequelize'
import { EntryTimelog, EntryTimelogDO } from './EntryTimelog'

export class Entry extends BaseEntity<Entry> {
declare label: string
declare notes?: string

declare createTimelog: HasManyCreateAssociationMixin<EntryTimelog, 'entry_id'>
declare timelogs?: NonAttribute<EntryTimelog[]>

declare static associations: {
timelogs: Association<Entry, EntryTimelog>
}
}

export interface EntryDO extends BaseDO {
Expand Down
7 changes: 1 addition & 6 deletions src/main/db/types/EntryTimelog.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { BaseDO, BaseEntity } from './BaseEntity'
import { Association, ForeignKey, HasManyGetAssociationsMixin, NonAttribute } from 'sequelize'
import { ForeignKey, NonAttribute } from 'sequelize'
import { Entry } from './Entry'

export class EntryTimelog extends BaseEntity<EntryTimelog> {
declare date: Date
declare duration: number
declare entry_id: ForeignKey<Entry['id']>

declare getEntry: HasManyGetAssociationsMixin<Entry>
declare entry?: NonAttribute<Entry>

declare static associations: {
entry: Association<Entry, EntryTimelog>
}
}

export interface EntryTimelogDO extends BaseDO {
Expand Down

0 comments on commit d33f711

Please sign in to comment.