Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IF NOT EXISTS support when creating tables #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ Clickhouse.connection.exists_table("events")
=> true
```

You can even check table existance upon creation.

```ruby
Clickhouse.connection.create_table("events") do |t|
t.if_not_exists
t.fixed_string :id, 16
t.uint16 :year
t.date :date
t.date_time :time
t.string :event
```

Insert data.

```ruby
Expand Down
11 changes: 10 additions & 1 deletion lib/clickhouse/connection/query/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Table

def initialize(name)
@name = name
@if_not_exists = false
@columns = []
yield self
end
Expand All @@ -13,12 +14,20 @@ def engine(value)
@engine = value
end

def if_not_exists(value = true)
@if_not_exists = value
end

def to_sql
raise Clickhouse::InvalidQueryError, "Missing table engine" unless @engine
length = @columns.collect{|x| x[0].to_s.size}.max

sql = []
sql << "CREATE TABLE #{@name} ("
if @if_not_exists
sql << "CREATE TABLE IF NOT EXISTS #{@name} ("
else
sql << "CREATE TABLE #{@name} ("
end

@columns.each_with_index do |(name, type), index|
sql << " #{name.ljust(length, " ")} #{type}#{"," unless index == @columns.size - 1}"
Expand Down
47 changes: 37 additions & 10 deletions test/unit/connection/query/test_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,43 @@ class TestTable < MiniTest::Test
t.engine "MergeTree(date, 8192)"
end

sql = <<-SQL
CREATE TABLE logs_test (
id UInt8,
price Float32,
name String,
date Date,
time DateTime,
hex_id FixedString(8)
)
ENGINE = MergeTree(date, 8192)
sql = <<~SQL
CREATE TABLE logs_test (
id UInt8,
price Float32,
name String,
date Date,
time DateTime,
hex_id FixedString(8)
)
ENGINE = MergeTree(date, 8192)
SQL

assert_equal sql.strip, table.to_sql.strip
end

it "generates a 'CREATE TABLE IF NOT EXISTS' statement" do
table = Clickhouse::Connection::Query::Table.new("logs_test") do |t|
t.if_not_exists
t.uint8 :id
t.float32 :price
t.string :name
t.date :date
t.date_time :time
t.fixed_string :hex_id, 8
t.engine "MergeTree(date, 8192)"
end

sql = <<~SQL
CREATE TABLE IF NOT EXISTS logs_test (
id UInt8,
price Float32,
name String,
date Date,
time DateTime,
hex_id FixedString(8)
)
ENGINE = MergeTree(date, 8192)
SQL

assert_equal sql.strip, table.to_sql.strip
Expand Down