Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2.3
5 changes: 3 additions & 2 deletions lib/activerecord-clean-db-structure/clean_dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def run

partitioned_tables.each do |partitioned_table|
_partitioned_schema_name, partitioned_table_name_only = partitioned_table.split('.', 2)
dump.gsub!(/-- Name: #{partitioned_table_name_only}; Type: TABLE ATTACH/, '')
dump.gsub!(/-- Name: #{partitioned_table_name_only}; Type: TABLE/, '')
dump.gsub!(/CREATE TABLE #{partitioned_table} \([^;]+;/m, '')
dump.gsub!(/ALTER TABLE ONLY ([\w_\.]+) ATTACH PARTITION #{partitioned_table}[^;]+;/m, '')
Expand All @@ -100,8 +101,8 @@ def run
dump.gsub!(/ALTER INDEX ([\w_\.]+) ATTACH PARTITION ([\w_]+\.)?#{partitioned_table_index};/, '')
end
dump.gsub!(index_regexp, '')

dump.gsub!(/-- Name: #{partitioned_table}_pkey; Type: INDEX ATTACH\n\n[^;]+?ATTACH PARTITION ([\w_]+\.)?#{partitioned_table}_pkey;/, '')
dump.gsub!(/-- Name: #{partitioned_table_name_only}_pkey; Type: INDEX ATTACH/, '')
dump.gsub!(/ALTER INDEX ([\w_]+\.)?[\w_]+_pkey ATTACH PARTITION #{partitioned_table}_pkey;/, '')
end
# This is mostly done to allow restoring Postgres 11 output on Postgres 10
dump.gsub!(/CREATE INDEX ([\w]+) ON ONLY/, 'CREATE INDEX \\1 ON')
Expand Down
22 changes: 13 additions & 9 deletions test/clean_dump_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,38 @@

class CleanDumpTest < Minitest::Test
def test_basic_case
assert_cleans_dump "expectations/default_props.sql", {}
assert_cleans_dump "data/input.sql", "expectations/default_props.sql"
end

def test_ignore_ids
assert_cleans_dump "expectations/ignore_ids.sql", { ignore_ids: true }
assert_cleans_dump "data/input.sql", "expectations/ignore_ids.sql", ignore_ids: true
end

def test_order_column_definitions
assert_cleans_dump "expectations/order_column_definitions.sql", { order_column_definitions: true }
assert_cleans_dump "data/input.sql", "expectations/order_column_definitions.sql", order_column_definitions: true
end

def test_order_schema_migrations_values
assert_cleans_dump "expectations/order_schema_migrations_values.sql", { order_schema_migrations_values: true }
assert_cleans_dump "data/input.sql", "expectations/order_schema_migrations_values.sql", order_schema_migrations_values: true
end

def test_indexes_after_tables
assert_cleans_dump "expectations/indexes_after_tables.sql", { indexes_after_tables: true }
assert_cleans_dump "data/input.sql", "expectations/indexes_after_tables.sql", indexes_after_tables: true
end

def test_keep_extensions_all
assert_cleans_dump "expectations/keep_extensions_all.sql", { keep_extensions: :all }
assert_cleans_dump "data/input.sql", "expectations/keep_extensions_all.sql", keep_extensions: :all
end

def test_partitions
assert_cleans_dump "data/partitions.sql", "expectations/partitions.sql"
end

private

def assert_cleans_dump(expected_output_path, props)
cleaner = ActiveRecordCleanDbStructure::CleanDump.new(File.read(File.join(__dir__, "data/input.sql")), props)
def assert_cleans_dump(input, output, props = {})
cleaner = ActiveRecordCleanDbStructure::CleanDump.new(File.read(File.join(__dir__, input)), props)
cleaner.run
assert_equal File.read(File.join(__dir__, expected_output_path)), cleaner.dump
assert_equal File.read(File.join(__dir__, output)), cleaner.dump
end
end
128 changes: 128 additions & 0 deletions test/data/partitions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
--
-- Name: autovacuum_run_stats_35d; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.autovacuum_run_stats_35d (
autovacuum_run_stats_id uuid DEFAULT public.gen_random_uuid() NOT NULL,
server_id uuid,
schema_table_id bigint,
occurred_at timestamp with time zone NOT NULL
)
PARTITION BY RANGE (occurred_at);

--
-- Name: index_autovacuum_run_stats_35d_on_schema_table_id_occurred_at; Type: INDEX
--

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);

--
-- Name: index_autovacuum_run_stats_35d_on_server_id_and_occurred_at; Type: INDEX
--

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);

--
-- Name: autovacuum_run_stats_35d_20241026; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.autovacuum_run_stats_35d_20241026 (
autovacuum_run_stats_id uuid DEFAULT public.gen_random_uuid() NOT NULL,
server_id uuid,
schema_table_id bigint,
occurred_at timestamp with time zone NOT NULL
);

--
-- Name: autovacuum_run_stats_35d_20241026; Type: TABLE ATTACH; Schema: public; Owner: -
--

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');

--
-- Name: autovacuum_run_stats_35d_20241026 autovacuum_run_stats_35d_20241026_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.autovacuum_run_stats_35d_20241026
ADD CONSTRAINT autovacuum_run_stats_35d_20241026_pkey PRIMARY KEY (autovacuum_run_stats_id);

--
-- Name: autovacuum_run_stats_35d_20241026_server_id_occurred_at_idx; Type: INDEX; Schema: public; Owner: -
--

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);

--
-- Name: autovacuum_run_stats_35d_2024_schema_table_id_occurred_at_idx25; Type: INDEX; Schema: public; Owner: -
--

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);

--
-- Name: autovacuum_run_stats_35d_20241026_server_id_occurred_at_idx; Type: INDEX ATTACH; Schema: public; Owner: -
--

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;

--
-- Name: schema_table_infos_35d; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.schema_table_infos_35d (
schema_table_id bigint NOT NULL,
collected_at timestamp with time zone NOT NULL,
server_id uuid NOT NULL
)
PARTITION BY RANGE (collected_at);


ALTER TABLE ONLY public.schema_table_infos_35d
ADD CONSTRAINT schema_table_infos_35d_pkey PRIMARY KEY (schema_table_id, collected_at);

--
-- Name: schema_table_infos_35d_20240920; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.schema_table_infos_35d_20240920 (
schema_table_id bigint NOT NULL,
collected_at timestamp with time zone NOT NULL,
server_id uuid NOT NULL
);

--
-- Name: schema_table_infos_35d_20240920; Type: TABLE ATTACH; Schema: public; Owner: -
--

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');

--
-- Name: schema_table_infos_35d_20240920 schema_table_infos_35d_20240920_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.schema_table_infos_35d_20240920
ADD CONSTRAINT schema_table_infos_35d_20240920_pkey PRIMARY KEY (schema_table_id, collected_at);

--
-- Name: schema_table_infos_35d_20240920_server_id_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX schema_table_infos_35d_20240920_server_id_idx ON public.schema_table_infos_35d_20240920 USING btree (server_id);

--
-- Name: schema_table_infos_35d_2024092_schema_table_id_collected_at_idx; Type: INDEX; Schema: public; Owner: -
--

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);

--
-- Name: schema_table_infos_35d_20240920_pkey; Type: INDEX ATTACH; Schema: public; Owner: -
--

ALTER INDEX public.schema_table_infos_35d_pkey ATTACH PARTITION public.schema_table_infos_35d_20240920_pkey;


--
-- Name: schema_table_infos_35d_20240920_server_id_idx; Type: INDEX ATTACH; Schema: public; Owner: -
--

ALTER INDEX public.index_schema_table_infos_35d_on_server_id ATTACH PARTITION public.schema_table_infos_35d_20240920_server_id_idx;
31 changes: 31 additions & 0 deletions test/expectations/partitions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

-- Name: autovacuum_run_stats_35d; Type: TABLE

CREATE TABLE public.autovacuum_run_stats_35d (
autovacuum_run_stats_id uuid DEFAULT public.gen_random_uuid() NOT NULL,
server_id uuid,
schema_table_id bigint,
occurred_at timestamp with time zone NOT NULL
)
PARTITION BY RANGE (occurred_at);

-- Name: index_autovacuum_run_stats_35d_on_schema_table_id_occurred_at; Type: INDEX

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);

-- Name: index_autovacuum_run_stats_35d_on_server_id_and_occurred_at; Type: INDEX

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);

-- Name: schema_table_infos_35d; Type: TABLE

CREATE TABLE public.schema_table_infos_35d (
schema_table_id bigint NOT NULL,
collected_at timestamp with time zone NOT NULL,
server_id uuid NOT NULL
)
PARTITION BY RANGE (collected_at);

ALTER TABLE ONLY public.schema_table_infos_35d
ADD CONSTRAINT schema_table_infos_35d_pkey PRIMARY KEY (schema_table_id, collected_at);

Loading