askrobots / postblob Public

An experimental web framework where PostgreSQL does everything: routing, HTML, file storage, jobs and sessions.

0 downloads

postblob/fix_hot_deploy.sql
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/postblob.git
HTTPS
https://gitstable.com/git/askrobots/postblob.git
SSH
ssh://git@gitstable.com:2222/askrobots/postblob.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 docs: Add screenshots to README ae44e69 1 year, 1 month ago History
fix_hot_deploy.sql · 3.2 KB · 119 lines
1-- Fix hot deploy functions
2-- psql postblob < fix_hot_deploy.sql
3
4-- Fix execute_code function
5CREATE OR REPLACE FUNCTION execute_code(
6 p_language text,
7 p_code text,
8 p_save_as text DEFAULT NULL
9) RETURNS jsonb AS $$
10DECLARE
11 v_result jsonb;
12 v_do_block text;
13BEGIN
14 -- Execute based on language
15 CASE p_language
16 WHEN 'sql' THEN
17 BEGIN
18 EXECUTE p_code;
19 v_result := jsonb_build_object(
20 'success', true,
21 'message', 'SQL executed successfully'
22 );
23
24 -- Save if requested
25 IF p_save_as IS NOT NULL THEN
26 PERFORM code_crud('PUT', p_save_as, p_code,
27 jsonb_build_object('language', 'sql', 'auto_saved', true));
28 v_result := jsonb_set(v_result, '{saved_as}', to_jsonb(p_save_as));
29 END IF;
30
31 EXCEPTION WHEN OTHERS THEN
32 v_result := jsonb_build_object(
33 'success', false,
34 'error', SQLERRM,
35 'detail', SQLSTATE
36 );
37 END;
38
39 WHEN 'plpgsql' THEN
40 BEGIN
41 -- For PL/pgSQL, wrap in DO block if needed
42 IF NOT (p_code ILIKE 'CREATE%FUNCTION%' OR p_code ILIKE 'DO%') THEN
43 v_do_block := 'DO $BLOCK$ BEGIN ' || p_code || ' END $BLOCK$;';
44 EXECUTE v_do_block;
45 ELSE
46 EXECUTE p_code;
47 END IF;
48
49 v_result := jsonb_build_object(
50 'success', true,
51 'message', 'PL/pgSQL executed successfully'
52 );
53
54 EXCEPTION WHEN OTHERS THEN
55 v_result := jsonb_build_object(
56 'success', false,
57 'error', SQLERRM
58 );
59 END;
60
61 ELSE
62 v_result := jsonb_build_object(
63 'error', 'Unsupported language: ' || p_language
64 );
65 END CASE;
66
67 RETURN v_result;
68END;
69$$ LANGUAGE plpgsql;
70
71-- Fix increment_version function
72CREATE OR REPLACE FUNCTION increment_version()
73RETURNS jsonb AS $$
74DECLARE
75 v_current_version text;
76 v_new_version text;
77 v_new_code text;
78 v_version_num numeric;
79BEGIN
80 -- Get current version
81 v_current_version := get_version();
82
83 -- Parse and increment
84 v_version_num := substring(v_current_version from '\d+\.\d+')::numeric + 0.1;
85 v_new_version := 'Version ' || v_version_num::text;
86
87 -- Create new function code
88 v_new_code := 'CREATE OR REPLACE FUNCTION get_version()
89RETURNS text AS $FUNC$
90BEGIN
91 RETURN ' || quote_literal(v_new_version) || ';
92END;
93$FUNC$ LANGUAGE plpgsql;';
94
95 -- Update in database
96 UPDATE code_files
97 SET content = v_new_code,
98 version = version + 1,
99 updated_at = now()
100 WHERE path = 'functions/self_modify.sql';
101
102 -- Hot reload
103 EXECUTE v_new_code;
104
105 RETURN jsonb_build_object(
106 'old_version', v_current_version,
107 'new_version', v_new_version,
108 'reloaded', true
109 );
110END;
111$$ LANGUAGE plpgsql;
112
113-- Create initial get_version function
114CREATE OR REPLACE FUNCTION get_version()
115RETURNS text AS $$
116BEGIN
117 RETURN 'Version 1.0';
118END;
119$$ LANGUAGE plpgsql;