From 7c240e3f2d3a408129e665b327101ec815fe3f2e Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sun, 7 Nov 2021 09:37:30 +0100 Subject: [PATCH] Refactoring --- README.md | 12 ++++++++++-- apps/api/src/services/data-gathering.service.ts | 7 +++---- .../interfaces/symbol-profile.interface.ts | 12 ++++-------- apps/api/src/services/symbol-profile.service.ts | 15 ++++++++------- package.json | 3 ++- .../migration.sql | 2 ++ prisma/schema.prisma | 2 +- 7 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 prisma/migrations/20211107082008_added_symbol_mapping_to_symbol_profile/migration.sql diff --git a/README.md b/README.md index a4ed80f52..03f8dc8a2 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ docker-compose -f docker/docker-compose-build-local.yml up Run the following command to setup the database once Ghostfolio is running: ```bash -docker-compose -f docker/docker-compose-build-local.yml exec ghostfolio yarn setup:database +docker-compose -f docker/docker-compose-build-local.yml exec ghostfolio yarn database:setup ``` ### Fetch Historical Data @@ -112,6 +112,14 @@ Open http://localhost:3333 in your browser and accomplish these steps: 1. Go to the _Admin Control Panel_ and click _Gather All Data_ to fetch historical data 1. Click _Sign out_ and check out the _Live Demo_ +### Migrate Database + +With the following command you can keep your database schema in sync after a Ghostfolio version update: + +```bash +docker-compose -f docker/docker-compose-build-local.yml exec ghostfolio yarn database:migrate +``` + ## Development ### Prerequisites @@ -126,7 +134,7 @@ Open http://localhost:3333 in your browser and accomplish these steps: 1. Run `cd docker` 1. Run `docker compose up -d` to start [PostgreSQL](https://www.postgresql.org) and [Redis](https://redis.io) 1. Run `cd -` to go back to the project root directory -1. Run `yarn setup:database` to initialize the database schema and populate your database with (example) data +1. Run `yarn database:setup` to initialize the database schema and populate your database with (example) data 1. Start server and client (see [_Development_](#Development)) 1. Login as _Admin_ with the following _Security Token_: `ae76872ae8f3419c6d6f64bf51888ecbcc703927a342d815fafe486acdb938da07d0cf44fca211a0be74a423238f535362d390a41e81e633a9ce668a6e31cdf9` 1. Go to the _Admin Control Panel_ and click _Gather All Data_ to fetch historical data diff --git a/apps/api/src/services/data-gathering.service.ts b/apps/api/src/services/data-gathering.service.ts index bb69437f4..1e3ecb478 100644 --- a/apps/api/src/services/data-gathering.service.ts +++ b/apps/api/src/services/data-gathering.service.ts @@ -141,10 +141,9 @@ export class DataGatheringService { ); for (const [symbol, response] of Object.entries(currentData)) { - const symbolMapping = - symbolProfiles.find((symbolProfile) => { - return symbolProfile.symbol === symbol; - })?.settings?.symbolMapping ?? {}; + const symbolMapping = symbolProfiles.find((symbolProfile) => { + return symbolProfile.symbol === symbol; + })?.symbolMapping; for (const dataEnhancer of this.dataEnhancers) { try { diff --git a/apps/api/src/services/interfaces/symbol-profile.interface.ts b/apps/api/src/services/interfaces/symbol-profile.interface.ts index 954132ff4..80e9da1e7 100644 --- a/apps/api/src/services/interfaces/symbol-profile.interface.ts +++ b/apps/api/src/services/interfaces/symbol-profile.interface.ts @@ -5,18 +5,14 @@ import { AssetClass, AssetSubClass, DataSource } from '@prisma/client'; export interface EnhancedSymbolProfile { assetClass: AssetClass; assetSubClass: AssetSubClass; + countries: Country[]; createdAt: Date; currency: string | null; dataSource: DataSource; id: string; name: string | null; - updatedAt: Date; - settings?: SymbolProfileSettings; - symbol: string; - countries: Country[]; sectors: Sector[]; -} - -export interface SymbolProfileSettings { - symbolMapping: { [key: string]: string }; + symbol: string; + symbolMapping?: { [key: string]: string }; + updatedAt: Date; } diff --git a/apps/api/src/services/symbol-profile.service.ts b/apps/api/src/services/symbol-profile.service.ts index 1d8541c4a..acf8118e2 100644 --- a/apps/api/src/services/symbol-profile.service.ts +++ b/apps/api/src/services/symbol-profile.service.ts @@ -1,7 +1,4 @@ -import { - EnhancedSymbolProfile, - SymbolProfileSettings -} from '@ghostfolio/api/services/interfaces/symbol-profile.interface'; +import { EnhancedSymbolProfile } from '@ghostfolio/api/services/interfaces/symbol-profile.interface'; import { PrismaService } from '@ghostfolio/api/services/prisma.service'; import { UNKNOWN_KEY } from '@ghostfolio/common/config'; import { Country } from '@ghostfolio/common/interfaces/country.interface'; @@ -33,7 +30,7 @@ export class SymbolProfileService { ...symbolProfile, countries: this.getCountries(symbolProfile), sectors: this.getSectors(symbolProfile), - settings: this.getSettings(symbolProfile) + symbolMapping: this.getSymbolMapping(symbolProfile) })); } @@ -66,7 +63,11 @@ export class SymbolProfileService { ); } - private getSettings(symbolProfile: SymbolProfile): SymbolProfileSettings { - return { symbolMapping: symbolProfile.settings['symbolMapping'] }; + private getSymbolMapping(symbolProfile: SymbolProfile) { + return ( + (symbolProfile['symbolMapping'] as { + [key: string]: string; + }) ?? {} + ); } } diff --git a/package.json b/package.json index 629c49be7..be0563899 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,10 @@ "database:format-schema": "prisma format", "database:generate-typings": "prisma generate", "database:gui": "prisma studio", + "database:migrate": "prisma migrate deploy", "database:push": "prisma db push", "database:seed": "prisma db seed --preview-feature", + "database:setup": "yarn database:push && yarn database:seed", "dep-graph": "nx dep-graph", "e2e": "ng e2e", "format": "nx format:write", @@ -33,7 +35,6 @@ "nx": "nx", "postinstall": "prisma generate && ngcc --properties es2015 browser module main", "replace-placeholders-in-build": "node ./replace.build.js", - "setup:database": "yarn database:push && yarn database:seed", "start": "node dist/apps/api/main", "start:client": "ng serve client --hmr -o", "start:prod": "node apps/api/main", diff --git a/prisma/migrations/20211107082008_added_symbol_mapping_to_symbol_profile/migration.sql b/prisma/migrations/20211107082008_added_symbol_mapping_to_symbol_profile/migration.sql new file mode 100644 index 000000000..a6cdeb92c --- /dev/null +++ b/prisma/migrations/20211107082008_added_symbol_mapping_to_symbol_profile/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "SymbolProfile" ADD COLUMN "symbolMapping" JSONB; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 8bfcea243..d087f5463 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -127,10 +127,10 @@ model SymbolProfile { id String @id @default(uuid()) name String? Order Order[] - settings Json? updatedAt DateTime @updatedAt sectors Json? symbol String + symbolMapping Json? @@unique([dataSource, symbol]) }