Files
twenty/packages/twenty-server/scripts/setup-db.ts
Paul Rastoin 4fbdfb6abc Activate v2 default seed (#14660)
## Introduction
After enabling flag by default got following errors:
```ts
Test Suites: 48 failed, 1 skipped, 97 passed, 145 of 146 total
Tests:       499 failed, 1 skipped, 644 passed, 1144 total
Snapshots:   61 failed, 133 passed, 194 total
Time:        363.226 s
Ran all test suites.
```

## From
<img width="2952" height="1510" alt="image"
src="https://github.com/user-attachments/assets/7e3b20c6-2552-40a7-90bb-2d7b3002c895"
/>

## To
<img width="3134" height="1510" alt="image"
src="https://github.com/user-attachments/assets/4fc9ada4-3c14-4333-a1db-11daf87db8d6"
/>

There's a huge test bundle in the latest shard that we could split up

## Notes
- Set as failing morph relation field rename as for the moment we do not
handle relation field mutation
- fixed the object update and creation validation adding label
identifier field metadata id checks
- and more

Some integrations tests are still on the v1 ( they have before and after
all disabling and re-enabling the flat ) but mainly we now have more
coverage on the v2 than the v1.
Mainly related records, uniqueness have to be migrated the v2 and so
tests too
2025-09-26 16:05:09 +02:00

100 lines
2.4 KiB
TypeScript

import console from 'console';
import { rawDataSource } from 'src/database/typeorm/raw/raw.datasource';
import { camelToSnakeCase, performQuery } from './utils';
rawDataSource
.initialize()
.then(async () => {
await performQuery(
'CREATE SCHEMA IF NOT EXISTS "public"',
'create schema "public"',
);
await performQuery(
'CREATE SCHEMA IF NOT EXISTS "core"',
'create schema "core"',
);
await performQuery(
'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"',
'create extension "uuid-ossp"',
);
await performQuery(
'CREATE EXTENSION IF NOT EXISTS "unaccent"',
'create extension "unaccent"',
);
await performQuery(
`CREATE OR REPLACE FUNCTION public.unaccent_immutable(input text)
RETURNS text
LANGUAGE sql
IMMUTABLE
AS $$
SELECT public.unaccent('public.unaccent'::regdictionary, input)
$$;`,
'create immutable unaccent wrapper function',
);
// We paused the work on FDW
if (process.env.IS_FDW_ENABLED !== 'true') {
return;
}
await performQuery(
'CREATE EXTENSION IF NOT EXISTS "postgres_fdw"',
'create extension "postgres_fdw"',
);
await performQuery(
'CREATE EXTENSION IF NOT EXISTS "wrappers"',
'create extension "wrappers"',
);
await performQuery(
'CREATE EXTENSION IF NOT EXISTS "mysql_fdw"',
'create extension "mysql_fdw"',
);
const supabaseWrappers = [
'airtable',
'bigQuery',
'clickHouse',
'firebase',
'logflare',
's3',
'stripe',
]; // See https://supabase.github.io/wrappers/
for (const wrapper of supabaseWrappers) {
if (await checkForeignDataWrapperExists(`${wrapper.toLowerCase()}_fdw`)) {
continue;
}
await performQuery(
`
CREATE FOREIGN DATA WRAPPER "${wrapper.toLowerCase()}_fdw"
HANDLER "${camelToSnakeCase(wrapper)}_fdw_handler"
VALIDATOR "${camelToSnakeCase(wrapper)}_fdw_validator";
`,
`create ${wrapper} "wrappers"`,
true,
true,
);
}
})
.catch((err) => {
console.error('Error during Data Source initialization:', err);
});
async function checkForeignDataWrapperExists(
wrapperName: string,
): Promise<boolean> {
const result = await rawDataSource.query(
`SELECT 1 FROM pg_foreign_data_wrapper WHERE fdwname = $1`,
[wrapperName],
);
return result.length > 0;
}