Skip to content

Commit

Permalink
enable restoring as materialized views
Browse files Browse the repository at this point in the history
  • Loading branch information
FxKu committed Mar 9, 2019
1 parent 046dab3 commit 254ae0d
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/RESTORE.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-- ChangeLog:
--
-- Version | Date | Description | Author
-- 0.6.9 2019-03-09 enable restoring as MATERIALIZED VIEWs FKun
-- 0.6.8 2019-02-25 restore_record with setof return for emtpy result FKun
-- 0.6.7 2018-11-04 have two restore_record_definition functions FKun
-- 0.6.6 2018-11-02 consider schema changes when restoring versions FKun
Expand Down Expand Up @@ -553,7 +554,8 @@ LANGUAGE plpgsql STRICT;
*
* See what the table looked like at a given date.
* The table state will be restored in a separate schema.
* The user can choose if it will appear as a TABLE or VIEW.
* The user can choose if it will appear as a TABLE, VIEW
* or MATERIALIZED VIEW
***********************************************************/
CREATE OR REPLACE FUNCTION pgmemento.restore_table_state(
start_from_tid INTEGER,
Expand Down Expand Up @@ -582,7 +584,7 @@ BEGIN
EXECUTE format('CREATE SCHEMA %I', pgmemento.trim_outer_quotes($5));
END IF;

-- test if table or view already exist in target schema
-- test if table, view or materialized view already exist in target schema
SELECT
c.relkind
INTO
Expand All @@ -597,15 +599,18 @@ BEGIN
AND (
c.relkind = 'r'
OR c.relkind = 'v'
OR c.relkind = 'm'
);

IF existing_table_type IS NOT NULL THEN
IF $7 THEN
-- drop existing table
-- drop or replace existing objects
IF existing_table_type = 'r' THEN
PERFORM pgmemento.drop_table_state($3, $5);
ELSIF existing_table_type = 'm' THEN
EXECUTE format('DROP MATERIALIZED VIEW %I.%I CASCADE', pgmemento.trim_outer_quotes($5), pgmemento.trim_outer_quotes($3));
ELSE
IF $6 = 'TABLE' THEN
IF $6 = 'MATERIALIZED VIEW' OR $6 = 'TABLE' THEN
EXECUTE format('DROP VIEW %I.%I CASCADE', pgmemento.trim_outer_quotes($5), pgmemento.trim_outer_quotes($3));
ELSE
replace_view := ' OR REPLACE ';
Expand All @@ -619,7 +624,7 @@ BEGIN
END IF;

-- let's go back in time - restore a table state for given transaction interval
IF upper($6) = 'VIEW' OR upper($6) = 'TABLE' THEN
IF upper($6) = 'VIEW' OR upper($6) = 'MATERIALIZED VIEW' OR upper($6) = 'TABLE' THEN
restore_query := 'CREATE'
|| replace_view || $6
|| format(E' %I.%I AS\n', pgmemento.trim_outer_quotes($5), pgmemento.trim_outer_quotes($3))
Expand All @@ -628,7 +633,7 @@ BEGIN
-- finally execute query string
EXECUTE restore_query;
ELSE
RAISE NOTICE 'Table type ''%'' not supported. Use ''VIEW'' or ''TABLE''.', $6;
RAISE NOTICE 'Table type ''%'' not supported. Use ''VIEW'', ''MATERIALIZED VIEW'' or ''TABLE''.', $6;
END IF;
END;
$$
Expand Down

0 comments on commit 254ae0d

Please sign in to comment.