Skip to content

Commit

Permalink
Add update test for repair table script
Browse files Browse the repository at this point in the history
This commit creates an update repair test that breaks a few tables for
pre-2.0 versions to ensure that the repair script actually fixes them.
The integrity check for the update tests already contain a check that
dimension slices are valid, so there is no need to add a test for that.

Fixes timescale#2824
  • Loading branch information
mkindahl committed Jan 18, 2021
1 parent 19d3912 commit d3cdaa5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions scripts/test_update_from_tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ UPDATE_VOLUME=$(docker inspect ${CONTAINER_ORIG} --format='{{range .Mounts }}{{.

echo "Executing setup script on container running ${UPDATE_FROM_IMAGE}:${UPDATE_FROM_TAG}"
docker_pgscript ${CONTAINER_ORIG} /src/test/sql/updates/setup.${TEST_VERSION}.sql

# We only break constraints on the version that we will updated and
# not on the clean rerun below since it is not possible to restore
# from a dump when constraints are broken.
if [[ "${TEST_VERSION}" > "v6" ]] || [[ "${TEST_VERSION}" = "v6" ]]; then
echo "Executing repair setup script"
docker_pgscript ${CONTAINER_ORIG} /src/test/sql/updates/setup.repair.sql "single"
fi

docker_pgcmd ${CONTAINER_ORIG} "CHECKPOINT;"

# Remove container but keep volume
Expand Down
66 changes: 66 additions & 0 deletions test/sql/updates/setup.repair.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.

-- Test file to check that the repair script works. It will create a
-- bunch of tables and "break" them by removing dimension slices from
-- the dimension slice table. The repair script should then repair all
-- of them and there should be no dimension slices missing.

SELECT extversion < '2.0.0' AS runs_repair_script
FROM pg_extension
WHERE extname = 'timescaledb' \gset

\if :runs_repair_script
CREATE TABLE repair_test_int(time integer not null, temp float8, tag integer, color integer);
CREATE TABLE repair_test_timestamptz(time timestamptz not null, temp float8, tag integer, color integer);
CREATE TABLE repair_test_timestamp(time timestamp not null, temp float8, tag integer, color integer);
CREATE TABLE repair_test_date(time date not null, temp float8, tag integer, color integer);

SELECT create_hypertable('repair_test_int', 'time', 'tag', 2,
chunk_time_interval => 3);
SELECT create_hypertable('repair_test_timestamptz', 'time', 'tag', 2,
chunk_time_interval => '1 day'::interval);
SELECT create_hypertable('repair_test_timestamp', 'time', 'tag', 2,
chunk_time_interval => '1 day'::interval);
SELECT create_hypertable('repair_test_date', 'time', 'tag', 2,
chunk_time_interval => '1 day'::interval);

-- These rows will create four constraints for each table.
INSERT INTO repair_test_int VALUES
(4, 24.3, 1, 1),
(4, 24.3, 2, 1),
(10, 24.3, 2, 1);

INSERT INTO repair_test_timestamptz VALUES
('2020-01-01 10:11:12', 24.3, 1, 1),
('2020-01-01 10:11:13', 24.3, 2, 1),
('2020-01-02 10:11:14', 24.3, 2, 1);

INSERT INTO repair_test_timestamp VALUES
('2020-01-01 10:11:12', 24.3, 1, 1),
('2020-01-01 10:11:13', 24.3, 2, 1),
('2020-01-02 10:11:14', 24.3, 2, 1);

INSERT INTO repair_test_date VALUES
('2020-01-01 10:11:12', 24.3, 1, 1),
('2020-01-01 10:11:13', 24.3, 2, 1),
('2020-01-02 10:11:14', 24.3, 2, 1);

ALTER TABLE _timescaledb_catalog.chunk_constraint
DROP CONSTRAINT chunk_constraint_dimension_slice_id_fkey;

-- Break the first dimension on each table, which is one of the time
-- constraints for the table. These are different depending on the
-- time type for the table.
--
-- Break the partition constraints on some of the tables. The
-- partition constraints look the same in all tables so we create a
-- mix of tables with just one missing dimension slice and several
-- missing dimension slices.
DELETE FROM _timescaledb_catalog.dimension_slice WHERE
id IN (
150, 154, 158, 162, --Time constraints
155, 163, 164 --Partition constraints
);
\endif

0 comments on commit d3cdaa5

Please sign in to comment.