From 2fe7ee196363d9627219b0709a1554c65d11ca50 Mon Sep 17 00:00:00 2001 From: Alex Ghiculescu Date: Mon, 9 Sep 2024 14:14:06 +1000 Subject: [PATCH 1/3] Handle tables with custom `fillfactor` --- lib/activerecord-clean-db-structure/clean_dump.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/activerecord-clean-db-structure/clean_dump.rb b/lib/activerecord-clean-db-structure/clean_dump.rb index 8abb6c5..20b21bf 100644 --- a/lib/activerecord-clean-db-structure/clean_dump.rb +++ b/lib/activerecord-clean-db-structure/clean_dump.rb @@ -148,7 +148,7 @@ def order_column_definitions(source) inside_table = true columns = [] result << source_line - elsif source_line.start_with?(");") + elsif source_line.start_with?(")") if inside_table inside_table = false columns.sort_by!(&:first) From 52fff69b6e1a3534858bc3e93047fc3692abb6d1 Mon Sep 17 00:00:00 2001 From: Alex Ghiculescu Date: Tue, 10 Sep 2024 09:11:09 +1000 Subject: [PATCH 2/3] Update clean_dump.rb --- lib/activerecord-clean-db-structure/clean_dump.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/activerecord-clean-db-structure/clean_dump.rb b/lib/activerecord-clean-db-structure/clean_dump.rb index 20b21bf..39a2d0f 100644 --- a/lib/activerecord-clean-db-structure/clean_dump.rb +++ b/lib/activerecord-clean-db-structure/clean_dump.rb @@ -121,7 +121,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" + indexes_for_table } end end From 2c31cd245410d5b42f0ca1bb710e9ca7faf467fb Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 12 Sep 2024 15:10:59 +1000 Subject: [PATCH 3/3] add a test harness --- .github/workflows/ci.yml | 16 ++ Gemfile.lock | 39 +++-- Rakefile | 9 + .../clean_dump.rb | 4 +- test/clean_dump_test.rb | 35 ++++ test/data/input.sql | 162 ++++++++++++++++++ test/expectations/default_props.sql | 78 +++++++++ test/expectations/ignore_ids.sql | 104 +++++++++++ test/expectations/indexes_after_tables.sql | 70 ++++++++ test/expectations/keep_extensions_all.sql | 84 +++++++++ .../expectations/order_column_definitions.sql | 78 +++++++++ .../order_schema_migrations_values.sql | 80 +++++++++ test/test_helper.rb | 6 + 13 files changed, 751 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 test/clean_dump_test.rb create mode 100644 test/data/input.sql create mode 100644 test/expectations/default_props.sql create mode 100644 test/expectations/ignore_ids.sql create mode 100644 test/expectations/indexes_after_tables.sql create mode 100644 test/expectations/keep_extensions_all.sql create mode 100644 test/expectations/order_column_definitions.sql create mode 100644 test/expectations/order_schema_migrations_values.sql create mode 100644 test/test_helper.rb diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..6f897f1 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 61847b5..9388331 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,25 +7,40 @@ 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 @@ -33,4 +48,4 @@ DEPENDENCIES rake (~> 13) BUNDLED WITH - 1.17.3 + 2.5.18 diff --git a/Rakefile b/Rakefile index fbfa2fb..07be39b 100644 --- a/Rakefile +++ b/Rakefile @@ -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] diff --git a/lib/activerecord-clean-db-structure/clean_dump.rb b/lib/activerecord-clean-db-structure/clean_dump.rb index 089f338..84208d6 100644 --- a/lib/activerecord-clean-db-structure/clean_dump.rb +++ b/lib/activerecord-clean-db-structure/clean_dump.rb @@ -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, '') @@ -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 } end end diff --git a/test/clean_dump_test.rb b/test/clean_dump_test.rb new file mode 100644 index 0000000..35bb8c1 --- /dev/null +++ b/test/clean_dump_test.rb @@ -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 diff --git a/test/data/input.sql b/test/data/input.sql new file mode 100644 index 0000000..77bd33f --- /dev/null +++ b/test/data/input.sql @@ -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'); + diff --git a/test/expectations/default_props.sql b/test/expectations/default_props.sql new file mode 100644 index 0000000..185e3d5 --- /dev/null +++ b/test/expectations/default_props.sql @@ -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'); diff --git a/test/expectations/ignore_ids.sql b/test/expectations/ignore_ids.sql new file mode 100644 index 0000000..4aa2f89 --- /dev/null +++ b/test/expectations/ignore_ids.sql @@ -0,0 +1,104 @@ +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 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 + +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 + +ALTER SEQUENCE public.delayed_jobs_id_seq OWNED BY public.delayed_jobs.id; + +-- Name: schema_migrations; Type: TABLE + +CREATE TABLE public.schema_migrations ( + version character varying NOT NULL +); + +-- Name: delayed_jobs id; Type: DEFAULT + +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 + +ALTER TABLE ONLY public.ar_internal_metadata + ADD CONSTRAINT ar_internal_metadata_pkey PRIMARY KEY (key); + +-- Name: delayed_jobs delayed_jobs_pkey; Type: CONSTRAINT + +ALTER TABLE ONLY public.delayed_jobs + ADD CONSTRAINT delayed_jobs_pkey PRIMARY KEY (id); + +-- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT + +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'); diff --git a/test/expectations/indexes_after_tables.sql b/test/expectations/indexes_after_tables.sql new file mode 100644 index 0000000..a354946 --- /dev/null +++ b/test/expectations/indexes_after_tables.sql @@ -0,0 +1,70 @@ +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'); + +CREATE INDEX index_delayed_jobs_on_locked_by ON public.delayed_jobs USING btree (locked_by); +CREATE INDEX index_delayed_jobs_on_queue ON public.delayed_jobs USING btree (queue); +CREATE INDEX index_delayed_jobs_on_run_at ON public.delayed_jobs USING btree (run_at) WHERE (locked_at IS NULL); + +-- 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); + +-- 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'); diff --git a/test/expectations/keep_extensions_all.sql b/test/expectations/keep_extensions_all.sql new file mode 100644 index 0000000..296e54d --- /dev/null +++ b/test/expectations/keep_extensions_all.sql @@ -0,0 +1,84 @@ +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; + +-- Name: pg_stat_statements; Type: EXTENSION + +CREATE EXTENSION IF NOT EXISTS pg_stat_statements WITH SCHEMA public; + +-- Name: EXTENSION pg_stat_statements; Type: COMMENT + +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'); diff --git a/test/expectations/order_column_definitions.sql b/test/expectations/order_column_definitions.sql new file mode 100644 index 0000000..5089ca8 --- /dev/null +++ b/test/expectations/order_column_definitions.sql @@ -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 ( + created_at timestamp(6) without time zone NOT NULL, + key character varying NOT NULL, + updated_at timestamp(6) without time zone NOT NULL, + value character varying +); + +-- Name: delayed_jobs; Type: TABLE + +CREATE TABLE public.delayed_jobs ( + attempts integer DEFAULT 0, + created_at timestamp without time zone NOT NULL, + failed_at timestamp without time zone, + handler text, + id BIGSERIAL PRIMARY KEY, + last_error text, + locked_at timestamp without time zone, + locked_by character varying(255), + metadata jsonb DEFAULT '{}'::jsonb, + priority integer DEFAULT 0, + queue character varying(255), + run_at timestamp without time zone, + updated_at timestamp without time zone NOT NULL, + 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'); diff --git a/test/expectations/order_schema_migrations_values.sql b/test/expectations/order_schema_migrations_values.sql new file mode 100644 index 0000000..cf8fd44 --- /dev/null +++ b/test/expectations/order_schema_migrations_values.sql @@ -0,0 +1,80 @@ +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 + ('20210903003251') +,('20210923052631') +,('20211012054749') +,('20211125055031') +,('20220802204003') +,('20240621020038') +,('20240621041110') +,('20240725043656') +,('20240822224954') +,('20240822225012') +; + diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..f9016f7 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,6 @@ +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) +require "activerecord-clean-db-structure" +require 'activerecord-clean-db-structure/clean_dump' + +require "minitest/autorun" +Dir[File.expand_path("support/**/*.rb", __dir__)].sort.each { |rb| require(rb) }