Skip to content

Commit 806292c

Browse files
committed
Fix partition handling for recent Postgres versions
1 parent b5532bb commit 806292c

File tree

5 files changed

+176
-11
lines changed

5 files changed

+176
-11
lines changed

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.2.3

lib/activerecord-clean-db-structure/clean_dump.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def run
8585

8686
partitioned_tables.each do |partitioned_table|
8787
_partitioned_schema_name, partitioned_table_name_only = partitioned_table.split('.', 2)
88+
dump.gsub!(/-- Name: #{partitioned_table_name_only}; Type: TABLE ATTACH/, '')
8889
dump.gsub!(/-- Name: #{partitioned_table_name_only}; Type: TABLE/, '')
8990
dump.gsub!(/CREATE TABLE #{partitioned_table} \([^;]+;/m, '')
9091
dump.gsub!(/ALTER TABLE ONLY ([\w_\.]+) ATTACH PARTITION #{partitioned_table}[^;]+;/m, '')
@@ -100,8 +101,8 @@ def run
100101
dump.gsub!(/ALTER INDEX ([\w_\.]+) ATTACH PARTITION ([\w_]+\.)?#{partitioned_table_index};/, '')
101102
end
102103
dump.gsub!(index_regexp, '')
103-
104-
dump.gsub!(/-- Name: #{partitioned_table}_pkey; Type: INDEX ATTACH\n\n[^;]+?ATTACH PARTITION ([\w_]+\.)?#{partitioned_table}_pkey;/, '')
104+
dump.gsub!(/-- Name: #{partitioned_table_name_only}_pkey; Type: INDEX ATTACH/, '')
105+
dump.gsub!(/ALTER INDEX ([\w_]+\.)?[\w_]+_pkey ATTACH PARTITION #{partitioned_table}_pkey;/, '')
105106
end
106107
# This is mostly done to allow restoring Postgres 11 output on Postgres 10
107108
dump.gsub!(/CREATE INDEX ([\w]+) ON ONLY/, 'CREATE INDEX \\1 ON')

test/clean_dump_test.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,38 @@
22

33
class CleanDumpTest < Minitest::Test
44
def test_basic_case
5-
assert_cleans_dump "expectations/default_props.sql", {}
5+
assert_cleans_dump "data/input.sql", "expectations/default_props.sql"
66
end
77

88
def test_ignore_ids
9-
assert_cleans_dump "expectations/ignore_ids.sql", { ignore_ids: true }
9+
assert_cleans_dump "data/input.sql", "expectations/ignore_ids.sql", ignore_ids: true
1010
end
1111

1212
def test_order_column_definitions
13-
assert_cleans_dump "expectations/order_column_definitions.sql", { order_column_definitions: true }
13+
assert_cleans_dump "data/input.sql", "expectations/order_column_definitions.sql", order_column_definitions: true
1414
end
1515

1616
def test_order_schema_migrations_values
17-
assert_cleans_dump "expectations/order_schema_migrations_values.sql", { order_schema_migrations_values: true }
17+
assert_cleans_dump "data/input.sql", "expectations/order_schema_migrations_values.sql", order_schema_migrations_values: true
1818
end
1919

2020
def test_indexes_after_tables
21-
assert_cleans_dump "expectations/indexes_after_tables.sql", { indexes_after_tables: true }
21+
assert_cleans_dump "data/input.sql", "expectations/indexes_after_tables.sql", indexes_after_tables: true
2222
end
2323

2424
def test_keep_extensions_all
25-
assert_cleans_dump "expectations/keep_extensions_all.sql", { keep_extensions: :all }
25+
assert_cleans_dump "data/input.sql", "expectations/keep_extensions_all.sql", keep_extensions: :all
26+
end
27+
28+
def test_partitions
29+
assert_cleans_dump "data/partitions.sql", "expectations/partitions.sql"
2630
end
2731

2832
private
2933

30-
def assert_cleans_dump(expected_output_path, props)
31-
cleaner = ActiveRecordCleanDbStructure::CleanDump.new(File.read(File.join(__dir__, "data/input.sql")), props)
34+
def assert_cleans_dump(input, output, props = {})
35+
cleaner = ActiveRecordCleanDbStructure::CleanDump.new(File.read(File.join(__dir__, input)), props)
3236
cleaner.run
33-
assert_equal File.read(File.join(__dir__, expected_output_path)), cleaner.dump
37+
assert_equal File.read(File.join(__dir__, output)), cleaner.dump
3438
end
3539
end

test/data/partitions.sql

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
--
2+
-- Name: autovacuum_run_stats_35d; Type: TABLE; Schema: public; Owner: -
3+
--
4+
5+
CREATE TABLE public.autovacuum_run_stats_35d (
6+
autovacuum_run_stats_id uuid DEFAULT public.gen_random_uuid() NOT NULL,
7+
server_id uuid,
8+
schema_table_id bigint,
9+
occurred_at timestamp with time zone NOT NULL
10+
)
11+
PARTITION BY RANGE (occurred_at);
12+
13+
--
14+
-- Name: index_autovacuum_run_stats_35d_on_schema_table_id_occurred_at; Type: INDEX
15+
--
16+
17+
CREATE INDEX index_autovacuum_run_stats_35d_on_schema_table_id_occurred_at ON public.autovacuum_run_stats_35d USING btree (schema_table_id, occurred_at);
18+
19+
--
20+
-- Name: index_autovacuum_run_stats_35d_on_server_id_and_occurred_at; Type: INDEX
21+
--
22+
23+
CREATE INDEX index_autovacuum_run_stats_35d_on_server_id_and_occurred_at ON public.autovacuum_run_stats_35d USING btree (server_id, occurred_at);
24+
25+
--
26+
-- Name: autovacuum_run_stats_35d_20241026; Type: TABLE; Schema: public; Owner: -
27+
--
28+
29+
CREATE TABLE public.autovacuum_run_stats_35d_20241026 (
30+
autovacuum_run_stats_id uuid DEFAULT public.gen_random_uuid() NOT NULL,
31+
server_id uuid,
32+
schema_table_id bigint,
33+
occurred_at timestamp with time zone NOT NULL
34+
);
35+
36+
--
37+
-- Name: autovacuum_run_stats_35d_20241026; Type: TABLE ATTACH; Schema: public; Owner: -
38+
--
39+
40+
ALTER TABLE ONLY public.autovacuum_run_stats_35d ATTACH PARTITION public.autovacuum_run_stats_35d_20241026 FOR VALUES FROM ('2024-10-25 19:00:00-05') TO ('2024-10-26 19:00:00-05');
41+
42+
--
43+
-- Name: autovacuum_run_stats_35d_20241026 autovacuum_run_stats_35d_20241026_pkey; Type: CONSTRAINT; Schema: public; Owner: -
44+
--
45+
46+
ALTER TABLE ONLY public.autovacuum_run_stats_35d_20241026
47+
ADD CONSTRAINT autovacuum_run_stats_35d_20241026_pkey PRIMARY KEY (autovacuum_run_stats_id);
48+
49+
--
50+
-- Name: autovacuum_run_stats_35d_20241026_server_id_occurred_at_idx; Type: INDEX; Schema: public; Owner: -
51+
--
52+
53+
CREATE INDEX autovacuum_run_stats_35d_20241026_server_id_occurred_at_idx ON public.autovacuum_run_stats_35d_20241026 USING btree (server_id, occurred_at);
54+
55+
--
56+
-- Name: autovacuum_run_stats_35d_2024_schema_table_id_occurred_at_idx25; Type: INDEX; Schema: public; Owner: -
57+
--
58+
59+
CREATE INDEX autovacuum_run_stats_35d_2024_schema_table_id_occurred_at_idx25 ON public.autovacuum_run_stats_35d_20241026 USING btree (schema_table_id, occurred_at);
60+
61+
--
62+
-- Name: autovacuum_run_stats_35d_20241026_server_id_occurred_at_idx; Type: INDEX ATTACH; Schema: public; Owner: -
63+
--
64+
65+
ALTER INDEX public.index_autovacuum_run_stats_35d_on_server_id_and_occurred_at ATTACH PARTITION public.autovacuum_run_stats_35d_20241026_server_id_occurred_at_idx;
66+
67+
--
68+
-- Name: schema_table_infos_35d; Type: TABLE; Schema: public; Owner: -
69+
--
70+
71+
CREATE TABLE public.schema_table_infos_35d (
72+
schema_table_id bigint NOT NULL,
73+
collected_at timestamp with time zone NOT NULL,
74+
server_id uuid NOT NULL
75+
)
76+
PARTITION BY RANGE (collected_at);
77+
78+
79+
ALTER TABLE ONLY public.schema_table_infos_35d
80+
ADD CONSTRAINT schema_table_infos_35d_pkey PRIMARY KEY (schema_table_id, collected_at);
81+
82+
--
83+
-- Name: schema_table_infos_35d_20240920; Type: TABLE; Schema: public; Owner: -
84+
--
85+
86+
CREATE TABLE public.schema_table_infos_35d_20240920 (
87+
schema_table_id bigint NOT NULL,
88+
collected_at timestamp with time zone NOT NULL,
89+
server_id uuid NOT NULL
90+
);
91+
92+
--
93+
-- Name: schema_table_infos_35d_20240920; Type: TABLE ATTACH; Schema: public; Owner: -
94+
--
95+
96+
ALTER TABLE ONLY public.schema_table_infos_35d ATTACH PARTITION public.schema_table_infos_35d_20240920 FOR VALUES FROM ('2024-09-19 19:00:00-05') TO ('2024-09-20 19:00:00-05');
97+
98+
--
99+
-- Name: schema_table_infos_35d_20240920 schema_table_infos_35d_20240920_pkey; Type: CONSTRAINT; Schema: public; Owner: -
100+
--
101+
102+
ALTER TABLE ONLY public.schema_table_infos_35d_20240920
103+
ADD CONSTRAINT schema_table_infos_35d_20240920_pkey PRIMARY KEY (schema_table_id, collected_at);
104+
105+
--
106+
-- Name: schema_table_infos_35d_20240920_server_id_idx; Type: INDEX; Schema: public; Owner: -
107+
--
108+
109+
CREATE INDEX schema_table_infos_35d_20240920_server_id_idx ON public.schema_table_infos_35d_20240920 USING btree (server_id);
110+
111+
--
112+
-- Name: schema_table_infos_35d_2024092_schema_table_id_collected_at_idx; Type: INDEX; Schema: public; Owner: -
113+
--
114+
115+
CREATE INDEX schema_table_infos_35d_2024092_schema_table_id_collected_at_idx ON public.schema_table_infos_35d_20240920 USING btree (schema_table_id, collected_at DESC);
116+
117+
--
118+
-- Name: schema_table_infos_35d_20240920_pkey; Type: INDEX ATTACH; Schema: public; Owner: -
119+
--
120+
121+
ALTER INDEX public.schema_table_infos_35d_pkey ATTACH PARTITION public.schema_table_infos_35d_20240920_pkey;
122+
123+
124+
--
125+
-- Name: schema_table_infos_35d_20240920_server_id_idx; Type: INDEX ATTACH; Schema: public; Owner: -
126+
--
127+
128+
ALTER INDEX public.index_schema_table_infos_35d_on_server_id ATTACH PARTITION public.schema_table_infos_35d_20240920_server_id_idx;

test/expectations/partitions.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
-- Name: autovacuum_run_stats_35d; Type: TABLE
3+
4+
CREATE TABLE public.autovacuum_run_stats_35d (
5+
autovacuum_run_stats_id uuid DEFAULT public.gen_random_uuid() NOT NULL,
6+
server_id uuid,
7+
schema_table_id bigint,
8+
occurred_at timestamp with time zone NOT NULL
9+
)
10+
PARTITION BY RANGE (occurred_at);
11+
12+
-- Name: index_autovacuum_run_stats_35d_on_schema_table_id_occurred_at; Type: INDEX
13+
14+
CREATE INDEX index_autovacuum_run_stats_35d_on_schema_table_id_occurred_at ON public.autovacuum_run_stats_35d USING btree (schema_table_id, occurred_at);
15+
16+
-- Name: index_autovacuum_run_stats_35d_on_server_id_and_occurred_at; Type: INDEX
17+
18+
CREATE INDEX index_autovacuum_run_stats_35d_on_server_id_and_occurred_at ON public.autovacuum_run_stats_35d USING btree (server_id, occurred_at);
19+
20+
-- Name: schema_table_infos_35d; Type: TABLE
21+
22+
CREATE TABLE public.schema_table_infos_35d (
23+
schema_table_id bigint NOT NULL,
24+
collected_at timestamp with time zone NOT NULL,
25+
server_id uuid NOT NULL
26+
)
27+
PARTITION BY RANGE (collected_at);
28+
29+
ALTER TABLE ONLY public.schema_table_infos_35d
30+
ADD CONSTRAINT schema_table_infos_35d_pkey PRIMARY KEY (schema_table_id, collected_at);
31+

0 commit comments

Comments
 (0)