{"id":18408335,"url":"https://github.com/pingcap/activerecord-tidb-adapter","last_synced_at":"2025-07-17T00:05:53.701Z","repository":{"id":37727723,"uuid":"392572648","full_name":"pingcap/activerecord-tidb-adapter","owner":"pingcap","description":"TiDB adapter for ActiveRecord, allows the use of TiDB as a backend for ActiveRecord and Rails apps.","archived":false,"fork":false,"pushed_at":"2023-09-05T11:12:51.000Z","size":127,"stargazers_count":24,"open_issues_count":3,"forks_count":4,"subscribers_count":31,"default_branch":"main","last_synced_at":"2025-07-14T20:29:55.333Z","etag":null,"topics":["activerecord","mysql","rails","tidb"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pingcap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-04T06:16:26.000Z","updated_at":"2024-02-10T04:44:24.000Z","dependencies_parsed_at":"2024-11-06T03:20:02.135Z","dependency_job_id":"cc7a7546-b07c-47ae-8f46-f0f52407fadb","html_url":"https://github.com/pingcap/activerecord-tidb-adapter","commit_stats":{"total_commits":86,"total_committers":5,"mean_commits":17.2,"dds":"0.34883720930232553","last_synced_commit":"d2ea7824552ebe7303367b6952aecf17964aa7be"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/pingcap/activerecord-tidb-adapter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pingcap%2Factiverecord-tidb-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pingcap%2Factiverecord-tidb-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pingcap%2Factiverecord-tidb-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pingcap%2Factiverecord-tidb-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pingcap","download_url":"https://codeload.github.com/pingcap/activerecord-tidb-adapter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pingcap%2Factiverecord-tidb-adapter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265552849,"owners_count":23786972,"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":["activerecord","mysql","rails","tidb"],"created_at":"2024-11-06T03:18:31.914Z","updated_at":"2025-07-17T00:05:53.681Z","avatar_url":"https://github.com/pingcap.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ActiveRecord TiDB Adapter\n\n[![Gem Version](https://badge.fury.io/rb/activerecord-tidb-adapter.svg)](https://badge.fury.io/rb/activerecord-tidb-adapter)\n[![activerecord-tidb-adapter 7.0](https://github.com/pingcap/activerecord-tidb-adapter/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/pingcap/activerecord-tidb-adapter/actions/workflows/ci.yml)\n\n⚠️ This Gem is not currently maintained. If you're looking to connect to TiDB serverless with a Rails application, follow our [TiDB serverless ruby connect example](https://github.com/hooopo/tidb-serverless-ruby-connect-example) \n\n⚠️ Since this Gem is a solution for the compatibility issues left by the older versions (prior to 6.0) of TiDB, TiDB Cloud and TiDB serverless will always use the latest versions, so there is no need for additional adapters for integration. You can directly use ActiveRecord or the mysql2 gem.\n\n------\n\nTiDB adapter for ActiveRecord 5.2, 6.1 and 7.0\nThis is a lightweight extension of the mysql2 adapter that establishes compatibility with [TiDB](https://github.com/pingcap/tidb).\n\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'activerecord-tidb-adapter', '~\u003e 7.0.0'\n```\n\nIf you're using Rails 5.2, use the 5.2.x versions of this gem.\n\nIf you're using Rails 6.1, use the 6.1.x versions of this gem.\n\nIf you're using Rails 7.0, use the 7.0.x versions of this gem.\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install activerecord-tidb-adapter\n\n## Usage\n\nconfig/database.yml\n\n```yml\ndefault: \u0026default\n  adapter: tidb\n  encoding: utf8mb4\n  collation: utf8mb4_general_ci\n  pool: \u003c%= ENV.fetch(\"RAILS_MAX_THREADS\") { 5 } %\u003e\n  host: 127.0.0.1\n  port: 4000\n  variables:\n    tidb_enable_noop_functions: ON\n  username: root\n  password:\n\ndevelopment:\n  \u003c\u003c: *default\n  database: activerecord_tidb_adapter_demo_development\n\n```\n\n* demo repo with rails 6.1.4: https://github.com/hooopo/activerecord-tidb-adapter-demo\n\n## TiDB features\n\n**[Sequence](https://docs.pingcap.com/tidb/stable/sql-statement-create-sequence)**\n\nSequence as primary key\n\n```ruby\nclass TestSeq \u003c ActiveRecord::Migration[6.1]\n  def up\n    # more options: increment, min_value, cycle, cache\n    create_sequence :orders_seq, start: 1024\n    create_table :orders, id: false do |t|\n      t.primary_key :id, default: -\u003e { \"nextval(orders_seq)\" }\n      t.string :name\n    end\n  end\n\n  def down\n    drop_table :orders\n    drop_sequence :orders_seq\n  end\nend\n```\n\nThis gem also adds a few helpers to interact with `SEQUENCE`\n\n```ruby\n# Advance sequence and return new value\nActiveRecord::Base.nextval(\"numbers\")\n\n# Return value most recently obtained with nextval for specified sequence.\nActiveRecord::Base.lastval(\"numbers\")\n\n# Set sequence's current value\nActiveRecord::Base.setval(\"numbers\", 1234)\n```\n\n**[CTE](https://docs.pingcap.com/tidb/dev/sql-statement-with#with)**\n\n```bash\n$ bundle add activerecord-cte\n\n```\n\n```ruby\nrequire 'activerecord/cte'\n\nPost\n  .with(posts_with_tags: \"SELECT * FROM posts WHERE tags_count \u003e 0\")\n  .from(\"posts_with_tags AS posts\")\n# WITH posts_with_tags AS (\n#   SELECT * FROM posts WHERE (tags_count \u003e 0)\n# )\n# SELECT * FROM posts_with_tags AS posts\n\nPost\n  .with(posts_with_tags: \"SELECT * FROM posts WHERE tags_count \u003e 0\")\n  .from(\"posts_with_tags AS posts\")\n  .count\n\n# WITH posts_with_tags AS (\n#   SELECT * FROM posts WHERE (tags_count \u003e 0)\n# )\n# SELECT COUNT(*) FROM posts_with_tags AS posts\n\nPost\n  .with(posts_with_tags: Post.where(\"tags_count \u003e 0\"))\n  .from(\"posts_with_tags AS posts\")\n  .count\n\n```\n\n\n## Setting up local TiDB server\n\nInstall [tiup](https://github.com/pingcap/tiup)\n\n```shell\n$ curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh\n```\nStarting TiDB playground\n\n```shell\n$ tiup playground  nightly\n```\n\n## Tutorials\n\n* [Build a Rails App with TiDB and the ActiveRecord TiDB Adapter](https://gist.github.com/hooopo/83db933ab07a054f70e23da0ff945747)\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Testing\n\ninstall gems\n\n```\nbundle install\n```\n\nstart tidb server\n\n```\ntiup playground  nightly\n```\n\ncreate database for testing\n\n```\nMYSQL_USER=root MYSQL_HOST=127.0.0.1 MYSQL_PORT=4000 tidb=1 ARCONN=tidb bundle exec rake db:tidb:rebuild\n\n```\n\nrun tidb adapter tests and activerecord buildin tests\n\n```\nMYSQL_USER=root MYSQL_HOST=127.0.0.1 MYSQL_PORT=4000 tidb=1 ARCONN=tidb bundle exec rake test:tidb\n\n```\n\nrun **ONLY** tidb adapter tests using `ONLY_TEST_TIDB` env:\n\n```\nONLY_TEST_TIDB=1 MYSQL_USER=root MYSQL_HOST=127.0.0.1 MYSQL_PORT=4000 tidb=1 ARCONN=tidb bundle exec rake test:tidb\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/pingcap/activerecord-tidb-adapter.\n\n## License\n\nApache 2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpingcap%2Factiverecord-tidb-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpingcap%2Factiverecord-tidb-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpingcap%2Factiverecord-tidb-adapter/lists"}