@opengis/fastify-table 1.0.75 → 1.0.76
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.
- package/Changelog.md +4 -0
- package/crud/controllers/deleteCrud.js +19 -19
- package/crud/controllers/insert.js +54 -54
- package/crud/controllers/update.js +59 -59
- package/crud/funcs/dataInsert.js +24 -24
- package/crud/funcs/getAccess.js +53 -53
- package/crud/funcs/getOpt.js +10 -10
- package/crud/funcs/setOpt.js +16 -16
- package/helper.js +28 -28
- package/notification/controllers/userNotifications.js +19 -19
- package/notification/funcs/addNotification.js +8 -8
- package/package.json +1 -1
- package/pg/pgClients.js +20 -20
- package/policy/funcs/checkPolicy.js +82 -82
- package/policy/funcs/sqlInjection.js +33 -33
- package/policy/index.js +14 -14
- package/redis/client.js +8 -8
- package/redis/funcs/redisClients.js +2 -2
- package/redis/index.js +19 -19
- package/server/migrations/0.sql +64 -64
- package/server/templates/form/test.dataset.form.json +411 -411
- package/server/templates/select/test.storage.data.json +2 -2
- package/server/templates/table/test.dataset.table.json +24 -24
- package/server/templates/table/test.gis.map.table.json +44 -44
- package/table/controllers/data.js +95 -95
- package/table/controllers/utils/getSelect.js +20 -20
- package/table/controllers/utils/gisIRColumn.js +68 -68
- package/table/funcs/getFilterSQL/index.js +75 -75
- package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
- package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
- package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
- package/table/funcs/metaFormat/getSelectVal.js +20 -20
- package/test/api/crud.test.js +88 -88
- package/util/controllers/status.monitor.js +3 -0
- package/util/index.js +21 -20
- package/widget/controllers/utils/historyFormat.js +76 -76
- package/widget/controllers/utils/obj2db.js +13 -13
- package/widget/controllers/widget.del.js +44 -44
- package/widget/controllers/widget.get.js +96 -96
- package/widget/controllers/widget.set.js +70 -70
package/pg/pgClients.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import pg from 'pg';
|
|
2
|
-
import config from '../config.js';
|
|
3
|
-
import init from './funcs/init.js';
|
|
4
|
-
|
|
5
|
-
const pgClients = {};
|
|
6
|
-
if (config.pg) {
|
|
7
|
-
const client = new pg.Pool({
|
|
8
|
-
host: config.pg?.host || '127.0.0.1',
|
|
9
|
-
port: config.pg?.port || 5432,
|
|
10
|
-
database: config.pg?.database || 'postgres',
|
|
11
|
-
user: config.pg?.user || 'postgres',
|
|
12
|
-
password: config.pg?.password || 'postgres',
|
|
13
|
-
});
|
|
14
|
-
client.init = async () => {
|
|
15
|
-
await init(client);
|
|
16
|
-
};
|
|
17
|
-
client.init();
|
|
18
|
-
pgClients.client = client;
|
|
19
|
-
}
|
|
20
|
-
export default pgClients;
|
|
1
|
+
import pg from 'pg';
|
|
2
|
+
import config from '../config.js';
|
|
3
|
+
import init from './funcs/init.js';
|
|
4
|
+
|
|
5
|
+
const pgClients = {};
|
|
6
|
+
if (config.pg) {
|
|
7
|
+
const client = new pg.Pool({
|
|
8
|
+
host: config.pg?.host || '127.0.0.1',
|
|
9
|
+
port: config.pg?.port || 5432,
|
|
10
|
+
database: config.pg?.database || 'postgres',
|
|
11
|
+
user: config.pg?.user || 'postgres',
|
|
12
|
+
password: config.pg?.password || 'postgres',
|
|
13
|
+
});
|
|
14
|
+
client.init = async () => {
|
|
15
|
+
await init(client);
|
|
16
|
+
};
|
|
17
|
+
client.init();
|
|
18
|
+
pgClients.client = client;
|
|
19
|
+
}
|
|
20
|
+
export default pgClients;
|
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import block from './sqlInjection.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Middleware func
|
|
5
|
-
*
|
|
6
|
-
* @type function
|
|
7
|
-
* @alias checkPolicy
|
|
8
|
-
* @summary Функція дозволяє налаштувати доступ до сайту або API для адмін. та публічної частини веб-ресурсу
|
|
9
|
-
* @param {String} path - назва апі
|
|
10
|
-
* @returns {object|null} Returns object
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
export default function checkPolicy(req) {
|
|
14
|
-
const {
|
|
15
|
-
originalUrl: path, hostname, query, params, headers: hs, log, sid = 35, funcs = {},
|
|
16
|
-
} = req;
|
|
17
|
-
const user = req.user || req.session?.passport?.user;
|
|
18
|
-
|
|
19
|
-
const { config } = funcs;
|
|
20
|
-
const isUser = config.debug || !!user;
|
|
21
|
-
|
|
22
|
-
const isServer = process.argv[2];
|
|
23
|
-
const { policy = [] } = req.routeOptions?.config || {};
|
|
24
|
-
|
|
25
|
-
/*= == 0.Check superadmin access === */
|
|
26
|
-
if (policy.includes('superadmin') && user?.user_type !== 'superadmin') {
|
|
27
|
-
log.warn({
|
|
28
|
-
name: 'api/superadmin', params, query, body: JSON.stringify(req?.body || {}).substring(30), message: 'access restricted: 0',
|
|
29
|
-
});
|
|
30
|
-
return { message: 'access restricted: 0', status: 403 };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/*= == 1.File injection === */
|
|
34
|
-
if (JSON.stringify(params || {})?.includes('../') || JSON.stringify(query || {})?.includes('../') || path?.includes('../')) {
|
|
35
|
-
log.warn({
|
|
36
|
-
name: 'injection/file', params, query, message: 'access restricted: 1',
|
|
37
|
-
});
|
|
38
|
-
return { message: 'access restricted: 1', status: 403 };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/*= == 1.1 File === */
|
|
42
|
-
const allowExtPublic = ['.png', '.jpg', '.svg'];
|
|
43
|
-
const ext = path.toLowerCase().substr(-4);
|
|
44
|
-
if (path.includes('files/') && allowExtPublic.includes(ext)) return null;
|
|
45
|
-
|
|
46
|
-
/*= == 2.SQL Injection policy: no-sql === */
|
|
47
|
-
if (!policy.includes('no-sql')) {
|
|
48
|
-
const stopWords = block.filter((el) => path.includes(el));
|
|
49
|
-
if (stopWords?.length) {
|
|
50
|
-
log.warn({ name: 'injection/sql', stopWords, message: 'access restricted: 2' });
|
|
51
|
-
return { message: 'access restricted: 2', status: 403 };
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
/* Check is Not API */
|
|
55
|
-
const isApi = ['/files/', '/api/format/', '/api-user/', '/logger', '/file/'].filter((el) => path.includes(el)).length;
|
|
56
|
-
if (!isApi) return null;
|
|
57
|
-
|
|
58
|
-
/*= == 3. policy: referer === */
|
|
59
|
-
if (!hs?.referer?.includes?.(hostname) && policy.includes('referer') && !config.local && !config.debug) {
|
|
60
|
-
log.warn({ name: 'referer', message: 'access restricted: 3' });
|
|
61
|
-
return { message: 'access restricted: 3', status: 403 };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/*= == policy: public === */
|
|
65
|
-
if (policy.includes('public')) {
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/*= == 4. policy: site auth === */
|
|
70
|
-
if (!policy.includes('site') && sid === 1 && isUser && !config.local && !config.debug) {
|
|
71
|
-
log.warn({ name: 'site', message: 'access restricted: 4' });
|
|
72
|
-
return { message: 'access restricted: 4', status: 403 };
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/*= == 5. base policy: block api === */
|
|
76
|
-
if (sid === 35 && !isUser && isServer && !config.local && !config.debug) {
|
|
77
|
-
log.warn({ name: 'api', message: 'access restricted: 5' });
|
|
78
|
-
return { message: 'access restricted: 5', status: 403 };
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
1
|
+
import block from './sqlInjection.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Middleware func
|
|
5
|
+
*
|
|
6
|
+
* @type function
|
|
7
|
+
* @alias checkPolicy
|
|
8
|
+
* @summary Функція дозволяє налаштувати доступ до сайту або API для адмін. та публічної частини веб-ресурсу
|
|
9
|
+
* @param {String} path - назва апі
|
|
10
|
+
* @returns {object|null} Returns object
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export default function checkPolicy(req) {
|
|
14
|
+
const {
|
|
15
|
+
originalUrl: path, hostname, query, params, headers: hs, log, sid = 35, funcs = {},
|
|
16
|
+
} = req;
|
|
17
|
+
const user = req.user || req.session?.passport?.user;
|
|
18
|
+
|
|
19
|
+
const { config } = funcs;
|
|
20
|
+
const isUser = config.debug || !!user;
|
|
21
|
+
|
|
22
|
+
const isServer = process.argv[2];
|
|
23
|
+
const { policy = [] } = req.routeOptions?.config || {};
|
|
24
|
+
|
|
25
|
+
/*= == 0.Check superadmin access === */
|
|
26
|
+
if (policy.includes('superadmin') && user?.user_type !== 'superadmin') {
|
|
27
|
+
log.warn({
|
|
28
|
+
name: 'api/superadmin', params, query, body: JSON.stringify(req?.body || {}).substring(30), message: 'access restricted: 0',
|
|
29
|
+
});
|
|
30
|
+
return { message: 'access restricted: 0', status: 403 };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/*= == 1.File injection === */
|
|
34
|
+
if (JSON.stringify(params || {})?.includes('../') || JSON.stringify(query || {})?.includes('../') || path?.includes('../')) {
|
|
35
|
+
log.warn({
|
|
36
|
+
name: 'injection/file', params, query, message: 'access restricted: 1',
|
|
37
|
+
});
|
|
38
|
+
return { message: 'access restricted: 1', status: 403 };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/*= == 1.1 File === */
|
|
42
|
+
const allowExtPublic = ['.png', '.jpg', '.svg'];
|
|
43
|
+
const ext = path.toLowerCase().substr(-4);
|
|
44
|
+
if (path.includes('files/') && allowExtPublic.includes(ext)) return null;
|
|
45
|
+
|
|
46
|
+
/*= == 2.SQL Injection policy: no-sql === */
|
|
47
|
+
if (!policy.includes('no-sql')) {
|
|
48
|
+
const stopWords = block.filter((el) => path.includes(el));
|
|
49
|
+
if (stopWords?.length) {
|
|
50
|
+
log.warn({ name: 'injection/sql', stopWords, message: 'access restricted: 2' });
|
|
51
|
+
return { message: 'access restricted: 2', status: 403 };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/* Check is Not API */
|
|
55
|
+
const isApi = ['/files/', '/api/format/', '/api-user/', '/logger', '/file/'].filter((el) => path.includes(el)).length;
|
|
56
|
+
if (!isApi) return null;
|
|
57
|
+
|
|
58
|
+
/*= == 3. policy: referer === */
|
|
59
|
+
if (!hs?.referer?.includes?.(hostname) && policy.includes('referer') && !config.local && !config.debug) {
|
|
60
|
+
log.warn({ name: 'referer', message: 'access restricted: 3' });
|
|
61
|
+
return { message: 'access restricted: 3', status: 403 };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/*= == policy: public === */
|
|
65
|
+
if (policy.includes('public')) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/*= == 4. policy: site auth === */
|
|
70
|
+
if (!policy.includes('site') && sid === 1 && isUser && !config.local && !config.debug) {
|
|
71
|
+
log.warn({ name: 'site', message: 'access restricted: 4' });
|
|
72
|
+
return { message: 'access restricted: 4', status: 403 };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/*= == 5. base policy: block api === */
|
|
76
|
+
if (sid === 35 && !isUser && isServer && !config.local && !config.debug) {
|
|
77
|
+
log.warn({ name: 'api', message: 'access restricted: 5' });
|
|
78
|
+
return { message: 'access restricted: 5', status: 403 };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
const sqlInjection = [
|
|
2
|
-
'()',
|
|
3
|
-
'^',
|
|
4
|
-
'*',
|
|
5
|
-
'like ',
|
|
6
|
-
'@variable',
|
|
7
|
-
'@@variable',
|
|
8
|
-
'group by ',
|
|
9
|
-
'union ',
|
|
10
|
-
'select ',
|
|
11
|
-
'having ',
|
|
12
|
-
'as injectx',
|
|
13
|
-
'where ',
|
|
14
|
-
'rlike ',
|
|
15
|
-
'if(',
|
|
16
|
-
'sleep(',
|
|
17
|
-
'waitfor delay',
|
|
18
|
-
'benchmark(',
|
|
19
|
-
'pg_sleep(',
|
|
20
|
-
"'\\\"",
|
|
21
|
-
'randomblob(',
|
|
22
|
-
'order by ',
|
|
23
|
-
'union all ',
|
|
24
|
-
'+or',
|
|
25
|
-
'or ',
|
|
26
|
-
'and ',
|
|
27
|
-
"'' ",
|
|
28
|
-
'""" ',
|
|
29
|
-
'<script',
|
|
30
|
-
'javascript:',
|
|
31
|
-
]
|
|
32
|
-
|
|
33
|
-
export default sqlInjection;
|
|
1
|
+
const sqlInjection = [
|
|
2
|
+
'()',
|
|
3
|
+
'^',
|
|
4
|
+
'*',
|
|
5
|
+
'like ',
|
|
6
|
+
'@variable',
|
|
7
|
+
'@@variable',
|
|
8
|
+
'group by ',
|
|
9
|
+
'union ',
|
|
10
|
+
'select ',
|
|
11
|
+
'having ',
|
|
12
|
+
'as injectx',
|
|
13
|
+
'where ',
|
|
14
|
+
'rlike ',
|
|
15
|
+
'if(',
|
|
16
|
+
'sleep(',
|
|
17
|
+
'waitfor delay',
|
|
18
|
+
'benchmark(',
|
|
19
|
+
'pg_sleep(',
|
|
20
|
+
"'\\\"",
|
|
21
|
+
'randomblob(',
|
|
22
|
+
'order by ',
|
|
23
|
+
'union all ',
|
|
24
|
+
'+or',
|
|
25
|
+
'or ',
|
|
26
|
+
'and ',
|
|
27
|
+
"'' ",
|
|
28
|
+
'""" ',
|
|
29
|
+
'<script',
|
|
30
|
+
'javascript:',
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
export default sqlInjection;
|
package/policy/index.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
// import fp from 'fastify-plugin';
|
|
2
|
-
|
|
3
|
-
import checkPolicy from './funcs/checkPolicy.js';
|
|
4
|
-
|
|
5
|
-
async function plugin(fastify) {
|
|
6
|
-
fastify.addHook('onRequest', async (request, reply) => {
|
|
7
|
-
const hookData = checkPolicy(request);
|
|
8
|
-
if (hookData?.status && hookData?.message) {
|
|
9
|
-
return reply.status(hookData?.status).send(hookData.message);
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default plugin;
|
|
1
|
+
// import fp from 'fastify-plugin';
|
|
2
|
+
|
|
3
|
+
import checkPolicy from './funcs/checkPolicy.js';
|
|
4
|
+
|
|
5
|
+
async function plugin(fastify) {
|
|
6
|
+
fastify.addHook('onRequest', async (request, reply) => {
|
|
7
|
+
const hookData = checkPolicy(request);
|
|
8
|
+
if (hookData?.status && hookData?.message) {
|
|
9
|
+
return reply.status(hookData?.status).send(hookData.message);
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default plugin;
|
package/redis/client.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import redisClients from './funcs/redisClients.js';
|
|
2
|
-
import getRedis from './funcs/getRedis.js';
|
|
3
|
-
|
|
4
|
-
if (!redisClients[0]) {
|
|
5
|
-
getRedis({ db: 0 });
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export default redisClients[0];
|
|
1
|
+
import redisClients from './funcs/redisClients.js';
|
|
2
|
+
import getRedis from './funcs/getRedis.js';
|
|
3
|
+
|
|
4
|
+
if (!redisClients[0]) {
|
|
5
|
+
getRedis({ db: 0 });
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default redisClients[0];
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const redisClients = {};
|
|
2
|
-
export default redisClients;
|
|
1
|
+
const redisClients = {};
|
|
2
|
+
export default redisClients;
|
package/redis/index.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
// import client from './client.js';
|
|
2
|
-
import getRedis from './funcs/getRedis.js';
|
|
3
|
-
// import client from './funcs/redisClients.js';
|
|
4
|
-
|
|
5
|
-
function close(fastify) {
|
|
6
|
-
fastify.rclient.quit();
|
|
7
|
-
// fastify.rclient2.quit();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
async function plugin(fastify) {
|
|
11
|
-
const client = getRedis({ db: 0, funcs: fastify });
|
|
12
|
-
client.getJSON = client.get;
|
|
13
|
-
fastify.decorate('rclient', client);
|
|
14
|
-
fastify.decorate('getRedis', getRedis);
|
|
15
|
-
// fastify.decorate('rclient2', client2);
|
|
16
|
-
fastify.addHook('onClose', close);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export default plugin;
|
|
1
|
+
// import client from './client.js';
|
|
2
|
+
import getRedis from './funcs/getRedis.js';
|
|
3
|
+
// import client from './funcs/redisClients.js';
|
|
4
|
+
|
|
5
|
+
function close(fastify) {
|
|
6
|
+
fastify.rclient.quit();
|
|
7
|
+
// fastify.rclient2.quit();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async function plugin(fastify) {
|
|
11
|
+
const client = getRedis({ db: 0, funcs: fastify });
|
|
12
|
+
client.getJSON = client.get;
|
|
13
|
+
fastify.decorate('rclient', client);
|
|
14
|
+
fastify.decorate('getRedis', getRedis);
|
|
15
|
+
// fastify.decorate('rclient2', client2);
|
|
16
|
+
fastify.addHook('onClose', close);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default plugin;
|
package/server/migrations/0.sql
CHANGED
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
-- fix error if function exists and return type not text i.e bigint
|
|
2
|
-
|
|
3
|
-
do $$
|
|
4
|
-
|
|
5
|
-
declare
|
|
6
|
-
m record;
|
|
7
|
-
_pk text;
|
|
8
|
-
_tables json;
|
|
9
|
-
_returnType text;
|
|
10
|
-
|
|
11
|
-
begin
|
|
12
|
-
|
|
13
|
-
select format_type(p.prorettype, null) as return_type
|
|
14
|
-
from pg_proc p
|
|
15
|
-
where p.proname = 'next_id'
|
|
16
|
-
and p.pronamespace = 'public'::regnamespace into _returnType;
|
|
17
|
-
|
|
18
|
-
if (_returnType != 'text') then
|
|
19
|
-
raise notice 'default reassign start: % -> text', _returnType;
|
|
20
|
-
|
|
21
|
-
CREATE EXTENSION if not exists "uuid-ossp";
|
|
22
|
-
|
|
23
|
-
SELECT json_object_agg(a.attrelid::regclass, a.attname)
|
|
24
|
-
FROM pg_catalog.pg_attribute a
|
|
25
|
-
LEFT JOIN pg_catalog.pg_attrdef d ON (a.attrelid, a.attnum) = (d.adrelid, d.adnum)
|
|
26
|
-
WHERE NOT a.attisdropped -- no dropped (dead) columns
|
|
27
|
-
AND a.attnum > 0 -- no system columns
|
|
28
|
-
AND pg_get_expr(d.adbin, d.adrelid) = 'next_id()' into _tables;
|
|
29
|
-
|
|
30
|
-
FOR m in (select json_object_keys(_tables) as table) loop
|
|
31
|
-
_pk = _tables->>m.table;
|
|
32
|
-
raise notice 'drop default: %,%', m.table, _pk;
|
|
33
|
-
EXECUTE('alter table '|| m.table || ' alter column ' || _pk || ' set default null;');
|
|
34
|
-
end loop;
|
|
35
|
-
|
|
36
|
-
DROP FUNCTION IF EXISTS next_id();
|
|
37
|
-
|
|
38
|
-
CREATE EXTENSION if not exists "uuid-ossp";
|
|
39
|
-
|
|
40
|
-
CREATE OR REPLACE FUNCTION next_id()
|
|
41
|
-
RETURNS text AS
|
|
42
|
-
$BODY$
|
|
43
|
-
DECLARE
|
|
44
|
-
|
|
45
|
-
BEGIN
|
|
46
|
-
return replace(uuid_generate_v4()::text, '-', '');
|
|
47
|
-
END;
|
|
48
|
-
$BODY$
|
|
49
|
-
LANGUAGE plpgsql VOLATILE
|
|
50
|
-
COST 100;
|
|
51
|
-
|
|
52
|
-
FOR m in (select json_object_keys(_tables) as table) loop
|
|
53
|
-
_pk = _tables->>m.table;
|
|
54
|
-
raise notice 'reassign default: %, %', m.table, _pk;
|
|
55
|
-
EXECUTE('alter table '|| m.table || ' alter column ' || _pk || ' set default next_id();');
|
|
56
|
-
end loop;
|
|
57
|
-
|
|
58
|
-
raise notice 'reassign default finish: %', _tables;
|
|
59
|
-
|
|
60
|
-
else
|
|
61
|
-
raise notice 'skip default reassign';
|
|
62
|
-
end if;
|
|
63
|
-
|
|
64
|
-
end $$
|
|
1
|
+
-- fix error if function exists and return type not text i.e bigint
|
|
2
|
+
|
|
3
|
+
do $$
|
|
4
|
+
|
|
5
|
+
declare
|
|
6
|
+
m record;
|
|
7
|
+
_pk text;
|
|
8
|
+
_tables json;
|
|
9
|
+
_returnType text;
|
|
10
|
+
|
|
11
|
+
begin
|
|
12
|
+
|
|
13
|
+
select format_type(p.prorettype, null) as return_type
|
|
14
|
+
from pg_proc p
|
|
15
|
+
where p.proname = 'next_id'
|
|
16
|
+
and p.pronamespace = 'public'::regnamespace into _returnType;
|
|
17
|
+
|
|
18
|
+
if (_returnType != 'text') then
|
|
19
|
+
raise notice 'default reassign start: % -> text', _returnType;
|
|
20
|
+
|
|
21
|
+
CREATE EXTENSION if not exists "uuid-ossp";
|
|
22
|
+
|
|
23
|
+
SELECT json_object_agg(a.attrelid::regclass, a.attname)
|
|
24
|
+
FROM pg_catalog.pg_attribute a
|
|
25
|
+
LEFT JOIN pg_catalog.pg_attrdef d ON (a.attrelid, a.attnum) = (d.adrelid, d.adnum)
|
|
26
|
+
WHERE NOT a.attisdropped -- no dropped (dead) columns
|
|
27
|
+
AND a.attnum > 0 -- no system columns
|
|
28
|
+
AND pg_get_expr(d.adbin, d.adrelid) = 'next_id()' into _tables;
|
|
29
|
+
|
|
30
|
+
FOR m in (select json_object_keys(_tables) as table) loop
|
|
31
|
+
_pk = _tables->>m.table;
|
|
32
|
+
raise notice 'drop default: %,%', m.table, _pk;
|
|
33
|
+
EXECUTE('alter table '|| m.table || ' alter column ' || _pk || ' set default null;');
|
|
34
|
+
end loop;
|
|
35
|
+
|
|
36
|
+
DROP FUNCTION IF EXISTS next_id();
|
|
37
|
+
|
|
38
|
+
CREATE EXTENSION if not exists "uuid-ossp";
|
|
39
|
+
|
|
40
|
+
CREATE OR REPLACE FUNCTION next_id()
|
|
41
|
+
RETURNS text AS
|
|
42
|
+
$BODY$
|
|
43
|
+
DECLARE
|
|
44
|
+
|
|
45
|
+
BEGIN
|
|
46
|
+
return replace(uuid_generate_v4()::text, '-', '');
|
|
47
|
+
END;
|
|
48
|
+
$BODY$
|
|
49
|
+
LANGUAGE plpgsql VOLATILE
|
|
50
|
+
COST 100;
|
|
51
|
+
|
|
52
|
+
FOR m in (select json_object_keys(_tables) as table) loop
|
|
53
|
+
_pk = _tables->>m.table;
|
|
54
|
+
raise notice 'reassign default: %, %', m.table, _pk;
|
|
55
|
+
EXECUTE('alter table '|| m.table || ' alter column ' || _pk || ' set default next_id();');
|
|
56
|
+
end loop;
|
|
57
|
+
|
|
58
|
+
raise notice 'reassign default finish: %', _tables;
|
|
59
|
+
|
|
60
|
+
else
|
|
61
|
+
raise notice 'skip default reassign';
|
|
62
|
+
end if;
|
|
63
|
+
|
|
64
|
+
end $$
|