2023-10-14 14:43:45 +03:00
|
|
|
import console from 'console';
|
|
|
|
|
Uniformize datasources (#5196)
## Context
We recently enabled the option to bypass SSL certificate authority
validation when establishing a connection to PostgreSQL. Previously, if
this validation failed, the server would revert to unencrypted traffic.
Now, it maintains encryption even if the SSL certificate check fails. In
the process, we overlooked a few DataSource setups, prompting a review
of DataSource creation within our code.
## Current State
Our DataSource initialization is distributed as follows:
- **Database folder**: Contains 'core', 'metadata', and 'raw'
DataSources. The 'core' and 'metadata' DataSources manage migrations and
static resolver calls to the database. The 'raw' DataSource is utilized
in scripts and commands that require handling both aspects.
- **typeorm.service.ts script**: These DataSources facilitate
multi-schema connections.
## Vision for Discussion
- **SystemSchema (formerly core) DataSource**: Manages system schema
migrations and system resolvers/repos. The 'core' schema will be renamed
to 'system' as the Core API will include parts of the system and
workspace schemas.
- **MetadataSchema DataSource**: Handles metadata schema migrations and
metadata API resolvers/repos.
- **(Dynamic) WorkspaceSchema DataSource**: Will be used in the Twenty
ORM to access a specific workspace schema.
We currently do not support cross-schema joins, so maintaining these
DataSources separately should be feasible. Core API resolvers will
select the appropriate DataSource based on the field context.
- **To be discussed**: The potential need for an AdminDataSource (akin
to 'Raw'), which would be used in commands, setup scripts, and the admin
panel to connect to any database schema without loading any model. This
DataSource should be reserved for cases where utilizing metadata,
system, or workspace entities is impractical.
## In This PR
- Ensuring all existing DataSources are compliant with the SSL update.
- Introducing RawDataSource to eliminate the need for declaring new
DataSource() instances in commands.
2024-04-27 12:43:44 +03:00
|
|
|
import { rawDataSource } from 'src/database/typeorm/raw/raw.datasource';
|
2023-10-14 14:43:45 +03:00
|
|
|
|
Uniformize datasources (#5196)
## Context
We recently enabled the option to bypass SSL certificate authority
validation when establishing a connection to PostgreSQL. Previously, if
this validation failed, the server would revert to unencrypted traffic.
Now, it maintains encryption even if the SSL certificate check fails. In
the process, we overlooked a few DataSource setups, prompting a review
of DataSource creation within our code.
## Current State
Our DataSource initialization is distributed as follows:
- **Database folder**: Contains 'core', 'metadata', and 'raw'
DataSources. The 'core' and 'metadata' DataSources manage migrations and
static resolver calls to the database. The 'raw' DataSource is utilized
in scripts and commands that require handling both aspects.
- **typeorm.service.ts script**: These DataSources facilitate
multi-schema connections.
## Vision for Discussion
- **SystemSchema (formerly core) DataSource**: Manages system schema
migrations and system resolvers/repos. The 'core' schema will be renamed
to 'system' as the Core API will include parts of the system and
workspace schemas.
- **MetadataSchema DataSource**: Handles metadata schema migrations and
metadata API resolvers/repos.
- **(Dynamic) WorkspaceSchema DataSource**: Will be used in the Twenty
ORM to access a specific workspace schema.
We currently do not support cross-schema joins, so maintaining these
DataSources separately should be feasible. Core API resolvers will
select the appropriate DataSource based on the field context.
- **To be discussed**: The potential need for an AdminDataSource (akin
to 'Raw'), which would be used in commands, setup scripts, and the admin
panel to connect to any database schema without loading any model. This
DataSource should be reserved for cases where utilizing metadata,
system, or workspace entities is impractical.
## In This PR
- Ensuring all existing DataSources are compliant with the SSL update.
- Introducing RawDataSource to eliminate the need for declaring new
DataSource() instances in commands.
2024-04-27 12:43:44 +03:00
|
|
|
import { camelToSnakeCase, performQuery } from './utils';
|
|
|
|
|
|
|
|
rawDataSource
|
2023-10-14 12:48:55 +03:00
|
|
|
.initialize()
|
|
|
|
.then(async () => {
|
2023-10-27 18:48:06 +03:00
|
|
|
await performQuery(
|
|
|
|
'CREATE SCHEMA IF NOT EXISTS "public"',
|
|
|
|
'create schema "public"',
|
|
|
|
);
|
2023-10-14 14:43:45 +03:00
|
|
|
await performQuery(
|
|
|
|
'CREATE SCHEMA IF NOT EXISTS "metadata"',
|
|
|
|
'create schema "metadata"',
|
|
|
|
);
|
2023-11-19 20:25:47 +03:00
|
|
|
await performQuery(
|
|
|
|
'CREATE SCHEMA IF NOT EXISTS "core"',
|
|
|
|
'create schema "core"',
|
|
|
|
);
|
2023-10-14 14:43:45 +03:00
|
|
|
await performQuery(
|
2023-12-08 16:43:52 +03:00
|
|
|
'CREATE EXTENSION IF NOT EXISTS "pg_graphql"',
|
2023-10-14 14:43:45 +03:00
|
|
|
'create extension pg_graphql',
|
|
|
|
);
|
|
|
|
|
|
|
|
await performQuery(
|
|
|
|
'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"',
|
|
|
|
'create extension "uuid-ossp"',
|
|
|
|
);
|
|
|
|
|
2023-12-08 16:43:52 +03:00
|
|
|
await performQuery(
|
|
|
|
'CREATE EXTENSION IF NOT EXISTS "postgres_fdw"',
|
|
|
|
'create extension "postgres_fdw"',
|
|
|
|
);
|
|
|
|
|
|
|
|
await performQuery(
|
|
|
|
'CREATE EXTENSION IF NOT EXISTS "wrappers"',
|
|
|
|
'create extension "wrappers"',
|
|
|
|
);
|
|
|
|
|
2023-12-13 16:56:52 +03:00
|
|
|
await performQuery(
|
|
|
|
'CREATE EXTENSION IF NOT EXISTS "mysql_fdw"',
|
|
|
|
'create extension "mysql_fdw"',
|
|
|
|
);
|
|
|
|
|
2023-12-08 16:43:52 +03:00
|
|
|
const supabaseWrappers = [
|
|
|
|
'airtable',
|
|
|
|
'bigQuery',
|
|
|
|
'clickHouse',
|
|
|
|
'firebase',
|
|
|
|
'logflare',
|
|
|
|
's3',
|
|
|
|
'stripe',
|
|
|
|
]; // See https://supabase.github.io/wrappers/
|
|
|
|
|
|
|
|
for (const wrapper of supabaseWrappers) {
|
|
|
|
await performQuery(
|
|
|
|
`
|
|
|
|
CREATE FOREIGN DATA WRAPPER "${wrapper.toLowerCase()}_fdw"
|
|
|
|
HANDLER "${camelToSnakeCase(wrapper)}_fdw_handler"
|
|
|
|
VALIDATOR "${camelToSnakeCase(wrapper)}_fdw_validator";
|
|
|
|
`,
|
|
|
|
`create ${wrapper} "wrappers"`,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-14 23:40:05 +03:00
|
|
|
await performQuery(
|
2023-11-19 20:25:47 +03:00
|
|
|
`COMMENT ON SCHEMA "core" IS '@graphql({"inflect_names": true})';`,
|
2023-10-14 23:40:05 +03:00
|
|
|
'inflect names for graphql',
|
|
|
|
);
|
|
|
|
|
2023-10-14 14:43:45 +03:00
|
|
|
await performQuery(
|
|
|
|
`
|
|
|
|
DROP FUNCTION IF EXISTS graphql;
|
|
|
|
CREATE FUNCTION graphql(
|
|
|
|
"operationName" text default null,
|
|
|
|
query text default null,
|
|
|
|
variables jsonb default null,
|
|
|
|
extensions jsonb default null
|
|
|
|
)
|
|
|
|
returns jsonb
|
|
|
|
language sql
|
|
|
|
as $$
|
|
|
|
select graphql.resolve(
|
|
|
|
query := query,
|
|
|
|
variables := coalesce(variables, '{}'),
|
|
|
|
"operationName" := "operationName",
|
|
|
|
extensions := extensions
|
|
|
|
);
|
|
|
|
$$;
|
|
|
|
`,
|
|
|
|
'create function graphql',
|
|
|
|
);
|
2023-10-14 12:48:55 +03:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error('Error during Data Source initialization:', err);
|
|
|
|
});
|