Skip to content

Commit

Permalink
Add support for CREATE SCHEMA
Browse files Browse the repository at this point in the history
Author: Mehmet Emin KARAKAŞ (#136)
  • Loading branch information
lfittl committed Jul 1, 2019
1 parent 1c6f2a0 commit ef5fc6b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def deparse_item(item, context = nil) # rubocop:disable Metrics/CyclomaticComple
deparse_create_function(node)
when CREATE_RANGE_STMT
deparse_create_range(node)
when CREATE_SCHEMA_STMT
deparse_create_schema(node)
when CREATE_STMT
deparse_create_table(node)
when CREATE_TABLE_AS_STMT
Expand Down Expand Up @@ -892,6 +894,15 @@ def deparse_create_range(node)
output.join(' ')
end

def deparse_create_schema(node)
output = ['CREATE SCHEMA']
output << 'IF NOT EXISTS' if node['if_not_exists']
output << deparse_identifier(node['schemaname']) if node.key?('schemaname')
output << format('AUTHORIZATION %s', deparse_item(node['authrole'])) if node.key?('authrole')
output << deparse_item_list(node['schemaElts']) if node.key?('schemaElts')
output.join(' ')
end

def deparse_create_table(node)
output = []
output << 'CREATE'
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,40 @@
end
end

context 'CREATE SCHEMA' do
# Taken from https://www.postgresql.org/docs/11/sql-createschema.html
context 'basic' do
let(:query) { 'CREATE SCHEMA myschema' }

it { is_expected.to eq query }
end

context 'with authorization' do
let(:query) { 'CREATE SCHEMA AUTHORIZATION "joe"' }

it { is_expected.to eq query }
end

context 'with if not exist' do
let(:query) { 'CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION "joe"' }

it { is_expected.to eq query }
end

context 'with cschema_element' do
let(:query) do
"""
CREATE SCHEMA hollywood
CREATE TABLE \"films\" (title text, release date, awards text[])
CREATE VIEW winners AS
SELECT \"title\", \"release\" FROM \"films\" WHERE \"awards\" IS NOT NULL
""".strip
end

it { is_expected.to eq oneline_query }
end
end

context 'CREATE TABLE' do
context 'top-level' do
let(:query) do
Expand Down

0 comments on commit ef5fc6b

Please sign in to comment.