Skip to content

Commit

Permalink
Replace clickhouse implementation with a driver
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonJS committed Jan 5, 2024
1 parent 61603a9 commit 3539ad3
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 240 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source 'https://rubygems.org'
ruby '3.2.2'

gem 'bootsnap', require: false
gem 'click_house'
gem 'clickhouse-activerecord'
gem 'importmap-rails'
gem 'jbuilder'
gem 'pg'
Expand Down
13 changes: 4 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
click_house (2.1.2)
activesupport
faraday (>= 1.7, < 3)
clickhouse-activerecord (1.0.0)
activerecord (>= 7.1)
bundler (>= 1.13.4)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
crass (1.0.6)
Expand All @@ -113,11 +113,6 @@ GEM
factory_bot_rails (6.4.2)
factory_bot (~> 6.4)
railties (>= 5.0.0)
faraday (2.8.1)
base64
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
ffi (1.16.3)
globalid (1.2.1)
activesupport (>= 6.1)
Expand Down Expand Up @@ -320,7 +315,7 @@ PLATFORMS
DEPENDENCIES
bootsnap
capybara
click_house
clickhouse-activerecord
debug
factory_bot_rails
importmap-rails
Expand Down
33 changes: 0 additions & 33 deletions app/models/click_house/base.rb

This file was deleted.

3 changes: 2 additions & 1 deletion app/models/click_house/click.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module ClickHouse
class Click < Base
class Click < ClickHouseRecord
self.table_name = 'clicks'
end
end
9 changes: 9 additions & 0 deletions app/models/click_house/click_house_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module ClickHouse
class ClickHouseRecord < ApplicationRecord
self.abstract_class = true

connects_to database: { writing: :clickhouse, reading: :clickhouse }
end
end
3 changes: 2 additions & 1 deletion app/models/click_house/page_view.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module ClickHouse
class PageView < Base
class PageView < ClickHouseRecord
self.table_name = 'page_views'
end
end
3 changes: 2 additions & 1 deletion app/models/click_house/session.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module ClickHouse
class Session < Base
class Session < ClickHouseRecord
self.table_name = 'sessions'
end
end
16 changes: 0 additions & 16 deletions config/click_house.yml

This file was deleted.

105 changes: 30 additions & 75 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,84 +1,39 @@
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#
default: &default
default_primary: &default_primary
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
<<: *default
database: sql_book_development

# The specified database role being used to connect to PostgreSQL.
# To create additional roles in PostgreSQL see `$ createuser --help`.
# When left blank, PostgreSQL will use the default role. This is
# the same name as the operating system user running Rails.
#username: sql_book

# The password associated with the PostgreSQL role (username).
#password:

# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost

# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432

# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
default_clickhouse: &default_clickhouse
adapter: clickhouse
encoding: unicode
username: sql_book
migrations_paths: db/migrate_clickhouse

# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
development:
primary:
<<: *default_primary
database: sql_book_development
clickhouse:
<<: *default_clickhouse
database: sql_book_development
password: password

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: sql_book_test
primary:
<<: *default_primary
database: sql_book_test
clickhouse:
<<: *default_clickhouse
database: sql_book_test
password: password

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: sql_book_production
username: sql_book
password: <%= ENV["SQL_BOOK_DATABASE_PASSWORD"] %>
primary:
<<: *default_primary
database: sql_book_production
username: sql_book
password: <%= ENV["SQL_BOOK_DATABASE_PASSWORD"] %>
clickhouse:
<<: *default_clickhouse
database: sql_book_production
password: <%= ENV["SQL_BOOK_CLICKHOUSE_DATABASE_PASSWORD"] %>
6 changes: 0 additions & 6 deletions config/initializers/click_house.rb

This file was deleted.

30 changes: 30 additions & 0 deletions db/clickhouse_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `rails
# clickhouse:schema:load`. When creating a new database, `rails clickhouse:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ClickhouseActiverecord::Schema.define(version: 2024_01_05_210103) do

# TABLE: clicks
# SQL: CREATE TABLE sql_book_development.clicks ( `uuid` UUID, `data_source_uuid` UUID, `session_uuid` UUID, `visitor_uuid` UUID, `timestamp` Int64, `coordinates_x` UInt32, `coordinates_y` UInt32, `xpath` String, `inner_text` Nullable(String), `attribute_id` Nullable(String), `attribute_class` Nullable(String) ) ENGINE = MergeTree ORDER BY (data_source_uuid, toDate(timestamp)) SETTINGS index_granularity = 8192
# Could not dump table "clicks" because of following StandardError
# Unknown type 'UUID' for column 'uuid'

# TABLE: page_views
# SQL: CREATE TABLE sql_book_development.page_views ( `uuid` UUID, `data_source_uuid` UUID, `session_uuid` UUID, `visitor_uuid` UUID, `timestamp` Int64, `url` String ) ENGINE = MergeTree ORDER BY (data_source_uuid, toDate(timestamp)) SETTINGS index_granularity = 8192
# Could not dump table "page_views" because of following StandardError
# Unknown type 'UUID' for column 'uuid'

# TABLE: sessions
# SQL: CREATE TABLE sql_book_development.sessions ( `uuid` UUID, `data_source_uuid` UUID, `session_uuid` UUID, `visitor_uuid` UUID, `timestamp` Int64, `viewport_x` UInt32, `viewport_y` UInt32, `device_x` UInt32, `device_y` UInt32, `referrer` Nullable(String), `locale` Nullable(String), `useragent` Nullable(String), `browser` Nullable(String), `device_type` Nullable(String), `timezone` Nullable(String), `country_code` Nullable(String), `utm_source` Nullable(String), `utm_medium` Nullable(String), `utm_campaign` Nullable(String), `utm_content` Nullable(String), `utm_term` Nullable(String) ) ENGINE = MergeTree ORDER BY (data_source_uuid, toDate(timestamp)) SETTINGS index_granularity = 8192
# Could not dump table "sessions" because of following StandardError
# Unknown type 'UUID' for column 'uuid'

end
73 changes: 0 additions & 73 deletions db/migrate/20240105192651_create_click_house_models.rb

This file was deleted.

19 changes: 19 additions & 0 deletions db/migrate_clickhouse/20240105202417_create_click.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class CreateClick < ActiveRecord::Migration[7.1]
def change # rubocop:disable Metrics/MethodLength
create_table :clicks, id: false, options: 'MergeTree ORDER BY (data_source_uuid, toDate(timestamp))' do |t|
t.uuid :uuid, null: false
t.uuid :data_source_uuid, null: false
t.uuid :session_uuid, null: false
t.uuid :visitor_uuid, null: false
t.bigint :timestamp, null: false
t.integer :coordinates_x, null: false
t.integer :coordinates_y, null: false
t.string :xpath, null: false
t.string :inner_text, null: true
t.string :attribute_id, null: true
t.string :attribute_class, null: true
end
end
end
14 changes: 14 additions & 0 deletions db/migrate_clickhouse/20240105205912_page_view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class PageView < ActiveRecord::Migration[7.1]
def change
create_table :page_views, id: false, options: 'MergeTree ORDER BY (data_source_uuid, toDate(timestamp))' do |t|
t.uuid :uuid, null: false
t.uuid :data_source_uuid, null: false
t.uuid :session_uuid, null: false
t.uuid :visitor_uuid, null: false
t.bigint :timestamp, null: false
t.string :url, null: false
end
end
end
Loading

0 comments on commit 3539ad3

Please sign in to comment.