mirror of
https://github.com/lingble/twenty.git
synced 2025-12-02 05:43:42 +00:00
Fix database experience (#2263)
This commit is contained in:
54
server/scripts/truncate-db.ts
Normal file
54
server/scripts/truncate-db.ts
Normal 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);
|
||||
});
|
||||
Reference in New Issue
Block a user