Skip to content

Commit

Permalink
fix: Add guards to migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
whiterabbit1983 committed Jan 9, 2025
1 parent 69ce438 commit c923f78
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 31 deletions.
10 changes: 10 additions & 0 deletions memory-store/migrations/000012_transitions.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ DROP INDEX IF EXISTS idx_transitions_next;
DROP INDEX IF EXISTS idx_transitions_current;

-- Drop the transitions table (this will also remove it from hypertables)
DO $$
BEGIN
BEGIN
DELETE FROM transitions;
EXCEPTION
WHEN others THEN
RAISE NOTICE 'An error occurred during deleting all from transitions: %, %', SQLSTATE, SQLERRM;
END;
END $$;

DROP TABLE IF EXISTS transitions;

-- Drop custom types if they exist
Expand Down
2 changes: 1 addition & 1 deletion memory-store/migrations/000012_transitions.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ END;
$$ LANGUAGE plpgsql;

-- Create a trigger on the transitions table
CREATE TRIGGER validate_transition BEFORE INSERT ON transitions FOR EACH ROW
CREATE OR REPLACE TRIGGER validate_transition BEFORE INSERT ON transitions FOR EACH ROW
EXECUTE FUNCTION check_valid_transition ();

COMMIT;
12 changes: 10 additions & 2 deletions memory-store/migrations/000013_executions_continuous_view.down.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
BEGIN;

-- Drop the continuous aggregate policy
SELECT
remove_continuous_aggregate_policy ('latest_transitions');
DO $$
BEGIN
BEGIN
SELECT
remove_continuous_aggregate_policy ('latest_transitions');
EXCEPTION
WHEN others THEN
RAISE NOTICE 'An error occurred during remove_continuous_aggregate_policy(latest_transitions): %, %', SQLSTATE, SQLERRM;
END;
END $$;

-- Drop the views
DROP VIEW IF EXISTS latest_executions;
Expand Down
52 changes: 42 additions & 10 deletions memory-store/migrations/000017_compression.down.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
BEGIN;

SELECT
remove_compression_policy ('entries');
DO $$
BEGIN
BEGIN
SELECT
remove_compression_policy ('entries');
EXCEPTION
WHEN others THEN
RAISE NOTICE 'An error occurred during remove_compression_policy(entries): %, %', SQLSTATE, SQLERRM;
END;
END $$;

SELECT
remove_compression_policy ('transitions');
DO $$
BEGIN
BEGIN
SELECT
remove_compression_policy ('transitions');
EXCEPTION
WHEN others THEN
RAISE NOTICE 'An error occurred during remove_compression_policy(transitions): %, %', SQLSTATE, SQLERRM;
END;
END $$;

ALTER TABLE entries
SET
(timescaledb.compress = FALSE);
DO $$
BEGIN
BEGIN
ALTER TABLE entries
SET
(timescaledb.compress = FALSE);
EXCEPTION
WHEN others THEN
RAISE NOTICE 'An error occurred during unsetting entries.compress: %, %', SQLSTATE, SQLERRM;
END;
END $$;

ALTER TABLE transitions
SET
(timescaledb.compress = FALSE);
DO $$
BEGIN
BEGIN
ALTER TABLE transitions
SET
(timescaledb.compress = FALSE);
EXCEPTION
WHEN others THEN
RAISE NOTICE 'An error occurred during unsetting transitions.compress: %, %', SQLSTATE, SQLERRM;
END;
END $$;

COMMIT;
67 changes: 49 additions & 18 deletions memory-store/migrations/000017_compression.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,57 @@

BEGIN;

ALTER TABLE entries
SET
(
timescaledb.compress = TRUE,
timescaledb.compress_segmentby = 'session_id',
timescaledb.compress_orderby = 'created_at DESC, entry_id DESC'
);
DO $$
BEGIN
BEGIN
ALTER TABLE entries
SET (
timescaledb.compress = TRUE,
timescaledb.compress_segmentby = 'session_id',
timescaledb.compress_orderby = 'created_at DESC, entry_id DESC'
);
EXCEPTION
WHEN others THEN
RAISE NOTICE 'An error occurred during entries.compress: %, %', SQLSTATE, SQLERRM;
END;
END $$;

SELECT
add_compression_policy ('entries', INTERVAL '7 days');
DO $$
BEGIN
BEGIN
SELECT
add_compression_policy ('entries', INTERVAL '7 days');
EXCEPTION
WHEN others THEN
RAISE NOTICE 'An error occurred during add_compression_policy(entries): %, %', SQLSTATE, SQLERRM;
END;
END $$;

ALTER TABLE transitions
SET
(
timescaledb.compress = TRUE,
timescaledb.compress_segmentby = 'execution_id',
timescaledb.compress_orderby = 'created_at DESC, transition_id DESC'
);
DO $$
BEGIN
BEGIN
ALTER TABLE transitions
SET
(
timescaledb.compress = TRUE,
timescaledb.compress_segmentby = 'execution_id',
timescaledb.compress_orderby = 'created_at DESC, transition_id DESC'
);
EXCEPTION
WHEN others THEN
RAISE NOTICE 'An error occurred during transitions.compress: %, %', SQLSTATE, SQLERRM;
END;
END $$;

SELECT
add_compression_policy ('transitions', INTERVAL '7 days');
DO $$
BEGIN
BEGIN
SELECT
add_compression_policy ('transitions', INTERVAL '7 days');
EXCEPTION
WHEN others THEN
RAISE NOTICE 'An error occurred during add_compression_policy(transitions): %, %', SQLSTATE, SQLERRM;
END;
END $$;

COMMIT;

0 comments on commit c923f78

Please sign in to comment.