diff --git a/lib/pg_query/deparse.rb b/lib/pg_query/deparse.rb index 6f6e4c74..16cfed73 100644 --- a/lib/pg_query/deparse.rb +++ b/lib/pg_query/deparse.rb @@ -148,6 +148,8 @@ def deparse_item(item, context = nil) # rubocop:disable Metrics/CyclomaticComple deparse_viewstmt(node) when VARIABLE_SET_STMT deparse_variable_set_stmt(node) + when VACUUM_STMT + deparse_vacuum_stmt(node) when STRING if context == A_CONST format("'%s'", node['str'].gsub("'", "''")) @@ -529,6 +531,26 @@ def deparse_variable_set_stmt(node) output.join(' ') end + def deparse_vacuum_stmt(node) + output = [] + output << 'VACUUM' + output.concat(deparse_vacuum_options(node)) + output << deparse_item(node['relation']) if node.key?('relation') + if node.key?('va_cols') + output << "(#{node['va_cols'].map(&method(:deparse_item)).join(', ')})" + end + output.join(' ') + end + + def deparse_vacuum_options(node) + output = [] + output << 'FULL' if node['options'][4] == 1 + output << 'FREEZE' if node['options'][3] == 1 + output << 'VERBOSE' if node['options'][2] == 1 + output << 'ANALYZE' if node['options'][1] == 1 + output + end + def deparse_cte(node) output = [] output << node['ctename'] diff --git a/spec/lib/pg_query/deparse_spec.rb b/spec/lib/pg_query/deparse_spec.rb index de5f3597..2c02f551 100644 --- a/spec/lib/pg_query/deparse_spec.rb +++ b/spec/lib/pg_query/deparse_spec.rb @@ -971,6 +971,56 @@ it { is_expected.to eq "SET search_path TO 10000" } end end + + context 'VACUUM' do + context 'without anything' do + let(:query) { 'VACUUM' } + + it { is_expected.to eq oneline_query } + end + + context 'with table name' do + let(:query) { 'VACUUM "t"' } + + it { is_expected.to eq oneline_query } + end + + context 'full' do + let(:query) { 'VACUUM FULL "t"' } + + it { is_expected.to eq oneline_query } + end + + context 'freeze' do + let(:query) { 'VACUUM FREEZE "t"' } + + it { is_expected.to eq oneline_query } + end + + context 'verbose' do + let(:query) { 'VACUUM VERBOSE "t"' } + + it { is_expected.to eq oneline_query } + end + + context 'analyze' do + let(:query) { 'VACUUM ANALYZE "t"' } + + it { is_expected.to eq oneline_query } + end + + context 'combine operations' do + let(:query) { 'VACUUM FULL FREEZE VERBOSE ANALYZE' } + + it { is_expected.to eq oneline_query } + end + + context 'adding column names' do + let(:query) { 'VACUUM ANALYZE "t" ("a", "b")' } + + it { is_expected.to eq oneline_query } + end + end end describe '#deparse' do