mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 09:04:07 -08:00
bed04ec122
## Summary Postgres columns can be - [generated as identity](https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-identity-column/) - [generated by a custom expression](https://www.postgresql.org/docs/current/ddl-generated-columns.html) In these 2 cases, the column is not required when inserting a new row. This PR makes sure these types of column are not marked required in n8n. ### How to test 1. Create a Postgres table with all types of generated columns: for version >= 10 ```sql CREATE TABLE "public"."test_table" ( "id" int8 NOT NULL DEFAULT nextval('test_table_id_seq'::regclass), "identity_id" bigint GENERATED ALWAYS AS IDENTITY, "id_plus" numeric GENERATED ALWAYS AS (id + 5) STORED, "title" varchar NOT NULL, "created_at" timestamp DEFAULT now(), PRIMARY KEY ("id") ) ``` Before 10 you have to use serial or bigserial types: ```sql CREATE TABLE distributors ( did serial not null primary key, name varchar(40) NOT NULL CHECK (name <> '') ); ``` 2. Add a postgres node to canvas and try to insert data without the generated columns 3. Should successfully insert More info in Linear/Github issue ⬇️ ## Related tickets and issues - fixes #7084 - https://linear.app/n8n/issue/NODE-816/rmc-not-all-id-fields-should-be-required - https://linear.app/n8n/issue/NODE-681/postgres-cant-map-automatically-if-database-requires-a-field ## Review / Merge checklist - [ ] PR title and summary are descriptive. **Remember, the title automatically goes into the changelog. Use `(no-changelog)` otherwise.** ([conventions](https://github.com/n8n-io/n8n/blob/master/.github/pull_request_title_conventions.md)) - [ ] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up ticket created. - [ ] Tests included. > A bug is not considered fixed, unless a test is added to prevent it from happening again. > A feature is not complete without tests. --------- Co-authored-by: Michael Kret <michael.k@radency.com>
100 lines
2.8 KiB
TypeScript
100 lines
2.8 KiB
TypeScript
import type { ILoadOptionsFunctions, ResourceMapperFields, FieldType } from 'n8n-workflow';
|
|
import { getEnumValues, getEnums, getTableSchema, uniqueColumns } from '../helpers/utils';
|
|
import { configurePostgres } from '../transport';
|
|
|
|
const fieldTypeMapping: Partial<Record<FieldType, string[]>> = {
|
|
string: ['text', 'varchar', 'character varying', 'character', 'char'],
|
|
number: [
|
|
'integer',
|
|
'smallint',
|
|
'bigint',
|
|
'decimal',
|
|
'numeric',
|
|
'real',
|
|
'double precision',
|
|
'smallserial',
|
|
'serial',
|
|
'bigserial',
|
|
],
|
|
boolean: ['boolean'],
|
|
dateTime: [
|
|
'timestamp',
|
|
'date',
|
|
'timestampz',
|
|
'timestamp without time zone',
|
|
'timestamp with time zone',
|
|
],
|
|
time: ['time', 'time without time zone', 'time with time zone'],
|
|
object: ['json', 'jsonb'],
|
|
options: ['enum', 'USER-DEFINED'],
|
|
array: ['ARRAY'],
|
|
};
|
|
|
|
function mapPostgresType(postgresType: string): FieldType {
|
|
let mappedType: FieldType = 'string';
|
|
|
|
for (const t of Object.keys(fieldTypeMapping)) {
|
|
const postgresTypes = fieldTypeMapping[t as FieldType];
|
|
if (postgresTypes?.includes(postgresType)) {
|
|
mappedType = t as FieldType;
|
|
}
|
|
}
|
|
return mappedType;
|
|
}
|
|
|
|
export async function getMappingColumns(
|
|
this: ILoadOptionsFunctions,
|
|
): Promise<ResourceMapperFields> {
|
|
const credentials = await this.getCredentials('postgres');
|
|
|
|
const { db, sshClient } = await configurePostgres(credentials);
|
|
|
|
const schema = this.getNodeParameter('schema', 0, {
|
|
extractValue: true,
|
|
}) as string;
|
|
|
|
const table = this.getNodeParameter('table', 0, {
|
|
extractValue: true,
|
|
}) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0, {
|
|
extractValue: true,
|
|
}) as string;
|
|
|
|
try {
|
|
const columns = await getTableSchema(db, schema, table, { getColumnsForResourceMapper: true });
|
|
const unique = operation === 'upsert' ? await uniqueColumns(db, table, schema) : [];
|
|
const enumInfo = await getEnums(db);
|
|
const fields = await Promise.all(
|
|
columns.map(async (col) => {
|
|
const canBeUsedToMatch =
|
|
operation === 'upsert' ? unique.some((u) => u.attname === col.column_name) : true;
|
|
const type = mapPostgresType(col.data_type);
|
|
const options =
|
|
type === 'options' ? getEnumValues(enumInfo, col.udt_name as string) : undefined;
|
|
const hasDefault = Boolean(col.column_default);
|
|
const isGenerated = col.is_generated === 'ALWAYS' || col.identity_generation === 'ALWAYS';
|
|
const nullable = col.is_nullable === 'YES';
|
|
return {
|
|
id: col.column_name,
|
|
displayName: col.column_name,
|
|
required: !nullable && !hasDefault && !isGenerated,
|
|
defaultMatch: (col.column_name === 'id' && canBeUsedToMatch) || false,
|
|
display: true,
|
|
type,
|
|
canBeUsedToMatch,
|
|
options,
|
|
};
|
|
}),
|
|
);
|
|
return { fields };
|
|
} catch (error) {
|
|
throw error;
|
|
} finally {
|
|
if (sshClient) {
|
|
sshClient.end();
|
|
}
|
|
await db.$pool.end();
|
|
}
|
|
}
|