@dbml/connector 10.4.0-alpha.1 → 10.4.0-alpha.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
+ import { Client } from 'pg';
3
4
  import { scanDirNames } from './testHelpers';
4
5
  import { connector } from '../src/index';
5
6
 
@@ -44,4 +45,76 @@ describe('@dbml/connector', () => {
44
45
  test.each(scanDirNames(__dirname, 'connectors'))('connectors/%s', async (dirName) => {
45
46
  await runTest(path.join(__dirname, 'connectors', dirName));
46
47
  }, 10000);
48
+
49
+ test('postgres preserves composite foreign key column order', async () => {
50
+ const testDir = path.join(__dirname, 'connectors', 'postgres');
51
+ const { connection } = JSON.parse(fs.readFileSync(path.join(testDir, 'connection.json'), 'utf-8'));
52
+ const schema = `dbml_fk_order_${Math.random().toString(16).slice(2)}`;
53
+ const client = new Client({ connectionString: connection });
54
+
55
+ await client.connect();
56
+ try {
57
+ await client.query(`CREATE SCHEMA "${schema}"`);
58
+ await client.query(`
59
+ CREATE TABLE "${schema}".parent (
60
+ zeta integer NOT NULL,
61
+ alpha integer NOT NULL,
62
+ PRIMARY KEY (zeta, alpha)
63
+ );
64
+ CREATE TABLE "${schema}".child (
65
+ id integer PRIMARY KEY,
66
+ a_zeta integer,
67
+ z_alpha integer,
68
+ CONSTRAINT child_parent_fk FOREIGN KEY (a_zeta, z_alpha)
69
+ REFERENCES "${schema}".parent (zeta, alpha)
70
+ );
71
+ `);
72
+
73
+ const schemaJson = await connector.fetchSchemaJson(`${connection}?schemas=${schema}`, 'postgres');
74
+ const ref = schemaJson.refs.find(({ name }) => name === 'child_parent_fk');
75
+
76
+ expect(ref?.endpoints[0].fieldNames).toEqual(['a_zeta', 'z_alpha']);
77
+ expect(ref?.endpoints[1].fieldNames).toEqual(['zeta', 'alpha']);
78
+ } finally {
79
+ await client.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`);
80
+ await client.end();
81
+ }
82
+ });
83
+
84
+ test('postgres keeps array column defaults that are not an ARRAY[...] list', async () => {
85
+ const testDir = path.join(__dirname, 'connectors', 'postgres');
86
+ const { connection } = JSON.parse(fs.readFileSync(path.join(testDir, 'connection.json'), 'utf-8'));
87
+ const schema = `dbml_array_default_${Math.random().toString(16).slice(2)}`;
88
+ const client = new Client({ connectionString: connection });
89
+
90
+ await client.connect();
91
+ try {
92
+ await client.query(`CREATE SCHEMA "${schema}"`);
93
+ await client.query(`
94
+ CREATE TABLE "${schema}".t (
95
+ literal_col varchar[] DEFAULT '{full_name,date_of_birth}',
96
+ empty_literal_col text[] DEFAULT '{}',
97
+ int_literal_col integer[] DEFAULT '{1,2}',
98
+ empty_array_col text[] DEFAULT ARRAY[]::text[],
99
+ function_col integer[] DEFAULT array_fill(0, ARRAY[3]),
100
+ array_col text[] DEFAULT ARRAY['a', 'b']
101
+ );
102
+ `);
103
+
104
+ const schemaJson = await connector.fetchSchemaJson(`${connection}?schemas=${schema}`, 'postgres');
105
+ const dbdefaults = Object.fromEntries(schemaJson.fields[`${schema}.t`].map(({ name, dbdefault }) => [name, dbdefault]));
106
+
107
+ expect(dbdefaults).toEqual({
108
+ literal_col: { type: 'string', value: '{full_name,date_of_birth}' },
109
+ empty_literal_col: { type: 'string', value: '{}' },
110
+ int_literal_col: { type: 'string', value: '{1,2}' },
111
+ empty_array_col: { type: 'expression', value: 'ARRAY[]::text[]' },
112
+ function_col: { type: 'expression', value: 'array_fill(0, ARRAY[3])' },
113
+ array_col: { type: 'expression', value: 'ARRAY[\'a\', \'b\']' },
114
+ });
115
+ } finally {
116
+ await client.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`);
117
+ await client.end();
118
+ }
119
+ });
47
120
  });