Skip to content

Commit c817ed7

Browse files
Blanca Rosasjaolanlo
authored andcommitted
fix: changing primary key in db
1 parent 56e4644 commit c817ed7

11 files changed

Lines changed: 90 additions & 113 deletions

File tree

data/init.sql

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,22 @@ CREATE TABLE IF NOT EXISTS domains(
100100
PRIMARY KEY (name, domain_suffix, tenant_id)
101101
);
102102
CREATE TABLE IF NOT EXISTS proxyconfigs(
103-
proxy_config_name citext NOT NULL,
104-
access_info varchar(256),
103+
access_info varchar(256) NOT NULL,
105104
info_format integer,
106105
port integer,
107106
network_dns_suffix varchar(192),
108107
creation_date timestamp,
109108
tenant_id varchar(36) NOT NULL,
110-
PRIMARY KEY (proxy_config_name, tenant_id)
109+
PRIMARY KEY (access_info, tenant_id)
111110
);
112111
CREATE TABLE IF NOT EXISTS profiles_proxyconfigs(
113-
proxy_config_name citext,
112+
access_info citext,
114113
profile_name citext,
115-
FOREIGN KEY (proxy_config_name,tenant_id) REFERENCES proxyconfigs(proxy_config_name,tenant_id),
114+
FOREIGN KEY (access_info,tenant_id) REFERENCES proxyconfigs(access_info,tenant_id),
116115
FOREIGN KEY (profile_name,tenant_id) REFERENCES profiles(profile_name,tenant_id),
117116
priority integer,
118117
creation_date timestamp,
119118
created_by varchar(40),
120119
tenant_id varchar(36) NOT NULL,
121-
PRIMARY KEY (proxy_config_name, profile_name, priority, tenant_id)
120+
PRIMARY KEY (access_info, profile_name, priority, tenant_id)
122121
);

src/data/postgres/tables/profileProxyConfigs.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('profileproxyconfig tests', () => {
3838
`
3939
SELECT
4040
priority as "priority",
41-
proxy_config_name as "profileName"
41+
access_info as "configName"
4242
FROM profiles_proxyconfigs
4343
WHERE profile_name = $1 and tenant_id = $2
4444
ORDER BY priority`,
@@ -82,7 +82,7 @@ describe('profileproxyconfig tests', () => {
8282
expect(querySpy).toBeCalledTimes(1)
8383
expect(querySpy).toBeCalledWith(`
8484
INSERT INTO
85-
profiles_proxyconfigs (proxy_config_name, profile_name, priority, tenant_id)
85+
profiles_proxyconfigs (access_info, profile_name, priority, tenant_id)
8686
VALUES ('proxyConfig', 'profileName', '1', '')`)
8787
})
8888
test('should NOT insert when no proxyconfigs', async () => {

src/data/postgres/tables/profileProxyConfigs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class ProfileProxyConfigsTable implements IProfileProxyConfigsTable {
2727
`
2828
SELECT
2929
priority as "priority",
30-
proxy_config_name as "profileName"
30+
access_info as "configName"
3131
FROM profiles_proxyconfigs
3232
WHERE profile_name = $1 and tenant_id = $2
3333
ORDER BY priority`,
@@ -62,7 +62,7 @@ export class ProfileProxyConfigsTable implements IProfileProxyConfigsTable {
6262
format(
6363
`
6464
INSERT INTO
65-
profiles_proxyconfigs (proxy_config_name, profile_name, priority, tenant_id)
65+
profiles_proxyconfigs (access_info, profile_name, priority, tenant_id)
6666
VALUES %L`,
6767
configs
6868
)

src/data/postgres/tables/profiles.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe('profiles tests', () => {
131131
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
132132
ip_sync_enabled as "ipSyncEnabled",
133133
local_wifi_sync_enabled as "localWifiSyncEnabled",
134-
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
134+
COALESCE(json_agg(json_build_object('configName',pc.access_info, 'priority', pc.priority)) FILTER (WHERE pc.access_info IS NOT NULL), '[]') AS "proxyConfigs"
135135
FROM profiles p
136136
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
137137
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id
@@ -191,7 +191,7 @@ describe('profiles tests', () => {
191191
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
192192
ip_sync_enabled as "ipSyncEnabled",
193193
local_wifi_sync_enabled as "localWifiSyncEnabled",
194-
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
194+
COALESCE(json_agg(json_build_object('configName',pc.access_info, 'priority', pc.priority)) FILTER (WHERE pc.access_info IS NOT NULL), '[]') AS "proxyConfigs"
195195
FROM profiles p
196196
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
197197
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id

src/data/postgres/tables/profiles.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class ProfilesTable implements IProfilesTable {
7979
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
8080
ip_sync_enabled as "ipSyncEnabled",
8181
local_wifi_sync_enabled as "localWifiSyncEnabled",
82-
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
82+
COALESCE(json_agg(json_build_object('configName',pc.access_info, 'priority', pc.priority)) FILTER (WHERE pc.access_info IS NOT NULL), '[]') AS "proxyConfigs"
8383
FROM profiles p
8484
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
8585
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id
@@ -143,7 +143,7 @@ export class ProfilesTable implements IProfilesTable {
143143
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
144144
ip_sync_enabled as "ipSyncEnabled",
145145
local_wifi_sync_enabled as "localWifiSyncEnabled",
146-
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
146+
COALESCE(json_agg(json_build_object('configName',pc.access_info, 'priority', pc.priority)) FILTER (WHERE pc.access_info IS NOT NULL), '[]') AS "proxyConfigs"
147147
FROM profiles p
148148
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
149149
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id

src/data/postgres/tables/proxyConfigs.test.ts

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ describe('proxy configs tests', () => {
2424
let proxyConfigsTable: ProxyConfigsTable
2525
let querySpy: SpyInstance<any>
2626
let proxyConfig: ProxyConfig
27-
const profileName = 'profileName'
27+
const accessInfo = 'profileName'
2828
beforeEach(() => {
2929
proxyConfig = {
30-
proxyName: 'proxy profile',
3130
accessInfo: 'proxy.com',
3231
infoFormat: 201, // FQDN (201)
3332
port: 1000,
@@ -96,16 +95,15 @@ describe('proxy configs tests', () => {
9695
expect(querySpy).toBeCalledTimes(1)
9796
expect(querySpy).toBeCalledWith(
9897
`
99-
SELECT
100-
proxy_config_name as "proxyName",
98+
SELECT
10199
access_info as "accessInfo",
102100
info_format as "infoFormat",
103101
port as "port",
104102
network_dns_suffix as "networkDnsSuffix",
105103
tenant_id as "tenantId"
106104
FROM proxyconfigs
107105
WHERE tenant_id = $3
108-
ORDER BY proxy_config_name
106+
ORDER BY access_info
109107
LIMIT $1 OFFSET $2`,
110108
[
111109
DEFAULT_TOP,
@@ -117,38 +115,37 @@ describe('proxy configs tests', () => {
117115
test('should get by name', async () => {
118116
const rows = [{}]
119117
querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: rows.length })
120-
const result = await proxyConfigsTable.getByName(profileName)
118+
const result = await proxyConfigsTable.getByName(accessInfo)
121119
expect(result).toStrictEqual(rows[0])
122120
expect(querySpy).toBeCalledTimes(1)
123121
expect(querySpy).toBeCalledWith(
124122
`
125-
SELECT
126-
proxy_config_name as "proxyName",
123+
SELECT
127124
access_info as "accessInfo",
128125
info_format as "infoFormat",
129126
port as "port",
130127
network_dns_suffix as "networkDnsSuffix",
131128
tenant_id as "tenantId"
132129
FROM proxyconfigs
133-
WHERE proxy_config_name = $1 and tenant_id = $2`,
134-
[profileName, '']
130+
WHERE access_info = $1 and tenant_id = $2`,
131+
[accessInfo, '']
135132
)
136133
})
137134
test('should NOT get by name when no profiles exists', async () => {
138135
querySpy.mockResolvedValueOnce({ rows: [], rowCount: 0 })
139-
const result = await proxyConfigsTable.getByName(profileName)
136+
const result = await proxyConfigsTable.getByName(accessInfo)
140137
expect(result).toBeNull()
141138
})
142139
test('should check profile exist', async () => {
143140
querySpy.mockResolvedValueOnce({ rows: [], rowCount: 0 })
144-
const result = await proxyConfigsTable.checkProfileExits(profileName)
141+
const result = await proxyConfigsTable.checkProfileExits(accessInfo)
145142
expect(querySpy).toBeCalledTimes(1)
146143
expect(querySpy).toBeCalledWith(
147144
`
148145
SELECT 1
149146
FROM proxyconfigs
150-
WHERE proxy_config_name = $1 and tenant_id = $2`,
151-
[profileName, '']
147+
WHERE access_info = $1 and tenant_id = $2`,
148+
[accessInfo, '']
152149
)
153150
expect(result).toBe(false)
154151
})
@@ -157,38 +154,38 @@ describe('proxy configs tests', () => {
157154
test('should delete', async () => {
158155
let count = 0
159156
querySpy.mockImplementation(() => ({ rows: [], rowCount: count++ }))
160-
const result = await proxyConfigsTable.delete(profileName)
157+
const result = await proxyConfigsTable.delete(accessInfo)
161158
expect(result).toBeTruthy()
162159
expect(querySpy).toBeCalledTimes(2)
163160
expect(querySpy).toHaveBeenNthCalledWith(
164161
1,
165162
`
166163
SELECT 1
167164
FROM profiles_proxyconfigs
168-
WHERE proxy_config_name = $1 and tenant_id = $2`,
169-
[profileName, '']
165+
WHERE access_info = $1 and tenant_id = $2`,
166+
[accessInfo, '']
170167
)
171168
expect(querySpy).toHaveBeenNthCalledWith(
172169
2,
173170
`
174171
DELETE
175172
FROM proxyconfigs
176-
WHERE proxy_config_name = $1 and tenant_id = $2`,
177-
[profileName, '']
173+
WHERE access_info = $1 and tenant_id = $2`,
174+
[accessInfo, '']
178175
)
179176
})
180177
test('should NOT delete when relationship still exists to profile', async () => {
181178
querySpy.mockResolvedValueOnce({ rows: [], rowCount: 1 })
182-
await expect(proxyConfigsTable.delete(profileName)).rejects.toThrow(
183-
NETWORK_CONFIG_DELETION_FAILED_CONSTRAINT('Proxy', profileName)
179+
await expect(proxyConfigsTable.delete(accessInfo)).rejects.toThrow(
180+
NETWORK_CONFIG_DELETION_FAILED_CONSTRAINT('Proxy', accessInfo)
184181
)
185182
expect(querySpy).toBeCalledTimes(1)
186183
expect(querySpy).toBeCalledWith(
187184
`
188185
SELECT 1
189186
FROM profiles_proxyconfigs
190-
WHERE proxy_config_name = $1 and tenant_id = $2`,
191-
[profileName, '']
187+
WHERE access_info = $1 and tenant_id = $2`,
188+
[accessInfo, '']
192189
)
193190
})
194191
test('should NOT delete when constraint violation', async () => {
@@ -201,8 +198,8 @@ describe('proxy configs tests', () => {
201198
;(err as any).code = '23503'
202199
throw err
203200
})
204-
await expect(proxyConfigsTable.delete(profileName)).rejects.toThrow(
205-
NETWORK_CONFIG_DELETION_FAILED_CONSTRAINT('Proxy', profileName)
201+
await expect(proxyConfigsTable.delete(accessInfo)).rejects.toThrow(
202+
NETWORK_CONFIG_DELETION_FAILED_CONSTRAINT('Proxy', accessInfo)
206203
)
207204
})
208205
test('should NOT delete when unknown error', async () => {
@@ -213,8 +210,8 @@ describe('proxy configs tests', () => {
213210
}
214211
throw new Error('unknown')
215212
})
216-
await expect(proxyConfigsTable.delete(profileName)).rejects.toThrow(
217-
API_UNEXPECTED_EXCEPTION(`Delete proxy configuration : ${profileName}`)
213+
await expect(proxyConfigsTable.delete(accessInfo)).rejects.toThrow(
214+
API_UNEXPECTED_EXCEPTION(`Delete proxy configuration : ${accessInfo}`)
218215
)
219216
})
220217
})
@@ -226,15 +223,14 @@ describe('proxy configs tests', () => {
226223
const result = await proxyConfigsTable.insert(proxyConfig)
227224

228225
expect(result).toBe(proxyConfig)
229-
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.proxyName, proxyConfig.tenantId)
226+
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.accessInfo, proxyConfig.tenantId)
230227
expect(querySpy).toBeCalledTimes(1)
231228
expect(querySpy).toBeCalledWith(
232229
`
233230
INSERT INTO proxyconfigs
234-
(proxy_config_name, access_info, info_format, port, network_dns_suffix, creation_date, tenant_id)
235-
values($1, $2, $3, $4, $5, $6, $7)`,
231+
(access_info, info_format, port, network_dns_suffix, creation_date, tenant_id)
232+
values($1, $2, $3, $4, $5, $6)`,
236233
[
237-
proxyConfig.proxyName,
238234
proxyConfig.accessInfo,
239235
proxyConfig.infoFormat,
240236
proxyConfig.port,
@@ -256,13 +252,13 @@ describe('proxy configs tests', () => {
256252
test('should NOT insert when duplicate name', async () => {
257253
querySpy.mockRejectedValueOnce({ code: '23505' })
258254
await expect(proxyConfigsTable.insert(proxyConfig)).rejects.toThrow(
259-
NETWORK_CONFIG_INSERTION_FAILED_DUPLICATE('Proxy', proxyConfig.proxyName)
255+
NETWORK_CONFIG_INSERTION_FAILED_DUPLICATE('Proxy', proxyConfig.accessInfo)
260256
)
261257
})
262258
test('should NOT insert when unexpected exception', async () => {
263259
querySpy.mockRejectedValueOnce(new Error('unknown'))
264260
await expect(proxyConfigsTable.insert(proxyConfig)).rejects.toThrow(
265-
NETWORK_CONFIG_ERROR('Proxy', proxyConfig.proxyName)
261+
NETWORK_CONFIG_ERROR('Proxy', proxyConfig.accessInfo)
266262
)
267263
})
268264
})
@@ -273,15 +269,14 @@ describe('proxy configs tests', () => {
273269
getByNameSpy.mockResolvedValue(proxyConfig)
274270
const result = await proxyConfigsTable.update(proxyConfig)
275271
expect(result).toBe(proxyConfig)
276-
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.proxyName, proxyConfig.tenantId)
272+
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.accessInfo, proxyConfig.tenantId)
277273
expect(querySpy).toBeCalledTimes(1)
278274
expect(querySpy).toBeCalledWith(
279275
`
280276
UPDATE proxyconfigs
281-
SET access_info=$2, info_format=$3, port=$4, network_dns_suffix=$5
282-
WHERE proxy_config_name=$1 and tenant_id = $6`,
277+
SET info_format=$2, port=$3, network_dns_suffix=$4
278+
WHERE access_info=$1 and tenant_id = $5`,
283279
[
284-
proxyConfig.proxyName,
285280
proxyConfig.accessInfo,
286281
proxyConfig.infoFormat,
287282
proxyConfig.port,
@@ -307,7 +302,7 @@ describe('proxy configs tests', () => {
307302
const getByNameSpy = spyOn(proxyConfigsTable, 'getByName')
308303
getByNameSpy.mockResolvedValue(proxyConfig)
309304
await expect(proxyConfigsTable.update(proxyConfig)).rejects.toThrow(
310-
NETWORK_CONFIG_ERROR('Proxy', proxyConfig.proxyName)
305+
NETWORK_CONFIG_ERROR('Proxy', proxyConfig.accessInfo)
311306
)
312307
})
313308

@@ -316,15 +311,14 @@ describe('proxy configs tests', () => {
316311
const getByNameSpy = spyOn(proxyConfigsTable, 'getByName')
317312
getByNameSpy.mockResolvedValue(proxyConfig)
318313
await expect(proxyConfigsTable.update(proxyConfig)).rejects.toThrow(CONCURRENCY_MESSAGE)
319-
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.proxyName, proxyConfig.tenantId)
314+
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.accessInfo, proxyConfig.tenantId)
320315
expect(querySpy).toBeCalledTimes(1)
321316
expect(querySpy).toBeCalledWith(
322317
`
323318
UPDATE proxyconfigs
324-
SET access_info=$2, info_format=$3, port=$4, network_dns_suffix=$5
325-
WHERE proxy_config_name=$1 and tenant_id = $6`,
319+
SET info_format=$2, port=$3, network_dns_suffix=$4
320+
WHERE access_info=$1 and tenant_id = $5`,
326321
[
327-
proxyConfig.proxyName,
328322
proxyConfig.accessInfo,
329323
proxyConfig.infoFormat,
330324
proxyConfig.port,

0 commit comments

Comments
 (0)