diff --git a/src/main/db/migrations/0001-add_col.ts b/src/main/db/migrations/0001-add_col.ts index 6db84d8..3121cab 100644 --- a/src/main/db/migrations/0001-add_col.ts +++ b/src/main/db/migrations/0001-add_col.ts @@ -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 => { - 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 => { - await context.removeColumn('entry', 'notes') - } + down: async (): Promise => {} } diff --git a/src/main/db/seed.ts b/src/main/db/seed.ts index 3d48878..c5a0e3a 100644 --- a/src/main/db/seed.ts +++ b/src/main/db/seed.ts @@ -31,7 +31,6 @@ export const seed = async (db: Sequelize): Promise => { defaultValue: DataTypes.UUIDV4, primaryKey: true }, - date: { type: DataTypes.DATE, allowNull: false @@ -44,8 +43,15 @@ export const seed = async (db: Sequelize): Promise => { { 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( { diff --git a/src/main/db/types/Entry.ts b/src/main/db/types/Entry.ts index f05a929..c87e6e9 100644 --- a/src/main/db/types/Entry.ts +++ b/src/main/db/types/Entry.ts @@ -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 { declare label: string declare notes?: string - declare createTimelog: HasManyCreateAssociationMixin declare timelogs?: NonAttribute - - declare static associations: { - timelogs: Association - } } export interface EntryDO extends BaseDO { diff --git a/src/main/db/types/EntryTimelog.ts b/src/main/db/types/EntryTimelog.ts index 02b34d0..4fc5fff 100644 --- a/src/main/db/types/EntryTimelog.ts +++ b/src/main/db/types/EntryTimelog.ts @@ -1,5 +1,5 @@ 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 { @@ -7,12 +7,7 @@ export class EntryTimelog extends BaseEntity { declare duration: number declare entry_id: ForeignKey - declare getEntry: HasManyGetAssociationsMixin declare entry?: NonAttribute - - declare static associations: { - entry: Association - } } export interface EntryTimelogDO extends BaseDO {