The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Fix database experience (#2263)
Lucas Bordeau14ae8da424ec
diff --git a/docs/docs/contributor/local-setup/docker-setup.mdx b/docs/docs/contributor/local-setup/docker-setup.mdxindex 5510720f3b2..30ba2936db5 100644--- a/docs/docs/contributor/local-setup/docker-setup.mdx+++ b/docs/docs/contributor/local-setup/docker-setup.mdx@@ -128,7 +128,7 @@ make shThen run commands through yarn:```bashcd server-yarn database:reset+yarn database:init```diff --git a/docs/docs/contributor/server/basics/workflows.mdx b/docs/docs/contributor/server/basics/workflows.mdxindex f2930bb526a..5b3fc5a2980 100644--- a/docs/docs/contributor/server/basics/workflows.mdx+++ b/docs/docs/contributor/server/basics/workflows.mdx@@ -41,4 +41,21 @@ yarn lint```yarn test-```\ No newline at end of file+```++## Resetting the database++If you want to reset the database, you can run the following command:++```bash+cd server+yarn database:reset+```++:::warning++This will drop the database and re-run the migrations and seed.++Make sure to back up any data you want to keep before running this command.++:::\ No newline at end of filediff --git a/infra/dev/Makefile b/infra/dev/Makefileindex a5fa5e589ed..a83cb95bfca 100644--- a/infra/dev/Makefile+++ b/infra/dev/Makefile@@ -10,7 +10,6 @@ provision-postgres:@docker rm twenty_postgres || true@docker volume rm twenty_db_data || true@docker compose up --build postgres -d- @cd ../../server && yarn database:setup && yarn database:resetup:@docker compose up -ddiff --git a/server/package.json b/server/package.jsonindex 25f4bfcadc7..6e214b705f4 100644--- a/server/package.json+++ b/server/package.json@@ -29,12 +29,13 @@"prisma:migrate": "npx prisma migrate deploy","typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js","typeorm:migrate": "yarn typeorm migration:run -d ./src/metadata/metadata.datasource.ts",- "database:init": "yarn database:setup && yarn database:reset",- "database:setup": "npx ts-node ./scripts/setup-db.ts",+ "database:init": "yarn database:setup && yarn database:seed",+ "database:setup": "npx ts-node ./scripts/setup-db.ts && yarn database:migrate",+ "database:truncate": "npx ts-node ./scripts/truncate-db.ts","database:migrate": "yarn typeorm:migrate && yarn prisma:migrate","database:generate": "yarn prisma:generate","database:seed": "yarn prisma:seed",- "database:reset": "yarn database:generate && yarn database:migrate && yarn database:seed"+ "database:reset": "yarn database:truncate && yarn database:init"},"dependencies": {"@apollo/server": "^4.7.3",diff --git a/server/scripts/setup-db.ts b/server/scripts/setup-db.tsindex 11a4dec06fa..5edab5b0785 100644--- a/server/scripts/setup-db.ts+++ b/server/scripts/setup-db.ts@@ -27,6 +27,10 @@ const performQuery = async (query: string, consoleDescription: string) => {connectionSource.initialize().then(async () => {+ await performQuery(+ 'CREATE SCHEMA IF NOT EXISTS "public"',+ 'create schema "public"',+ );await performQuery('CREATE SCHEMA IF NOT EXISTS "metadata"','create schema "metadata"',diff --git a/server/scripts/truncate-db.ts b/server/scripts/truncate-db.tsnew file mode 100644index 00000000000..8117f6a6283--- /dev/null+++ b/server/scripts/truncate-db.ts@@ -0,0 +1,54 @@+import { ConfigService } from '@nestjs/config';++import console from 'console';++import { config } from 'dotenv';+import { DataSource } from 'typeorm';++config();++const configService = new ConfigService();++export const connectionSource = new DataSource({+ type: 'postgres',+ logging: false,+ url: configService.get<string>('PG_DATABASE_URL'),+});++const performQuery = async (query: string, consoleDescription: string) => {+ try {+ await connectionSource.query(query);+ console.log(`Performed '${consoleDescription}' successfully`);+ } catch (err) {+ console.error(`Failed to perform '${consoleDescription}':`, err);+ }+};++connectionSource+ .initialize()+ .then(async () => {+ await performQuery(+ `+ CREATE OR REPLACE FUNCTION drop_all() RETURNS VOID AS $$+ DECLARE schema_item RECORD;+ BEGIN+ FOR schema_item IN+ SELECT subrequest."name" as schema_name+ FROM (SELECT n.nspname AS "name"+ FROM pg_catalog.pg_namespace n+ WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema') as subrequest+ LOOP+ EXECUTE 'DROP SCHEMA ' || schema_item.schema_name || ' CASCADE';+ END LOOP;+ RETURN;+ END;+ $$ LANGUAGE plpgsql;++ SELECT drop_all ();+ `,+ 'Dropping all schemas...',+ );+ })+ .catch((err) => {+ console.error('Error during Data Source initialization:', err);+ });