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
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI
on:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
ruby: ["3.1", "3.2"]
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bundle exec rake test
39 changes: 27 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,45 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activemodel (7.0.4.3)
activesupport (= 7.0.4.3)
activerecord (7.0.4.3)
activemodel (= 7.0.4.3)
activesupport (= 7.0.4.3)
activesupport (7.0.4.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
activemodel (7.2.1)
activesupport (= 7.2.1)
activerecord (7.2.1)
activemodel (= 7.2.1)
activesupport (= 7.2.1)
timeout (>= 0.4.0)
activesupport (7.2.1)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
tzinfo (~> 2.0)
concurrent-ruby (1.3.3)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
base64 (0.2.0)
bigdecimal (3.1.8)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
drb (2.2.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
minitest (5.24.1)
rake (13.0.6)
logger (1.6.1)
minitest (5.25.1)
rake (13.2.1)
securerandom (0.3.1)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)

PLATFORMS
arm64-darwin-22
ruby

DEPENDENCIES
activerecord-clean-db-structure!
rake (~> 13)

BUNDLED WITH
1.17.3
2.5.18
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
require 'rubygems'
require 'bundler/setup'
require 'bundler/gem_tasks'
require "rake/testtask"

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"]
end

task default: %i[test]
6 changes: 3 additions & 3 deletions lib/activerecord-clean-db-structure/clean_dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def run
partitioned_tables += dump.scan(partitioned_tables_regexp2).map(&:first)

partitioned_tables.each do |partitioned_table|
partitioned_schema_name, partitioned_table_name_only = partitioned_table.split('.', 2)
_partitioned_schema_name, partitioned_table_name_only = partitioned_table.split('.', 2)
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 Down Expand Up @@ -124,7 +124,7 @@ def run
dump.gsub!(/^CREATE( UNIQUE)? INDEX \w+ ON .+\n+/, '')
dump.gsub!(/^-- Name: \w+; Type: INDEX\n+/, '')
indexes.each do |table, indexes_for_table|
dump.gsub!(/^(CREATE TABLE #{table}\b(:?[^;\n]*\n)+\);\n)/) { $1 + "\n" + indexes_for_table }
dump.gsub!(/^(CREATE TABLE #{table}\b(:?[^;\n]*\n)+\);*\n(?:.*);*)/) { $1 + "\n\n" + indexes_for_table }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test fails without this fix:

image

end
end

Expand All @@ -151,7 +151,7 @@ def order_column_definitions(source)
inside_table = true
columns = []
result << source_line
elsif source_line.start_with?(");")
elsif source_line.start_with?(")")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test fails without this fix:

image

if inside_table
inside_table = false
columns.sort_by!(&:first)
Expand Down
35 changes: 35 additions & 0 deletions test/clean_dump_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "test_helper"

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

def test_ignore_ids
assert_cleans_dump "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 }
end

def test_order_schema_migrations_values
assert_cleans_dump "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 }
end

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

private

def assert_cleans_dump(expected_output_path, props)
cleaner = ActiveRecordCleanDbStructure::CleanDump.new(File.read(File.join(__dir__, "data/input.sql")), props)
cleaner.run
assert_equal File.read(File.join(__dir__, expected_output_path)), cleaner.dump
end
end
162 changes: 162 additions & 0 deletions test/data/input.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: pg_stat_statements; Type: EXTENSION; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS pg_stat_statements WITH SCHEMA public;


--
-- Name: EXTENSION pg_stat_statements; Type: COMMENT; Schema: -; Owner: -
--

COMMENT ON EXTENSION pg_stat_statements IS 'track execution statistics of all SQL statements executed';


SET default_tablespace = '';

SET default_table_access_method = heap;

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

CREATE TABLE public.ar_internal_metadata (
key character varying NOT NULL,
value character varying,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);


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

CREATE TABLE public.delayed_jobs (
id bigint NOT NULL,
priority integer DEFAULT 0,
attempts integer DEFAULT 0,
handler text,
last_error text,
run_at timestamp without time zone,
locked_at timestamp without time zone,
failed_at timestamp without time zone,
locked_by character varying(255),
queue character varying(255),
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
metadata jsonb DEFAULT '{}'::jsonb,
whodunnit text
)
WITH (fillfactor='85');


--
-- Name: delayed_jobs_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.delayed_jobs_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: delayed_jobs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.delayed_jobs_id_seq OWNED BY public.delayed_jobs.id;


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

CREATE TABLE public.schema_migrations (
version character varying NOT NULL
);


--
-- Name: delayed_jobs id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.delayed_jobs ALTER COLUMN id SET DEFAULT nextval('public.delayed_jobs_id_seq'::regclass);


--
-- Name: ar_internal_metadata ar_internal_metadata_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.ar_internal_metadata
ADD CONSTRAINT ar_internal_metadata_pkey PRIMARY KEY (key);


--
-- Name: delayed_jobs delayed_jobs_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.delayed_jobs
ADD CONSTRAINT delayed_jobs_pkey PRIMARY KEY (id);


--
-- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.schema_migrations
ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);


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

CREATE INDEX index_delayed_jobs_on_locked_by ON public.delayed_jobs USING btree (locked_by);


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

CREATE INDEX index_delayed_jobs_on_queue ON public.delayed_jobs USING btree (queue);


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

CREATE INDEX index_delayed_jobs_on_run_at ON public.delayed_jobs USING btree (run_at) WHERE (locked_at IS NULL);


--
-- PostgreSQL database dump complete
--

SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
('20240822225012'),
('20240822224954'),
('20240725043656'),
('20240621041110'),
('20240621020038'),
('20220802204003'),
('20211125055031'),
('20211012054749'),
('20210923052631'),
('20210903003251');

78 changes: 78 additions & 0 deletions test/expectations/default_props.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET client_min_messages = warning;

SET default_tablespace = '';

-- Name: ar_internal_metadata; Type: TABLE

CREATE TABLE public.ar_internal_metadata (
key character varying NOT NULL,
value character varying,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);

-- Name: delayed_jobs; Type: TABLE

CREATE TABLE public.delayed_jobs (
id BIGSERIAL PRIMARY KEY,
priority integer DEFAULT 0,
attempts integer DEFAULT 0,
handler text,
last_error text,
run_at timestamp without time zone,
locked_at timestamp without time zone,
failed_at timestamp without time zone,
locked_by character varying(255),
queue character varying(255),
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
metadata jsonb DEFAULT '{}'::jsonb,
whodunnit text
)
WITH (fillfactor='85');

-- Name: schema_migrations; Type: TABLE

CREATE TABLE public.schema_migrations (
version character varying NOT NULL
);

ALTER TABLE ONLY public.ar_internal_metadata
ADD CONSTRAINT ar_internal_metadata_pkey PRIMARY KEY (key);

ALTER TABLE ONLY public.schema_migrations
ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);

-- Name: index_delayed_jobs_on_locked_by; Type: INDEX

CREATE INDEX index_delayed_jobs_on_locked_by ON public.delayed_jobs USING btree (locked_by);

-- Name: index_delayed_jobs_on_queue; Type: INDEX

CREATE INDEX index_delayed_jobs_on_queue ON public.delayed_jobs USING btree (queue);

-- Name: index_delayed_jobs_on_run_at; Type: INDEX

CREATE INDEX index_delayed_jobs_on_run_at ON public.delayed_jobs USING btree (run_at) WHERE (locked_at IS NULL);

-- PostgreSQL database dump complete

SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
('20240822225012'),
('20240822224954'),
('20240725043656'),
('20240621041110'),
('20240621020038'),
('20220802204003'),
('20211125055031'),
('20211012054749'),
('20210923052631'),
('20210903003251');
Loading