Tšusumetso ea ho ngola sekhechana sena e ne e le sengoloa
Ho ne ho bonahala ho thahasellisa hore mohopolo o tšoanang - "kenya ts'ebetsong logic ea khoebo ho database".
E ne e se ’na feela ea ileng a tla kelellong.
Hape, bakeng sa bokamoso, ke ne ke batla ho boloka, pele ho tsohle, bakeng sa ka, tsoelo-pele e thahasellisang e ileng ea hlaha nakong ea ts'ebetsong. Haholo-holo ha ho nahanoa hore haufinyane tjena ho ile ha etsoa qeto ea leano ea ho fetola meralo le ho fetisetsa logic ea khoebo boemong ba backend. E le hore ntho e 'ngoe le e' ngoe e entsoeng e tla tloha e se na thuso ho mang kapa mang 'me e ke ke ea e-ba le thahasello ho mang kapa mang.
Mekhoa e hlalositsoeng ha se mofuta o itseng oa ho sibolla kapa o ikhethang. tseba kamoo, ntho e 'ngoe le e' ngoe ke ea khale 'me e sebelisitsoe ka makhetlo a' maloa (mohlala, ke sebelisitse mokhoa o tšoanang lilemong tse 20 tse fetileng ho Oracle) Ke sa tsoa etsa qeto ea ho bokella ntho e 'ngoe le e' ngoe sebakeng se le seng. Haeba ho tla ba molemo ho motho e mong. Joalokaha tloaelo e bontšitse, hangata maikutlo a tšoanang a tla ho batho ba fapaneng ka boikemelo. 'Me ho molemo ho ipolokela eona e le khopotso.
Ha e le hantle, ha ho letho lefatšeng lena le phethahetseng, liphoso le typos ka bomalimabe lia khoneha. Ho nyatsuoa le ho fana ka maikutlo hoa amoheloa ka matla 'me ho lebeletsoe.'Me ntlha e 'ngoe hape e nyane - lintlha tse tobileng tsa ts'ebetso li siiloe. Leha ho le joalo, ntho e 'ngoe le e' ngoe e ntse e sebelisoa morerong oa sebele oa ho sebetsa. Kahoo, sengoloa ke sekhechana feela le tlhaloso ea mohopolo o akaretsang, ha ho letho le leng hape. Ke tšepa hore ho na le lintlha tse lekaneng ho utloisisa setšoantšo ka kakaretso.
Mohopolo o akaretsang ke “arola le ho hlola, ho ipata le ho rua”
Mohopolo ke oa khale - schema e arohaneng ea litafole, schema e arohaneng bakeng sa mesebetsi e bolokiloeng.
Moreki ha a na phihlello ea data ka kotloloho. Sohle seo moreki a ka se etsang ke ho letsetsa tšebetso e bolokiloeng ebe o sebetsana le karabo e amohetsoeng.
Mesebetsi
CREATE ROLE store;
CREATE ROLE sys_functions;
CREATE ROLE loc_audit_functions;
CREATE ROLE service_functions;
CREATE ROLE business_functions;
Merero
Sekema sa polokelo ea litafole
Litafole tse reretsoeng ho kenya tšebetsong mekhatlo ea lithuto.
CREATE SCHEMA store AUTHORIZATION store ;
Setšoantšo sa mosebetsi oa tsamaiso
Mesebetsi ea tsamaiso, haholo-holo bakeng sa liphetoho tsa tafole ea ho rema lifate.
CREATE SCHEMA sys_functions AUTHORIZATION sys_functions ;
Sekema sa tlhahlobo ea lehae
Mesebetsi le litafole ho kenya tšebetsong tlhahlobo ea lehae ea ts'ebetso ea mesebetsi e bolokiloeng le liphetoho ho litafole tse reriloeng.
CREATE SCHEMA loc_audit_functions AUTHORIZATION loc_audit_functions;
Setšoantšo sa tšebetso ea tšebeletso
Mesebetsi ea litšebeletso le mesebetsi ea DML.
CREATE SCHEMA service_functions AUTHORIZATION service_functions;
Setšoantšo sa mosebetsi oa khoebo
Mesebetsi ea mesebetsi ea ho qetela ea khoebo e bitsoang moreki.
CREATE SCHEMA business_functions AUTHORIZATION business_functions;
Litokelo tsa ho fihlella
Karolo - dba e na le phihlello e felletseng ea meralo eohle (e arohaneng le karolo ea 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;
Karolo - MOTSOALI e na le tokelo MOEKETSI setšoantšong mesebetsi_ea_khoebo.
CREATE ROLE user_role;
Litokelo pakeng tsa merero
MPHO
Kaha mesebetsi eohle e bōpiloe ka tšobotsi TS'IRELETSO MOTS'ELI ditaelo tse hlokahalang HLAKOLA KETSAHALO KA MOSEBETSI OHLE… HO tsoa sechabeng;
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 ;
Kahoo schema ea database e se e loketse. U ka qala ho tlatsa data.
Litafole tsa sepheo
Ho etsa litafole ha ho letho. Ha ho na likarolo tse khethehileng, ntle le hore ho ile ha etsoa qeto ea ho se sebelise SEBELETSI le ho hlahisa tatelano ka ho hlaka. Hape, ehlile, tšebeliso e phahameng ea litaelo
COMMENT ON ...
Maikutlo bakeng sa всех dintho, ntle le mokgelo.
Tlhahlobo ea lehae
Ho ngolla ts'ebetso ea mesebetsi e bolokiloeng le liphetoho ho litafole tse shebiloeng, ho sebelisoa tafole ea tlhahlobo ea lehae, e kenyelletsang, har'a lintho tse ling, lintlha tsa khokahano ea bareki, leibole la module e bitsoang, le boleng ba 'nete ba kenyelletso, lintlha tse hlahisoang ka mokhoa oa JSON.
Mesebetsi ea tsamaiso
E etselitsoe bakeng sa liphetoho tsa ho rema litafole tsa litafole. Ke mesebetsi e tsosang takatso.
Template - mosebetsi oa tsamaiso
---------------------------------------------------------
-- 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 ();
Mesebetsi ea litšebeletso
E etselitsoe ho kenya tšebetsong lits'ebeletso le ts'ebetso ea DML litafoleng tse reriloeng.
Template - ts'ebetso ea ts'ebeletso
--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;
Mesebetsi ea khoebo
E etselitsoe mesebetsi ea ho qetela ea khoebo e bitsoang moreki. Ba kgutla kamehla - JSON. Ho thibela le ho ngola liphoso tsa ts'ebetso, sebelisa block KEKETSO.
Template - mosebetsi oa khoebo
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;
Phello
Ho hlalosa setšoantšo se akaretsang, ke nahana hore se lekane. Haeba mang kapa mang a thahasella lintlha le liphetho, ngola maikutlo, ke tla thabela ho eketsa lintlha tse eketsehileng setšoantšong.
PES
Ho etsa phoso e bonolo - mofuta oa ho kenya paramethara
-[ 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"" | }
Source: www.habr.com