From 0ddef31ed872dc103b80ac8fcab7a830801608fb Mon Sep 17 00:00:00 2001 From: kangfenmao Date: Fri, 13 Sep 2024 17:02:07 +0800 Subject: [PATCH] chore: update build process and database configuration. - Updated configuration to exclude additional directories from electron-builder's build process. - Dropped the creation of the "files" table in the database schema. - Improved code organization and extracted the data path into a reusable function. - Updated database migration configuration to use a new migration manager. - Added database migration to create a table for file management. - A migration to remove the "files" table has been applied. --- electron-builder.yml | 4 +- .../migrations/001_create_files_table.sql | 11 ---- src/main/config.ts | 12 +++-- src/main/database/index.ts | 3 +- src/main/database/migrations.ts | 50 +++++++++++++++++++ .../20240912072241-create-files-table.js | 48 ------------------ src/main/database/models/FileModel.ts | 2 +- 7 files changed, 64 insertions(+), 66 deletions(-) delete mode 100644 resources/migrations/001_create_files_table.sql create mode 100644 src/main/database/migrations.ts delete mode 100644 src/main/database/migrations/20240912072241-create-files-table.js diff --git a/electron-builder.yml b/electron-builder.yml index 9aa9b33e..83d4ec35 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -8,8 +8,10 @@ files: - '!{.eslintignore,.eslintrc.cjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}' - '!{.env,.env.*,.npmrc,pnpm-lock.yaml}' - '!{tsconfig.json,tsconfig.node.json,tsconfig.web.json}' - - '!src/*' + - '!src' - '!local' + - '!scripts' + - '!resources' asarUnpack: - resources/** win: diff --git a/resources/migrations/001_create_files_table.sql b/resources/migrations/001_create_files_table.sql deleted file mode 100644 index 837de796..00000000 --- a/resources/migrations/001_create_files_table.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE TABLE IF NOT EXISTS files ( - id TEXT PRIMARY KEY, - name TEXT NOT NULL, - file_name TEXT NOT NULL, - path TEXT NOT NULL, - size INTEGER NOT NULL, - ext TEXT NOT NULL, - type TEXT NOT NULL, - created_at TEXT NOT NULL, - count INTEGER DEFAULT 1 -) diff --git a/src/main/config.ts b/src/main/config.ts index 035eafa4..3072b4bc 100644 --- a/src/main/config.ts +++ b/src/main/config.ts @@ -4,12 +4,16 @@ import { app } from 'electron' import Store from 'electron-store' import path from 'path' -export const DATA_PATH = path.join(app.getPath('userData'), 'Data') - -if (!fs.existsSync(DATA_PATH)) { - fs.mkdirSync(DATA_PATH, { recursive: true }) +const getDataPath = () => { + const dataPath = path.join(app.getPath('userData'), 'Data') + if (!fs.existsSync(dataPath)) { + fs.mkdirSync(dataPath, { recursive: true }) + } + return dataPath } +export const DATA_PATH = getDataPath() + export const appConfig = new Store() export const titleBarOverlayDark = { diff --git a/src/main/database/index.ts b/src/main/database/index.ts index 222734e9..e4ecfaf8 100644 --- a/src/main/database/index.ts +++ b/src/main/database/index.ts @@ -4,6 +4,7 @@ import { Sequelize } from 'sequelize' import { SequelizeStorage, Umzug } from 'umzug' import { DATA_PATH } from '../config' +import migrations from './migrations' const sequelize = new Sequelize({ dialect: 'sqlite', @@ -12,7 +13,7 @@ const sequelize = new Sequelize({ }) const umzug = new Umzug({ - migrations: { glob: 'src/main/database/migrations/*.js' }, + migrations, context: sequelize.getQueryInterface(), storage: new SequelizeStorage({ sequelize, modelName: 'Migration', tableName: 'migrations' }), logger: Logger diff --git a/src/main/database/migrations.ts b/src/main/database/migrations.ts new file mode 100644 index 00000000..1fc0af14 --- /dev/null +++ b/src/main/database/migrations.ts @@ -0,0 +1,50 @@ +import { DataTypes } from 'sequelize' + +export default [ + { + name: '20240912072241-create-files-table', + async up({ context }) { + await context.createTable('files', { + id: { + type: DataTypes.TEXT, + primaryKey: true + }, + name: { + type: DataTypes.TEXT, + allowNull: false + }, + file_name: { + type: DataTypes.TEXT, + allowNull: false + }, + path: { + type: DataTypes.TEXT, + allowNull: false + }, + size: { + type: DataTypes.INTEGER, + allowNull: false + }, + ext: { + type: DataTypes.TEXT, + allowNull: false + }, + type: { + type: DataTypes.TEXT, + allowNull: false + }, + created_at: { + type: DataTypes.TEXT, + allowNull: false + }, + count: { + type: DataTypes.INTEGER, + defaultValue: 1 + } + }) + }, + async down({ context }) { + await context.dropTable('files') + } + } +] diff --git a/src/main/database/migrations/20240912072241-create-files-table.js b/src/main/database/migrations/20240912072241-create-files-table.js deleted file mode 100644 index 0a94d6b1..00000000 --- a/src/main/database/migrations/20240912072241-create-files-table.js +++ /dev/null @@ -1,48 +0,0 @@ -const { Sequelize } = require('sequelize') - -async function up({ context: queryInterface }) { - await queryInterface.createTable('files', { - id: { - type: Sequelize.TEXT, - primaryKey: true - }, - name: { - type: Sequelize.TEXT, - allowNull: false - }, - file_name: { - type: Sequelize.TEXT, - allowNull: false - }, - path: { - type: Sequelize.TEXT, - allowNull: false - }, - size: { - type: Sequelize.INTEGER, - allowNull: false - }, - ext: { - type: Sequelize.TEXT, - allowNull: false - }, - type: { - type: Sequelize.TEXT, - allowNull: false - }, - created_at: { - type: Sequelize.TEXT, - allowNull: false - }, - count: { - type: Sequelize.INTEGER, - defaultValue: 1 - } - }) -} - -async function down({ context: queryInterface }) { - await queryInterface.dropTable('files') -} - -module.exports = { up, down } diff --git a/src/main/database/models/FileModel.ts b/src/main/database/models/FileModel.ts index 5fe783fb..b25102a5 100644 --- a/src/main/database/models/FileModel.ts +++ b/src/main/database/models/FileModel.ts @@ -1,6 +1,6 @@ +import { FileMetadata, FileType } from '@types' import { DataTypes, Model } from 'sequelize' -import { FileMetadata, FileType } from '../../../renderer/src/types' import sequelize from '..' class FileModel extends Model implements FileMetadata {