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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Introduce StatementAppender for better spacing between statements

## [0.2.1] - 2025-02-15

- Add transform to remove SET commands with default values
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ INSERT INTO "schema_migrations" (version) VALUES
into this normalize (and much more compatch & readable) version:

```sql


CREATE EXTENSION IF NOT EXISTS pgcrypto SCHEMA public;


Expand Down
2 changes: 2 additions & 0 deletions lib/activerecord-pg-format-db-structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative "activerecord-pg-format-db-structure/version"

require_relative "activerecord-pg-format-db-structure/deparser"
require_relative "activerecord-pg-format-db-structure/statement_appender"
require_relative "activerecord-pg-format-db-structure/transforms/remove_comments_on_extensions"
require_relative "activerecord-pg-format-db-structure/transforms/inline_serials"
require_relative "activerecord-pg-format-db-structure/transforms/inline_primary_keys"
Expand All @@ -29,6 +30,7 @@ module ActiveRecordPgFormatDbStructure
].freeze

DEFAULT_DEPARSER = Deparser
DEFAULT_STATEMENT_APPENDER = StatementAppender
end

# :nocov:
Expand Down
12 changes: 6 additions & 6 deletions lib/activerecord-pg-format-db-structure/deparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,42 @@ def deparse_raw_statement(raw_statement)
private

def deparse_stmt_generic(stmt)
generic_str = +"\n\n"
generic_str = +""
generic_str << deparse_stmt_and_indent(stmt)
generic_str << ";"
generic_str
end

def deparse_stmt_compact(stmt)
compact_str = +"\n"
compact_str = +""
compact_str << deparse_stmt(stmt)
compact_str << ";"
compact_str
end

def deparse_insert_stmt(stmt)
insert_str = +"\n\n\n"
insert_str = +""
insert_str << deparse_stmt_and_indent(stmt)
insert_str << "\n;"
insert_str
end

def deparse_create_stmt(stmt)
table_str = "\n\n\n-- Name: #{stmt.relation.relname}; Type: TABLE;\n\n"
table_str = "-- Name: #{stmt.relation.relname}; Type: TABLE;\n\n"
table_str << deparse_stmt_and_indent(stmt)
table_str << ";"
table_str
end

def deparse_view_stmt(stmt)
table_str = "\n\n\n-- Name: #{stmt.view.relname}; Type: VIEW;\n\n"
table_str = "-- Name: #{stmt.view.relname}; Type: VIEW;\n\n"
table_str << deparse_stmt_and_indent(stmt)
table_str << ";"
table_str
end

def deparse_create_table_as_stmt(stmt)
table_str = "\n\n\n-- Name: #{stmt.into.rel.relname}; Type: MATERIALIZED VIEW;\n\n"
table_str = "-- Name: #{stmt.into.rel.relname}; Type: MATERIALIZED VIEW;\n\n"
table_str << deparse_stmt_and_indent(stmt)

# couldn't find a better solution for this, but probably an OK workaround?
Expand Down
20 changes: 15 additions & 5 deletions lib/activerecord-pg-format-db-structure/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
module ActiveRecordPgFormatDbStructure
# Formats & normalizes in place the given SQL string
class Formatter
attr_reader :transforms, :deparser
attr_reader :transforms, :deparser, :statement_appender

def initialize(
transforms: DEFAULT_TRANSFORMS,
deparser: DEFAULT_DEPARSER
deparser: DEFAULT_DEPARSER,
statement_appender: DEFAULT_STATEMENT_APPENDER
)
@transforms = transforms
@deparser = deparser
@statement_appender = statement_appender
end

def format(source)
Expand All @@ -23,9 +25,17 @@ def format(source)
transform.new(raw_statements).transform!
end

raw_statements.map do |raw_statement|
deparser.new(source).deparse_raw_statement(raw_statement)
end.compact.join
appender = statement_appender.new
raw_statements.each do |raw_statement|
statement = deparser.new(source).deparse_raw_statement(raw_statement)
appender.append_statement!(
statement,
statement_kind: PgQuery::Node.inner_class_to_name(
raw_statement.stmt.inner.class
)
)
end
appender.output
end
end
end
1 change: 1 addition & 0 deletions lib/activerecord-pg-format-db-structure/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Railtie < Rails::Railtie
config.activerecord_pg_format_db_structure = ActiveSupport::OrderedOptions.new
config.activerecord_pg_format_db_structure.transforms = DEFAULT_TRANSFORMS.dup
config.activerecord_pg_format_db_structure.deparser = DEFAULT_DEPARSER
config.activerecord_pg_format_db_structure.statement_appender = DEFAULT_STATEMENT_APPENDER

rake_tasks do
load "activerecord-pg-format-db-structure/tasks/clean_db_structure.rake"
Expand Down
57 changes: 57 additions & 0 deletions lib/activerecord-pg-format-db-structure/statement_appender.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require "pg_query"
require_relative "indenter"

module ActiveRecordPgFormatDbStructure
# Appends statements with reasonable spacing in-between
class StatementAppender
attr_reader :output

def initialize(output = +"")
@output = output
@previous_statement_kind = nil
end

def append_statement!(statement, statement_kind:)
output.chomp!
output << newlines_separator(
previous_kind: @previous_statement_kind,
current_kind: statement_kind
)
@previous_statement_kind = statement_kind
output << statement
output << "\n"
end

private

def newlines_separator(previous_kind:, current_kind:)
case [
previous_kind,
current_kind
]
in [
nil,
_
]
""
in [
_,
:insert_stmt | :create_stmt | :view_stmt | :create_table_as_stmt
]
"\n\n\n"
in [
:create_stmt | :view_stmt | :create_table_as_stmt | :index_stmt,
:index_stmt
] | [
:variable_set_stmt,
:variable_set_stmt
]
"\n"
else
"\n\n"
end
end
end
end
54 changes: 12 additions & 42 deletions spec/activerecord-pg-format-db-structure/deparser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
SELECT * FROM my_table WHERE 1 = 1;
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)


expect(formatter.format(source)).to eq(<<~SQL)
SELECT *
FROM my_table
WHERE 1 = 1;
Expand All @@ -29,9 +27,7 @@
SELECT '1'::integer;
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)


expect(formatter.format(source)).to eq(<<~SQL)
SELECT '1'::int;
SQL
end
Expand All @@ -48,9 +44,7 @@
WHERE bar > 10 OR bar < 5 OR (bar < 2 AND baz);
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)


expect(formatter.format(source)).to eq(<<~SQL)
SELECT sum(foo) AS column_a,
CASE
WHEN foo = 'a' THEN 1
Expand All @@ -71,10 +65,7 @@
INSERT INTO schema_migrations (version) VALUES ('20250124155339'), ('20250134155339') , ('20250144155339');
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)



expect(formatter.format(source)).to eq(<<~SQL)
INSERT INTO schema_migrations (version) VALUES
('20250124155339')
, ('20250134155339')
Expand All @@ -89,10 +80,7 @@

SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)



expect(formatter.format(source)).to eq(<<~SQL)
INSERT INTO schema_migrations (version)
SELECT foo
FROM bar
Expand All @@ -107,8 +95,7 @@
CREATE UNIQUE INDEX only_one_pending_per_comment_id ON public.my_table USING btree (comment_id) WHERE pending;
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)

expect(formatter.format(source)).to eq(<<~SQL)
CREATE UNIQUE INDEX only_one_pending_per_comment_id ON public.my_table USING btree (comment_id) WHERE pending;
SQL
end
Expand All @@ -122,10 +109,7 @@
);
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)



expect(formatter.format(source)).to eq(<<~SQL)
-- Name: post_stats; Type: VIEW;

CREATE VIEW public.post_stats AS
Expand All @@ -141,10 +125,7 @@
);
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)



expect(formatter.format(source)).to eq(<<~SQL)
-- Name: post_stats; Type: VIEW;

CREATE VIEW public.post_stats AS
Expand All @@ -160,9 +141,7 @@
SELECT * from my_cte;
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)


expect(formatter.format(source)).to eq(<<~SQL)
WITH my_cte AS (
SELECT foo,
baz
Expand All @@ -182,10 +161,7 @@
);
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)



expect(formatter.format(source)).to eq(<<~SQL)
-- Name: post_stats; Type: MATERIALIZED VIEW;

CREATE MATERIALIZED VIEW public.post_stats AS
Expand All @@ -201,10 +177,7 @@
) WITH NO DATA;
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)



expect(formatter.format(source)).to eq(<<~SQL)
-- Name: post_stats; Type: MATERIALIZED VIEW;

CREATE MATERIALIZED VIEW public.post_stats AS
Expand Down Expand Up @@ -250,10 +223,7 @@
) main_status;
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)



expect(formatter.format(source)).to eq(<<~SQL)
-- Name: my_bigg_aggregated_view; Type: VIEW;

CREATE VIEW public.my_bigg_aggregated_view AS
Expand Down
6 changes: 1 addition & 5 deletions spec/activerecord-pg-format-db-structure/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,13 @@
('20250124155339');
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)


expect(formatter.format(source)).to eq(<<~SQL)
SET client_encoding TO "UTF8";

SELECT pg_catalog.set_config('search_path', '', false);

SET check_function_bodies TO TRUE;

SET client_min_messages TO warning;

SET row_security TO OFF;

CREATE EXTENSION IF NOT EXISTS pgcrypto SCHEMA public;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@
('20250124155339');
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)



expect(formatter.format(source)).to eq(<<~SQL)
-- Name: comments; Type: TABLE;

CREATE TABLE public.comments (
Expand Down Expand Up @@ -143,10 +140,7 @@
('20250124155339');
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)



expect(formatter.format(source)).to eq(<<~SQL)
-- Name: comments; Type: TABLE;

CREATE TABLE public.comments (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@
('20250124155339');
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)



expect(formatter.format(source)).to eq(<<~SQL)
-- Name: comments; Type: TABLE;

CREATE TABLE public.comments (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@
('20250124155339');
SQL

expect(formatter.format(source)).to eq(<<~SQL.chomp)



expect(formatter.format(source)).to eq(<<~SQL)
-- Name: comments; Type: TABLE;

CREATE TABLE public.comments (
Expand Down
Loading
Loading