ΠΠΎΠ±ΡΠ΄ΠΈΡΠ΅Π»ΡΠ½ΡΠΌ ΠΌΠΎΡΠΈΠ²ΠΎΠΌ ΠΊ Π½Π°ΠΏΠΈΡΠ°Π½ΠΈΡ ΡΡΡΠ΄Π° ΠΏΠΎΡΠ»ΡΠΆΠΈΠ»Π° ΡΡΠ°ΡΡΡ
ΠΠΎΠΊΠ°Π·Π°Π»ΠΎΡΡ ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ½ΡΠΌ ΡΠΎ, ΡΡΠΎ ΠΎΠ΄Π½Π° ΠΈ ΡΠ° ΠΆΠ΅ ΠΌΡΡΠ»Ρ-"ΡΠ΅Π°Π»ΠΈΠ·ΠΎΠ²Π°ΡΡ Π±ΠΈΠ·Π½Π΅Ρ-Π»ΠΎΠ³ΠΈΠΊΡ Π² ΠΠ".
ΠΏΡΠΈΡΠ»Π° Π² Π³ΠΎΠ»ΠΎΠ²Ρ Π½Π΅ ΡΠΎΠ»ΡΠΊΠΎ ΠΌΠ½Π΅ ΠΎΠ΄Π½ΠΎΠΌΡ.
Π’Π°ΠΊΠΆΠ΅ Π½Π° Π±ΡΠ΄ΡΡΠ΅Π΅ Ρ ΠΎΡΠ΅Π»ΠΎΡΡ ΡΠΎΡ ΡΠ°Π½ΠΈΡΡ, Π΄Π»Ρ ΡΠ΅Π±Ρ Π² ΠΏΠ΅ΡΠ²ΡΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ½ΡΠ΅ Π½Π°ΡΠ°Π±ΠΎΡΠΊΠΈ Π²ΠΎΠ·Π½ΠΈΠΊΡΠΈΠ΅ ΠΏΠΎ Ρ ΠΎΠ΄Ρ ΡΠ΅Π°Π»ΠΈΠ·Π°ΡΠΈΠΈ. ΠΡΠΎΠ±Π΅Π½Π½ΠΎ ΡΡΠΈΡΡΠ²Π°Ρ ΡΠΎ, ΡΡΠΎ ΠΎΡΠ½ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΠΎ Π½Π΅Π΄Π°Π²Π½ΠΎ Π±ΡΠ»ΠΎ ΠΏΡΠΈΠ½ΡΡΠΎ ΡΡΡΠ°ΡΠ΅Π³ΠΈΡΠ΅ΡΠΊΠΎΠ΅ ΡΠ΅ΡΠ΅Π½ΠΈΠ΅ ΠΎ ΡΠΌΠ΅Π½Π΅ Π°ΡΡ ΠΈΡΠ΅ΠΊΡΡΡΡ ΠΈ ΠΏΠ΅ΡΠ΅Π½ΠΎΡΠ΅ Π±ΠΈΠ·Π½Π΅Ρ-Π»ΠΎΠ³ΠΈΠΊΠΈ Π½Π° ΡΡΠΎΠ²Π΅Π½Ρ backend. Π’Π°ΠΊ, ΡΡΠΎ Π²ΡΠ΅, ΡΡΠΎ Π±ΡΠ»ΠΎ Π½Π°ΡΠ°Π±ΠΎΡΠ°Π½ΠΎ, ΡΠΊΠΎΡΠΎ Π½ΠΈΠΊΠΎΠΌΡ Π½Π΅ ΠΏΠΎΠ½Π°Π΄ΠΎΠ±ΠΈΡΡΡ ΠΈ Π½ΠΈΠΊΠΎΠΌΡ Π±ΡΠ΄Π΅Ρ Π½Π΅ ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ½ΠΎ.
ΠΠΏΠΈΡΠ°Π½Π½ΡΠ΅ ΠΌΠ΅ΡΠΎΠ΄Ρ Π½Π΅ ΡΠ²Π»ΡΡΡΡΡ ΠΊΠ°ΠΊΠΈΠΌ ΡΠΎ ΠΎΡΠΊΡΡΡΠΈΠ΅ΠΌ ΠΈ ΠΈΡΠΊΠ»ΡΡΠΈΡΠ΅Π»ΡΠ½ΡΠΌ know how, Π²ΡΠ΅ ΠΏΠΎ ΠΊΠ»Π°ΡΡΠΈΠΊΠ΅ ΠΈ Π±ΡΠ»ΠΎ ΡΠ΅Π°Π»ΠΈΠ·ΠΎΠ²Π°Π½ΠΎ Π½Π΅ΠΎΠ΄Π½ΠΎΠΊΡΠ°ΡΠ½ΠΎ (Ρ Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ ΠΏΠΎΠ΄ΠΎΠ±Π½ΡΠΉ ΠΏΠΎΠ΄Ρ ΠΎΠ΄ ΠΏΡΠΈΠΌΠ΅Π½ΠΈΠ» 20 Π»Π΅Ρ Π½Π°Π·Π°Π΄ Π½Π° Oracle).ΠΡΠΎΡΡΠΎ ΡΠ΅ΡΠΈΠ» ΡΠΎΠ±ΡΠ°Π» Π²ΡΠ΅ Π² ΠΎΠ΄Π½ΠΎΠΌ ΠΌΠ΅ΡΡΠ΅. ΠΠ΄ΡΡΠ³ ΠΊΠΎΠΌΡ ΠΏΡΠΈΠ³ΠΎΠ΄ΠΈΡΡΡ. ΠΠ°ΠΊ ΠΏΠΎΠΊΠ°Π·Π°Π»Π° ΠΏΡΠ°ΠΊΡΠΈΠΊΠ° β Π΄ΠΎΠ²ΠΎΠ»ΡΠ½ΠΎ ΡΠ°ΡΡΠΎ ΠΎΠ΄Π½Π° ΠΈ ΡΠ° ΠΆΠ΅ ΠΈΠ΄Π΅Ρ ΠΏΡΠΈΡ ΠΎΠ΄ΠΈΡ Π½Π΅Π·Π°Π²ΠΈΡΠΈΠΌΠΎ ΡΠ°Π·Π½ΡΠΌ Π»ΡΠ΄ΡΠΌ. ΠΠ° ΠΈ Π΄Π»Ρ ΡΠ΅Π±Ρ ΠΎΡΡΠ°Π²ΠΈΡΡ Π½Π° ΠΏΠ°ΠΌΡΡΡ, ΠΏΠΎΠ»Π΅Π·Π½ΠΎ.
ΠΠΎΠ½Π΅ΡΠ½ΠΎ, ΠΆΠ΅ Π½ΠΈΡΡΠΎ Π² ΡΡΠΎΠΌ ΠΌΠΈΡΠ΅ Π½Π΅ ΡΠΎΠ²Π΅ΡΡΠ΅Π½Π½ΠΎ, ΠΎΡΠΈΠ±ΠΊΠΈ ΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΊΠΈ ΠΊ ΡΠΎΠΆΠ°Π»Π΅Π½ΠΈΡ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ. ΠΡΠΈΡΠΈΠΊΠ° ΠΈ Π·Π°ΠΌΠ΅ΡΠ°Π½ΠΈΡ Π²ΡΡΡΠ΅ΡΠΊΠΈ ΠΏΡΠΈΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΠΈ ΠΎΠΆΠΈΠ΄Π°ΡΡΡΡ.Π Π΅ΡΠ΅ ΠΎΠ΄Π½Π° ΠΌΠ°Π»Π΅Π½ΡΠΊΠ°Ρ Π΄Π΅ΡΠ°Π»Ρ β ΠΊΠΎΠ½ΠΊΡΠ΅ΡΠ½ΡΠ΅ Π΄Π΅ΡΠ°Π»ΠΈ ΡΠ΅Π°Π»ΠΈΠ·Π°ΡΠΈΠΈ ΠΎΠΏΡΡΠ΅Π½Ρ. ΠΡΠ΅ ΡΠ°ΠΊΠΈ Π²ΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ ΠΏΠΎΠΊΠ° Π² ΡΠ΅Π°Π»ΡΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°ΡΡΠ΅ΠΌ ΠΏΡΠΎΠ΅ΠΊΡΠ΅. Π’Π°ΠΊ, ΡΡΠΎ ΡΡΠ°ΡΡΡ ΠΊΠ°ΠΊ ΡΡΡΠ΄ ΠΈ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅ΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΏΡΠΈΠΈ, Π½Π΅ Π±ΠΎΠ»Π΅Π΅ ΡΠΎΠ³ΠΎ. ΠΠ°Π΄Π΅ΡΡΡ Π΄Π»Ρ ΠΏΠΎΠ½ΡΡΠΈΡ ΠΎΠ±ΡΠ΅ΠΉ ΠΊΠ°ΡΡΠΈΠ½Ρ, Π΄Π΅ΡΠ°Π»Π΅ΠΉ Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ.
ΠΠ±ΡΠ°Ρ ΠΈΠ΄Π΅Ρ β Β«ΡΠ°Π·Π΄Π΅Π»ΡΠΉ ΠΈ Π²Π»Π°ΡΡΠ²ΡΠΉ, ΡΠΊΡΡΠ²Π°ΠΉ ΠΈ Π²Π»Π°Π΄Π΅ΠΉΒ»
ΠΠ΄Π΅Ρ ΠΊΠ»Π°ΡΡΠΈΡΠ΅ΡΠΊΠ°Ρ β ΠΎΡΠ΄Π΅Π»ΡΠ½Π°Ρ ΡΡ
Π΅ΠΌΠ° Π΄Π»Ρ ΡΠ°Π±Π»ΠΈΡ, ΠΎΡΠ΄Π΅Π»ΡΠ½Π°Ρ ΡΡ
Π΅ΠΌΠ° Π΄Π»Ρ Ρ
ΡΠ°Π½ΠΈΠΌΡΡ
ΡΡΠ½ΠΊΡΠΈΠΉ.
ΠΠ»ΠΈΠ΅Π½Ρ Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ Π΄ΠΎΡΡΡΠΏΠ° ΠΊ Π΄Π°Π½Π½ΡΠΌ Π½Π°ΠΏΡΡΠΌΡΡ. ΠΡΠ΅, ΡΡΠΎ ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ β ΡΠΎΠ»ΡΠΊΠΎ Π²ΡΠ·Π²Π°ΡΡ Ρ
ΡΠ°Π½ΠΈΠΌΡΡ ΡΡΠ½ΠΊΡΠΈΡ ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΠ°ΡΡ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΠΉ ΠΎΡΠ²Π΅Ρ.
Π ΠΎΠ»ΠΈ
CREATE ROLE store;
CREATE ROLE sys_functions;
CREATE ROLE loc_audit_functions;
CREATE ROLE service_functions;
CREATE ROLE business_functions;
Π‘Ρ Π΅ΠΌΡ
Π‘Ρ Π΅ΠΌΠ° Ρ ΡΠ°Π½Π΅Π½ΠΈΡ ΡΠ°Π±Π»ΠΈΡ
Π¦Π΅Π»Π΅Π²ΡΠ΅ ΡΠ°Π±Π»ΠΈΡΡ, ΡΠ΅Π°Π»ΠΈΠ·ΡΡΡΠΈΠ΅ ΠΏΡΠ΅Π΄ΠΌΠ΅ΡΠ½ΡΠ΅ ΡΡΡΠ½ΠΎΡΡΠΈ.
CREATE SCHEMA store AUTHORIZATION store ;
Π‘Ρ Π΅ΠΌΠ° ΡΠΈΡΡΠ΅ΠΌΠ½ΡΡ ΡΡΠ½ΠΊΡΠΈΠΉ
Π‘ΠΈΡΡΠ΅ΠΌΠ½ΡΠ΅ ΡΡΠ½ΠΊΡΠΈΠΈ, Π² ΡΠ°ΡΡΠ½ΠΎΡΡΠΈ Π΄Π»Ρ Π»ΠΎΠ³ΠΈΡΠΎΠ²Π°Π½ΠΈΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ ΡΠ°Π±Π»ΠΈΡ.
CREATE SCHEMA sys_functions AUTHORIZATION sys_functions ;
Π‘Ρ Π΅ΠΌΠ° Π»ΠΎΠΊΠ°Π»ΡΠ½ΠΎΠ³ΠΎ Π°ΡΠ΄ΠΈΡΠ°
Π€ΡΠ½ΠΊΡΠΈΠΈ ΠΈ ΡΠ°Π±Π»ΠΈΡΡ Π΄Π»Ρ ΡΠ΅Π°Π»ΠΈΠ·Π°ΡΠΈΠΈ Π»ΠΎΠΊΠ°Π»ΡΠ½ΠΎΠ³ΠΎ Π°ΡΠ΄ΠΈΡΠ° Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Ρ ΡΠ°Π½ΠΈΠΌΡΡ ΡΡΠ½ΠΊΡΠΈΠΉ ΠΈ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ ΡΠ΅Π»Π΅Π²ΡΡ ΡΠ°Π±Π»ΠΈΡ.
CREATE SCHEMA loc_audit_functions AUTHORIZATION loc_audit_functions;
Π‘Ρ Π΅ΠΌΠ° ΡΠ΅ΡΠ²ΠΈΡΠ½ΡΡ ΡΡΠ½ΠΊΡΠΈΠΉ
Π€ΡΠ½ΠΊΡΠΈΠΈ Π΄Π»Ρ ΡΠ΅ΡΠ²ΠΈΡΠ½ΡΡ ΠΈ DML ΡΡΠ½ΠΊΡΠΈΠΉ.
CREATE SCHEMA service_functions AUTHORIZATION service_functions;
Π‘Ρ Π΅ΠΌΠ° Π±ΠΈΠ·Π½Π΅Ρ ΡΡΠ½ΠΊΡΠΈΠΉ
Π€ΡΠ½ΠΊΡΠΈΠΈ Π΄Π»Ρ ΠΊΠΎΠ½Π΅ΡΠ½ΡΡ Π±ΠΈΠ·Π½Π΅Ρ ΡΡΠ½ΠΊΡΠΈΠΉ Π²ΡΠ·ΡΠ²Π°Π΅ΠΌΡΡ ΠΊΠ»ΠΈΠ΅Π½ΡΠΎΠΌ.
CREATE SCHEMA business_functions AUTHORIZATION business_functions;
ΠΡΠ°Π²Π° Π΄ΠΎΡΡΡΠΏΠ°
Π ΠΎΠ»Ρ β DBA ΠΈΠΌΠ΅Π΅Ρ ΠΏΠΎΠ»Π½ΡΠΉ Π΄ΠΎΡΡΡΠΏ ΠΊΠΎ Π²ΡΠ΅ΠΌ ΡΡ Π΅ΠΌΠ°ΠΌ (ΠΎΡΠ΄Π΅Π»Π΅Π½Π° ΠΎΡ ΡΠΎΠ»ΠΈ DB Owner).
CREATE ROLE dba_role;
GRANT store TO dba_role;
GRANT sys_functions TO dba_role;
GRANT loc_audit_functions TO dba_role;
GRANT service_functions TO dba_role;
GRANT business_functions TO dba_role;
Π ΠΎΠ»Ρ β USER ΠΈΠΌΠ΅Π΅Ρ ΠΏΡΠΈΠ²ΠΈΠ»Π΅Π³ΠΈΡ EXECUTE Π² ΡΡ Π΅ΠΌΠ΅ business_functions.
CREATE ROLE user_role;
ΠΡΠΈΠ²ΠΈΠ»Π΅Π³ΠΈΠΈ ΠΌΠ΅ΠΆΠ΄Ρ ΡΡ Π΅ΠΌΠ°ΠΌΠΈ
GRANT
ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ Π²ΡΠ΅ ΡΡΠ½ΠΊΡΠΈΠΈ ΡΠΎΠ·Π΄Π°ΡΡΡΡ Ρ Π°ΡΡΠΈΠ±ΡΡΠΎΠΌ SECURITY DEFINER Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠ° ΠΈΠ½ΡΡΡΡΠΊΡΠΈΡ REVOKE EXECUTE ON ALL FUNCTIONβ¦ FROM public;
REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA sys_functions FROM public ;
REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA loc_audit_functions FROM public ;
REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA service_functions FROM public ;
REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA business_functions FROM public ;
GRANT USAGE ON SCHEMA sys_functions TO dba_role ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA sys_functions TO dba_role ;
GRANT USAGE ON SCHEMA loc_audit_functions TO dba_role ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA loc_audit_functions TO dba_role ;
GRANT USAGE ON SCHEMA service_functions TO dba_role ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA service_functions TO dba_role ;
GRANT USAGE ON SCHEMA business_functions TO dba_role ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA business_functions TO dba_role ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA business_functions TO user_role ;
GRANT ALL PRIVILEGES ON SCHEMA store TO GROUP business_functions ;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA store TO business_functions ;
GRANT USAGE ON ALL SEQUENCES IN SCHEMA store TO business_functions ;
ΠΡΠ°ΠΊ ΡΡ Π΅ΠΌΠ° ΠΠ β Π³ΠΎΡΠΎΠ²Π°. ΠΠΎΠΆΠ½ΠΎ ΠΏΡΠΈΡΡΡΠΏΠ°ΡΡ ΠΊ Π½Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π΄Π°Π½Π½ΡΠΌΠΈ.
Π¦Π΅Π»Π΅Π²ΡΠ΅ ΡΠ°Π±Π»ΠΈΡΡ
Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΡΠ°Π±Π»ΠΈΡ ΡΡΠΈΠ²ΠΈΠ°Π»ΡΠ½ΠΎ. ΠΠΈΠΊΠ°ΠΊΠΈΡ ΠΎΡΠΎΠ±Π΅Π½Π½ΠΎΡΡΠ΅ΠΉ, Π·Π° ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ΠΌ ΡΠΎΠ³ΠΎ, ΡΡΠΎ Π±ΡΠ»ΠΎ ΡΠ΅ΡΠ΅Π½ΠΎ ΠΎΡΠΊΠ°Π·Π°ΡΡΡΡ ΠΎΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ SERIAL ΠΈ Π³Π΅Π½Π΅ΡΠΈΡΠΎΠ²Π°ΡΡ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎΡΡΠΈ ΡΠ²Π½ΠΎ. ΠΠ»ΡΡ, ΡΠ°Π·ΡΠΌΠ΅Π΅ΡΡΡ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΈΠ½ΡΡΡΡΠΊΡΠΈΠΈ
COMMENT ON ...
ΠΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ Π΄Π»Ρ Π²ΡΠ΅Ρ ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠ², Π±Π΅Π· ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠΉ.
ΠΠΎΠΊΠ°Π»ΡΠ½ΡΠΉ Π°ΡΠ΄ΠΈΡ
ΠΠ»Ρ Π²Π΅Π΄Π΅Π½ΠΈΡ ΠΆΡΡΠ½Π°Π»Π° Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Ρ ΡΠ°Π½ΠΈΠΌΡΡ ΡΡΠ½ΠΊΡΠΈΠΉ ΠΈ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ ΡΠ΅Π»Π΅Π²ΡΡ ΡΠ°Π±Π»ΠΈΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ ΡΠ°Π±Π»ΠΈΡΠ° Π»ΠΎΠΊΠ°Π»ΡΠ½ΠΎΠ³ΠΎ Π°ΡΠ΄ΠΈΡΠ°, Π²ΠΊΠ»ΡΡΠ°ΡΡΠ°Ρ Π² ΡΠ΅Π±Ρ Π² ΡΠΎΠΌ ΡΠΈΡΠ»Π΅ Π΄Π΅ΡΠ°Π»ΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΡΠΊΠΎΠ³ΠΎ ΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΡ, ΠΌΠ΅ΡΠΊΡ Π²ΡΠ·ΡΠ²Π°Π΅ΠΌΠΎΠ³ΠΎ ΠΌΠΎΠ΄ΡΠ»Ρ, ΡΠ°ΠΊΡΠΈΡΠ΅ΡΠΊΠΈΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π²Ρ ΠΎΠ΄Π½ΡΡ ΠΈ Π²ΡΡ ΠΎΠ΄Π½ΡΡ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠ² Π² Π²ΠΈΠ΄Π΅ JSON.
Π‘ΠΈΡΡΠ΅ΠΌΠ½ΡΠ΅ ΡΡΠ½ΠΊΡΠΈΠΈ
ΠΡΠ΅Π΄Π½Π°Π·Π½Π°ΡΠ΅Π½Ρ Π΄Π»Ρ Π»ΠΎΠ³ΠΈΡΠΎΠ²Π°Π½ΠΈΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ Π² ΡΠ΅Π»Π΅Π²ΡΡ ΡΠ°Π±Π»ΠΈΡΠ°Ρ . ΠΡΠ΅Π΄ΡΡΠ°Π²Π»ΡΡΡ ΡΠΎΠ±ΠΎΠΉ ΡΡΠΈΠ³Π³Π΅ΡΠ½ΡΠ΅ ΡΡΠ½ΠΊΡΠΈΠΈ.
Π¨Π°Π±Π»ΠΎΠ½ — ΡΠΈΡΡΠ΅ΠΌΠ½Π°Ρ ΡΡΠ½ΠΊΡΠΈΡ
---------------------------------------------------------
-- INSERT
CREATE OR REPLACE FUNCTION sys_functions.table_insert_log ()
RETURNS TRIGGER AS $$
BEGIN
PERFORM loc_audit_functions.make_log( ' '||'table' , 'insert' , json_build_object('id', NEW.id) );
RETURN NULL ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER table_after_insert AFTER INSERT ON storage.table FOR EACH ROW EXECUTE PROCEDURE sys_functions.table_insert_log();
---------------------------------------------------------
-- UPDATE
CREATE OR REPLACE FUNCTION sys_functions.table_update_log ()
RETURNS TRIGGER AS $$
BEGIN
IF OLD.column != NEW.column
THEN
PERFORM loc_audit_functions.make_log( ' '||'table' , 'update' , json_build_object('OLD.column', OLD.column , 'NEW.column' , NEW.column ) );
END IF ;
RETURN NULL ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER table_after_update AFTER UPDATE ON storage.table FOR EACH ROW EXECUTE PROCEDURE sys_functions.table_update_log ();
---------------------------------------------------------
-- DELETE
CREATE OR REPLACE FUNCTION sys_functions.table_delete_log ()
RETURNS TRIGGER AS $$
BEGIN
PERFORM loc_audit_functions.make_log( ' '||'table' , 'delete' , json_build_object('id', OLD.id ) );
RETURN NULL ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER table_after_delete AFTER DELETE ON storage.table FOR EACH ROW EXECUTE PROCEDURE sys_functions.table_delete_log ();
Π‘Π΅ΡΠ²ΠΈΡΠ½ΡΠ΅ ΡΡΠ½ΠΊΡΠΈΠΈ
ΠΡΠ΅Π΄Π½Π°Π·Π½Π°ΡΠ΅Π½Ρ Π΄Π»Ρ ΡΠ΅Π°Π»ΠΈΠ·Π°ΡΠΈΠΈ ΡΠ΅ΡΠ²ΠΈΡΠ½ΡΡ ΠΈ DML ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ Π½Π°Π΄ ΡΠ΅Π»Π΅Π²ΡΠΌΠΈ ΡΠ°Π±Π»ΠΈΡΠ°ΠΌΠΈ.
Π¨Π°Π±Π»ΠΎΠ½ — ΡΠ΅ΡΠ²ΠΈΡΠ½Π°Ρ ΡΡΠ½ΠΊΡΠΈΡ
--INSERT
--RETURN id OF NEW ROW
CREATE OR REPLACE FUNCTION service_functions.table_insert ( new_column store.table.column%TYPE )
RETURNS integer AS $$
DECLARE
new_id integer ;
BEGIN
-- Generate new id
new_id = nextval('store.table.seq');
-- Insert into table
INSERT INTO store.table
(
id ,
column
)
VALUES
(
new_id ,
new_column
);
RETURN new_id ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
--DELETE
--RETURN ROW NUMBERS DELETED
CREATE OR REPLACE FUNCTION service_functions.table_delete ( current_id integer )
RETURNS integer AS $$
DECLARE
rows_count integer ;
BEGIN
DELETE FROM store.table WHERE id = current_id;
GET DIAGNOSTICS rows_count = ROW_COUNT;
RETURN rows_count ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- UPDATE DETAILS
-- RETURN ROW NUMBERS UPDATED
CREATE OR REPLACE FUNCTION service_functions.table_update_column
(
current_id integer
,new_column store.table.column%TYPE
)
RETURNS integer AS $$
DECLARE
rows_count integer ;
BEGIN
UPDATE store.table
SET
column = new_column
WHERE id = current_id;
GET DIAGNOSTICS rows_count = ROW_COUNT;
RETURN rows_count ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
ΠΠΈΠ·Π½Π΅Ρ ΡΡΠ½ΠΊΡΠΈΠΈ
ΠΡΠ΅Π΄Π½Π°Π·Π½Π°ΡΠ΅Π½Ρ Π΄Π»Ρ ΠΊΠΎΠ½Π΅ΡΠ½ΡΡ Π±ΠΈΠ·Π½Π΅Ρ ΡΡΠ½ΠΊΡΠΈΠΉ Π²ΡΠ·ΡΠ²Π°Π΅ΠΌΡΠΉ ΠΊΠ»ΠΈΠ΅Π½ΡΠΎΠΌ. ΠΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ Π²ΡΠ΅Π³Π΄Π° β JSON. ΠΠ»Ρ ΠΏΠ΅ΡΠ΅Ρ Π²Π°ΡΠ° ΠΈ Π»ΠΎΠ³ΠΈΡΠΎΠ²Π°Π½ΠΈΡ ΠΎΡΠΈΠ±ΠΎΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ, ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Π±Π»ΠΎΠΊ EXCEPTION.
Π¨Π°Π±Π»ΠΎΠ½ — Π±ΠΈΠ·Π½Π΅Ρ ΡΡΠ½ΠΊΡΠΈΡ
CREATE OR REPLACE FUNCTION business_functions.business_function_template(
--Input parameters
)
RETURNS JSON AS $$
DECLARE
------------------------
--for exception catching
error_message text ;
error_json json ;
result json ;
------------------------
BEGIN
--LOGGING
PERFORM loc_audit_functions.make_log
(
'business_function_template',
'STARTED',
json_build_object
(
--IN Parameters
)
);
PERFORM business_functions.notice('business_function_template');
--START BUSINESS PART
--END BUSINESS PART
-- SUCCESFULLY RESULT
PERFORM business_functions.notice('result');
PERFORM business_functions.notice(result);
PERFORM loc_audit_functions.make_log
(
'business_function_template',
'FINISHED',
json_build_object( 'result',result )
);
RETURN result ;
----------------------------------------------------------------------------------------------------------
-- EXCEPTION CATCHING
EXCEPTION
WHEN OTHERS THEN
PERFORM loc_audit_functions.make_log
(
'business_function_template',
'STARTED',
json_build_object
(
--IN Parameters
) , TRUE );
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR',
json_build_object('SQLSTATE',SQLSTATE ), TRUE
);
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR',
json_build_object('SQLERRM',SQLERRM ), TRUE
);
GET STACKED DIAGNOSTICS error_message = RETURNED_SQLSTATE ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-RETURNED_SQLSTATE',json_build_object('RETURNED_SQLSTATE',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = COLUMN_NAME ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-COLUMN_NAME',
json_build_object('COLUMN_NAME',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = CONSTRAINT_NAME ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-CONSTRAINT_NAME',
json_build_object('CONSTRAINT_NAME',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = PG_DATATYPE_NAME ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-PG_DATATYPE_NAME',
json_build_object('PG_DATATYPE_NAME',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = MESSAGE_TEXT ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-MESSAGE_TEXT',json_build_object('MESSAGE_TEXT',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = SCHEMA_NAME ;
PERFORM loc_audit_functions.make_log
(s
'business_function_template',
' ERROR-SCHEMA_NAME',json_build_object('SCHEMA_NAME',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = PG_EXCEPTION_DETAIL ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-PG_EXCEPTION_DETAIL',
json_build_object('PG_EXCEPTION_DETAIL',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = PG_EXCEPTION_HINT ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-PG_EXCEPTION_HINT',json_build_object('PG_EXCEPTION_HINT',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = PG_EXCEPTION_CONTEXT ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-PG_EXCEPTION_CONTEXT',json_build_object('PG_EXCEPTION_CONTEXT',error_message ), TRUE );
RAISE WARNING 'ALARM: %' , SQLERRM ;
SELECT json_build_object
(
'isError' , TRUE ,
'errorMsg' , SQLERRM
) INTO error_json ;
RETURN error_json ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
ΠΡΠΎΠ³
ΠΠ»Ρ ΠΎΠΏΠΈΡΠ°Π½ΠΈΡ ΠΎΠ±ΡΠ΅ΠΉ ΠΊΠ°ΡΡΠΈΠ½Ρ, Π΄ΡΠΌΠ°Ρ Π²ΠΏΠΎΠ»Π½Π΅ Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ. ΠΡΠ»ΠΈ ΠΊΠΎΠ³ΠΎ Π·Π°ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠΎΠ²Π°Π»ΠΈ Π΄Π΅ΡΠ°Π»ΠΈ ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ-ΠΏΠΈΡΠΈΡΠ΅ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ, Ρ ΡΠ΄ΠΎΠ²ΠΎΠ»ΡΡΡΠ²ΠΈΠ΅ΠΌ Π΄ΠΎΠΏΠΎΠ»Π½Ρ ΠΊΠ°ΡΡΠΈΠ½Ρ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡΠ΅Π»ΡΠ½ΡΠΌΠΈ ΡΡΡΠΈΡ Π°ΠΌΠΈ.
P.S.
ΠΠΎΠ³ΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΡΠΎΡΡΠΎΠΉ ΠΎΡΠΈΠ±ΠΊΠΈ — ΡΠΈΠΏ Π²Ρ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°
-[ RECORD 1 ]- date_trunc | 2020-08-19 13:15:46 id | 1072 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | STARTED jsonb_pretty | { | "dko": { | "id": 4, | "type": "Type1", | "title": "CREATED BY addKD", | "Weight": 10, | "Tr": "300", | "reduction": 10, | "isTrud": "TRUE", | "description": "decription", | "lowerTr": "100", | "measurement": "measurement1", | "methodology": "m1", | "passportUrl": "files", | "upperTr": "200", | "weightingFactor": 100.123, | "actualTrValue": null, | "upperTrCalcNumber": "120" | }, | "CardId": 3 | } -[ RECORD 2 ]- date_trunc | 2020-08-19 13:15:46 id | 1073 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR jsonb_pretty | { | "SQLSTATE": "22P02" | } -[ RECORD 3 ]- date_trunc | 2020-08-19 13:15:46 id | 1074 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR jsonb_pretty | { | "SQLERRM": "invalid input syntax for type numeric: "null"" | } -[ RECORD 4 ]- date_trunc | 2020-08-19 13:15:46 id | 1075 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-RETURNED_SQLSTATE jsonb_pretty | { | "RETURNED_SQLSTATE": "22P02" | } -[ RECORD 5 ]- date_trunc | 2020-08-19 13:15:46 id | 1076 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-COLUMN_NAME jsonb_pretty | { | "COLUMN_NAME": "" | } -[ RECORD 6 ]- date_trunc | 2020-08-19 13:15:46 id | 1077 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-CONSTRAINT_NAME jsonb_pretty | { | "CONSTRAINT_NAME": "" | } -[ RECORD 7 ]- date_trunc | 2020-08-19 13:15:46 id | 1078 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-PG_DATATYPE_NAME jsonb_pretty | { | "PG_DATATYPE_NAME": "" | } -[ RECORD 8 ]- date_trunc | 2020-08-19 13:15:46 id | 1079 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-MESSAGE_TEXT jsonb_pretty | { | "MESSAGE_TEXT": "invalid input syntax for type numeric: "null"" | } -[ RECORD 9 ]- date_trunc | 2020-08-19 13:15:46 id | 1080 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-SCHEMA_NAME jsonb_pretty | { | "SCHEMA_NAME": "" | } -[ RECORD 10 ]- date_trunc | 2020-08-19 13:15:46 id | 1081 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-PG_EXCEPTION_DETAIL jsonb_pretty | { | "PG_EXCEPTION_DETAIL": "" | } -[ RECORD 11 ]- date_trunc | 2020-08-19 13:15:46 id | 1082 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-PG_EXCEPTION_HINT jsonb_pretty | { | "PG_EXCEPTION_HINT": "" | } -[ RECORD 12 ]- date_trunc | 2020-08-19 13:15:46 id | 1083 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-PG_EXCEPTION_CONTEXT jsonb_pretty | { usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-MESSAGE_TEXT jsonb_pretty | { | "MESSAGE_TEXT": "invalid input syntax for type numeric: "null"" | }
ΠΡΡΠΎΡΠ½ΠΈΠΊ: habr.com