From a9c4edb5ebd02851aec93e7aae06258492661e81 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Wed, 5 May 2021 23:17:46 +0800 Subject: [PATCH 1/2] test: Add test for creating primary_key column --- test/integration/index.spec.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/test/integration/index.spec.js b/test/integration/index.spec.js index 04fe7c11..000bc877 100644 --- a/test/integration/index.spec.js +++ b/test/integration/index.spec.js @@ -314,8 +314,7 @@ describe('/tables', async () => { is_nullable: false, comment: 'foo', // Currently no way to test these: - // isPrimaryKey: true, - // isUnique: true, + // is_unique: true }) const { data: columns } = await axios.get(`${URL}/columns`) @@ -330,6 +329,33 @@ describe('/tables', async () => { await axios.delete(`${URL}/columns/${newTable.id}.1`) await axios.delete(`${URL}/tables/${newTable.id}`) }) + it('POST /columns for primary key', async () => { + const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'foo' }) + await axios.post(`${URL}/columns`, { + table_id: newTable.id, + name: 'bar', + type: 'int2', + is_primary_key: true, + }) + + // https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns + const { data: primaryKeys } = await axios.post( + `${URL}/query`, + { query: ` + SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type + FROM pg_index i + JOIN pg_attribute a ON a.attrelid = i.indrelid + AND a.attnum = ANY(i.indkey) + WHERE i.indrelid = '${newTable.name}'::regclass + AND i.indisprimary; + ` } + ) + assert.equal(primaryKeys.length, 1) + assert.equal(primaryKeys[0].attname, 'bar') + + await axios.delete(`${URL}/columns/${newTable.id}.1`) + await axios.delete(`${URL}/tables/${newTable.id}`) + }) it('POST /columns array type', async () => { const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'a' }) await axios.post(`${URL}/columns`, { From bcaead0144eb7e7e775198c0f3754eaa33ce554a Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Wed, 5 May 2021 23:20:45 +0800 Subject: [PATCH 2/2] test: Remove unnecessary column from select --- test/integration/index.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/index.spec.js b/test/integration/index.spec.js index 000bc877..d070a285 100644 --- a/test/integration/index.spec.js +++ b/test/integration/index.spec.js @@ -342,7 +342,7 @@ describe('/tables', async () => { const { data: primaryKeys } = await axios.post( `${URL}/query`, { query: ` - SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type + SELECT a.attname FROM pg_index i JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)