Skip to content

Commit

Permalink
create table similar to other table
Browse files Browse the repository at this point in the history
  • Loading branch information
PNixx committed May 3, 2024
1 parent 72829db commit 34684cf
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ false`. The default integer is `UInt32`

Example:

``` ruby
```ruby
class CreateDataItems < ActiveRecord::Migration[7.1]
def change
create_table "data_items", id: false, options: "VersionedCollapsingMergeTree(sign, version) PARTITION BY toYYYYMM(day) ORDER BY category", force: :cascade do |t|
Expand Down Expand Up @@ -230,8 +230,8 @@ end

Create table with custom column structure:

``` ruby
class CreateDataItems < ActiveRecord::Migration
```ruby
class CreateDataItems < ActiveRecord::Migration[7.1]
def change
create_table "data_items", id: false, options: "MergeTree PARTITION BY toYYYYMM(timestamp) ORDER BY timestamp", force: :cascade do |t|
t.column "timestamp", "DateTime('UTC') CODEC(DoubleDelta, LZ4)"
Expand All @@ -240,6 +240,16 @@ class CreateDataItems < ActiveRecord::Migration
end
```

Create Buffer table with connection database name:

```ruby
class CreateDataItems < ActiveRecord::Migration[7.1]
def change
create_table :some_buffers, as: :some, options: "Buffer(#{connection.database}, some, 1, 10, 60, 100, 10000, 10000000, 100000000)"
end
end
```


### Using replica and cluster params in connection parameters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def visit_TableDefinition(o)
create_sql = +"CREATE#{table_modifier_in_create(o)} #{o.view ? "VIEW" : "TABLE"} "
create_sql << "IF NOT EXISTS " if o.if_not_exists
create_sql << "#{quote_table_name(o.name)} "
add_as_clause!(create_sql, o) if o.as && !o.view
add_to_clause!(create_sql, o) if o.materialized

statements = o.columns.map { |c| accept c }
Expand All @@ -103,7 +104,7 @@ def visit_TableDefinition(o)
create_sql << "(#{statements.join(', ')})" if statements.present?
# Attach options for only table or materialized view without TO section
add_table_options!(create_sql, o) if !o.view || o.view && o.materialized && !o.to
add_as_clause!(create_sql, o)
add_as_clause!(create_sql, o) if o.as && o.view
create_sql
end

Expand Down
6 changes: 5 additions & 1 deletion lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def create_table(table_name, **options, &block)
options = apply_replica(table_name, options)
td = create_table_definition(apply_cluster(table_name), **options)
block.call td if block_given?
td.column(:id, options[:id], null: false) if options[:id].present? && td[:id].blank?
td.column(:id, options[:id], null: false) if options[:id].present? && td[:id].blank? && options[:as].blank?

if options[:force]
drop_table(table_name, options.merge(if_exists: true))
Expand Down Expand Up @@ -431,6 +431,10 @@ def replica
@config[:replica_name]
end

def database
@config[:database]
end

def use_default_replicated_merge_tree_params?
database_engine_atomic? && @config[:use_default_replicated_merge_tree_params]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/clickhouse-activerecord/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ClickhouseActiverecord
VERSION = '1.0.7'
VERSION = '1.0.8'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class CreateSomeTable < ActiveRecord::Migration[5.0]
def up
create_table :some do

end
create_table :some_buffers, as: :some, options: "Buffer(#{connection.database}, some, 1, 10, 60, 100, 10000, 10000000, 100000000)"
end
end

9 changes: 9 additions & 0 deletions spec/single/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
end
end

context 'with buffer table' do
let(:directory) { 'dsl_table_buffer_creation' }
it 'creates a table' do
subject

expect(ActiveRecord::Base.connection.tables).to include('some_buffers')
end
end

context 'with engine' do
let(:directory) { 'dsl_table_with_engine_creation' }
it 'creates a table' do
Expand Down

0 comments on commit 34684cf

Please sign in to comment.