Fix database experience (#2263)

This commit is contained in:
Lucas Bordeau
2023-10-27 17:48:06 +02:00
committed by GitHub
parent 3c6ce75606
commit 14ae8da424
6 changed files with 81 additions and 6 deletions

View File

@@ -128,7 +128,7 @@ make sh
Then run commands through yarn:
```bash
cd server
yarn database:reset
yarn database:init
```

View File

@@ -42,3 +42,20 @@ yarn lint
```
yarn test
```
## 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.
:::

View File

@@ -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:reset
up:
@docker compose up -d

View File

@@ -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",

View File

@@ -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"',

View File

@@ -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);
});