{"id":15513130,"url":"https://github.com/aldesantis/clickhouse-activerecord","last_synced_at":"2025-03-05T11:31:40.971Z","repository":{"id":207938371,"uuid":"720457063","full_name":"aldesantis/clickhouse-activerecord","owner":"aldesantis","description":null,"archived":false,"fork":false,"pushed_at":"2023-11-18T15:08:51.000Z","size":159,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"rails_7","last_synced_at":"2024-10-09T11:01:11.209Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aldesantis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-11-18T14:48:00.000Z","updated_at":"2023-11-18T15:01:45.000Z","dependencies_parsed_at":"2023-11-18T16:26:09.763Z","dependency_job_id":"fd3edc65-0b0d-4661-87ac-123c499b529c","html_url":"https://github.com/aldesantis/clickhouse-activerecord","commit_stats":null,"previous_names":["aldesantis/clickhouse-activerecord"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldesantis%2Fclickhouse-activerecord","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldesantis%2Fclickhouse-activerecord/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldesantis%2Fclickhouse-activerecord/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldesantis%2Fclickhouse-activerecord/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aldesantis","download_url":"https://codeload.github.com/aldesantis/clickhouse-activerecord/tar.gz/refs/heads/rails_7","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":220666817,"owners_count":16685102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T09:54:06.735Z","updated_at":"2024-10-20T03:01:22.194Z","avatar_url":"https://github.com/aldesantis.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Clickhouse::Activerecord\n\nA Ruby database ActiveRecord driver for ClickHouse. Support Rails \u003e= 5.2.\nSupport ClickHouse version from 22.0 LTS.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'clickhouse-activerecord'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install clickhouse-activerecord\n    \n## Available database connection parameters\n```yml\ndefault: \u0026default\n  adapter: clickhouse\n  database: database\n  host: localhost\n  port: 8123\n  username: username\n  password: password\n  ssl: true # optional for using ssl connection\n  debug: true # use for showing in to log technical information\n  migrations_paths: db/clickhouse # optional, default: db/migrate_clickhouse\n  cluster_name: 'cluster_name' # optional for creating tables in cluster \n  replica_name: '{replica}' # replica macros name, optional for creating replicated tables\n```\n\nAlternatively if you wish to pass a custom `Net::HTTP` transport (or any other\nobject which supports a `.post()` function with the same parameters as\n`Net::HTTP`'s), you can do this directly instead of specifying\n`host`/`port`/`ssl`:\n\n```ruby\nclass ActionView \u003c ActiveRecord::Base\n  establish_connection(\n    adapter: 'clickhouse',\n    database: 'database',\n    connection: Net::HTTP.start('http://example.org', 8123)\n  )\nend\n```\n\n## Usage in Rails 5\n\nAdd your `database.yml` connection information with postfix `_clickhouse` for you environment:\n\n```yml\ndevelopment_clickhouse:\n  adapter: clickhouse\n  database: database\n```\n\nAdd to your model:\n\n```ruby\nclass Action \u003c ActiveRecord::Base\n  establish_connection \"#{Rails.env}_clickhouse\".to_sym\nend\n```\n\nFor materialized view model add:\n```ruby\nclass ActionView \u003c ActiveRecord::Base\n  establish_connection \"#{Rails.env}_clickhouse\".to_sym\n  self.is_view = true\nend\n```\n\nOr global connection:\n\n```yml\ndevelopment:\n  adapter: clickhouse\n  database: database\n```\n\n## Usage in Rails 6 with second database\n\nAdd your `database.yml` connection information for you environment:\n\n```yml\ndevelopment:\n  primary:\n    ...\n    \n  clickhouse:\n    adapter: clickhouse\n    database: database\n```\n\nConnection [Multiple Databases with Active Record](https://guides.rubyonrails.org/active_record_multiple_databases.html) or short example:\n\n```ruby\nclass Action \u003c ActiveRecord::Base\n  connects_to database: { writing: :clickhouse, reading: :clickhouse }\nend\n```\n\n### Rake tasks\n\n**Note!** For Rails 6 you can use default rake tasks if you configure `migrations_paths` in your `database.yml`, for example: `rake db:migrate`\n\nCreate / drop / purge / reset database:\n \n    $ rake clickhouse:create\n    $ rake clickhouse:drop\n    $ rake clickhouse:purge\n    $ rake clickhouse:reset\n\nPrepare system tables for rails:\n\n    $ rake clickhouse:prepare_schema_migration_table\n    $ rake clickhouse:prepare_internal_metadata_table\n    \nMigration:\n\n    $ rails g clickhouse_migration MIGRATION_NAME COLUMNS\n    $ rake clickhouse:migrate\n    $ rake clickhouse:rollback\n\n### Dump / Load for multiple using databases\n\nIf you using multiple databases, for example: PostgreSQL, Clickhouse.\n\nSchema dump to `db/clickhouse_schema.rb` file:\n\n    $ rake clickhouse:schema:dump\n    \nSchema load from `db/clickhouse_schema.rb` file:\n\n    $ rake clickhouse:schema:load\n\nFor export schema to PostgreSQL, you need use:\n\n    $ rake clickhouse:schema:dump -- --simple\n\nSchema will be dump to `db/clickhouse_schema_simple.rb`. If default file exists, it will be auto update after migration.\n    \nStructure dump to `db/clickhouse_structure.sql` file:\n\n    $ rake clickhouse:structure:dump\n    \nStructure load from `db/clickhouse_structure.sql` file:\n\n    $ rake clickhouse:structure:load\n\n### Dump / Load for only Clickhouse database using\n\n    $ rake db:schema:dump  \n    $ rake db:schema:load  \n    $ rake db:structure:dump  \n    $ rake db:structure:load  \n    \n### Insert and select data\n\n```ruby\nAction.where(url: 'http://example.com', date: Date.current).where.not(name: nil).order(created_at: :desc).limit(10)\n# Clickhouse Action Load (10.3ms)  SELECT actions.* FROM actions WHERE actions.date = '2017-11-29' AND actions.url = 'http://example.com' AND (actions.name IS NOT NULL)  ORDER BY actions.created_at DESC LIMIT 10\n#=\u003e #\u003cActiveRecord::Relation [#\u003cAction *** \u003e]\u003e\n\nAction.create(url: 'http://example.com', date: Date.yesterday)\n# Clickhouse Action Load (10.8ms)  INSERT INTO actions (url, date) VALUES ('http://example.com', '2017-11-28')\n#=\u003e true\n \nActionView.maximum(:date)\n# Clickhouse (10.3ms)  SELECT maxMerge(actions.date) FROM actions\n#=\u003e 'Wed, 29 Nov 2017'\n\nAction.where(date: Date.current).final.limit(10)\n# Clickhouse Action Load (10.3ms)  SELECT actions.* FROM actions FINAL WHERE actions.date = '2017-11-29' LIMIT 10\n#=\u003e #\u003cActiveRecord::Relation [#\u003cAction *** \u003e]\u003e\n\nAction.settings(optimize_read_in_order: 1).where(date: Date.current).limit(10)\n# Clickhouse Action Load (10.3ms)  SELECT actions.* FROM actions FINAL WHERE actions.date = '2017-11-29' LIMIT 10 SETTINGS optimize_read_in_order = 1\n#=\u003e #\u003cActiveRecord::Relation [#\u003cAction *** \u003e]\u003e\n```\n\n\n### Migration Data Types\n\nInteger types are unsigned by default. Specify signed values with `:unsigned =\u003e\nfalse`. The default integer is `UInt32`\n\n| Type (bit size)    | Range | :limit (byte size) |\n| :---        |    :----:   |          ---: |\n| Int8 | -128 to 127 | 1 | \n| Int16 | -32768 to 32767 | 2 |\n| Int32 | -2147483648 to 2,147,483,647 | 3,4 |\n| Int64 | -9223372036854775808 to 9223372036854775807] |  5,6,7,8 |\n| Int128 | ... | 9 - 15 |\n| Int256 | ... | 16+ |\n| UInt8 | 0 to 255 | 1 |\n| UInt16 | 0 to 65,535 | 2 |\n| UInt32 | 0 to 4,294,967,295 | 3,4 |\n| UInt64 | 0 to 18446744073709551615 | 5,6,7,8 |\n| UInt256 | 0 to ... | 8+ |\n| Array | ... | ... |\n\nExample:\n\n``` ruby\nclass CreateDataItems \u003c ActiveRecord::Migration\n  def change\n    create_table \"data_items\", id: false, options: \"VersionedCollapsingMergeTree(sign, version) PARTITION BY toYYYYMM(day) ORDER BY category\", force: :cascade do |t|\n      t.date \"day\", null: false\n      t.string \"category\", null: false\n      t.integer \"value_in\", null: false\n      t.integer \"sign\", limit: 1, unsigned: false, default: -\u003e { \"CAST(1, 'Int8')\" }, null: false\n      t.integer \"version\", limit: 8, default: -\u003e { \"CAST(toUnixTimestamp(now()), 'UInt64')\" }, null: false\n    end\n  end\nend\n      \n```\n\n\n### Using replica and cluster params in connection parameters\n\n```yml\ndefault: \u0026default\n  ***\n  cluster_name: 'cluster_name'\n  replica_name: '{replica}'\n```\n\n`ON CLUSTER cluster_name` will be attach to all queries create / drop.\n\nEngines `MergeTree` and all support replication engines will be replaced to `Replicated***('/clickhouse/tables/cluster_name/database.table', '{replica}')`\n\n## Donations\n\nDonations to this project are going directly to [PNixx](https://github.com/PNixx), the original author of this project:\n\n* BTC address: `1H3rhpf7WEF5JmMZ3PVFMQc7Hm29THgUfN`\n* ETH address: `0x6F094365A70fe7836A633d2eE80A1FA9758234d5`\n* XMR address: `42gP71qLB5M43RuDnrQ3vSJFFxis9Kw9VMURhpx9NLQRRwNvaZRjm2TFojAMC8Fk1BQhZNKyWhoyJSn5Ak9kppgZPjE17Zh`\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at [https://github.com/pnixx/clickhouse-activerecord](https://github.com/pnixx/clickhouse-activerecord). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faldesantis%2Fclickhouse-activerecord","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faldesantis%2Fclickhouse-activerecord","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faldesantis%2Fclickhouse-activerecord/lists"}