ddlscript_prepare( integer, integer )

1.40. ddlscript_prepare( integer, integer )

Function Properties

Language: PLPGSQL

Return Type: integer

Prepare for DDL script execution on origin

declare
	p_set_id			alias for $1;
	p_only_on_node		alias for $2;
	v_set_origin		int4;
begin
	-- ----
	-- Grab the central configuration lock
	-- ----
	lock table sl_config_lock;

	
	-- ----
	-- Check that the set exists and originates here
	-- unless only_on_node was specified (then it can be applied to
	-- that node because that is what the user wanted)
	-- ----
	select set_origin into v_set_origin
			from sl_set
			where set_id = p_set_id
			for update;
	if not found then
		raise exception 'Slony-I: set % not found', p_set_id;
	end if;

	if p_only_on_node = -1 then
		if v_set_origin <> getLocalNodeId('_schemadoc') then
			raise exception 'Slony-I: set % does not originate on local node',
				p_set_id;
		end if;
		-- ----
		-- Create a SYNC event, run the script and generate the DDL_SCRIPT event
		-- ----
		perform createEvent('_schemadoc', 'SYNC', NULL);
		perform alterTableRestore(tab_id) from sl_table where tab_set in (select set_id from sl_set where set_origin = getLocalNodeId('_schemadoc'));
	else
		-- ----
		-- If doing "only on one node" - restore ALL tables irrespective of set
		-- ----
		perform alterTableRestore(tab_id) from sl_table;
	end if;
	return 1;
end;