分享 activerecord-tidb-adapter released

hooopo · 2021年08月12日 · 最后由 hooopo 回复于 2021年08月15日 · 634 次阅读

https://github.com/pingcap/activerecord-tidb-adapter

目前已经兼容 ActiveRecord,之后后添加 TiDB 特有的 feature,欢迎测试反馈

现在 GitHub 的正确打开姿势是这个😁https://github.dev/pingcap/activerecord-tidb-adapter (官方)

hjiangwen 回复

🐮 和 github1s 有什么区别?

hooopo 回复

官方功能,看私人 repo“感觉”更加安全了。 在 repo 页面点击 .,就能进入 web 版 vs code。 体验了创建分支、改文件、提交、创建 pr、浏览 pr、评审 pr,十分流畅。更重要的是还支持 vs code 的快捷键!

彩蛋:在 pr 页面也可以通过点击.进入 vs code 模式,评审大 pr 的时候实在是太方便了

4 楼 已删除
5 楼 已删除

这个快捷键不友好,因为大部分时间处于中文输入状态。

增加了 sequence 支持:

Sequence

Sequence as primary key

class TestSeq < ActiveRecord::Migration[6.1]
  def up
    # more options: increment, min_value, cycle, cache
    create_sequence :orders_seq, start: 1024
    create_table :orders, id: false do |t|
      t.primary_key :id, default: -> { "nextval(orders_seq)" }
      t.string :name
    end
  end

  def down
    drop_table :orders
    drop_sequence :orders_seq
  end
end

Generated DDL

mysql> show create table orders_seq\G;
*************************** 1. row ***************************
       Sequence: orders_seq
Create Sequence: CREATE SEQUENCE `orders_seq` start with 1024 minvalue 1 maxvalue 9223372036854775806 increment by 1 nocache nocycle ENGINE=InnoDB

mysql> show create table orders\G;
*************************** 1. row ***************************
       Table: orders
Create Table: CREATE TABLE `orders` (
  `id` bigint(20) NOT NULL DEFAULT nextval(`activerecord_tidb_adapter_demo_development`.`orders_seq`),
  `name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci

This gem also adds a few helpers to interact with SEQUENCE

# Advance sequence and return new value
ActiveRecord::Base.nextval("numbers")

# Return value most recently obtained with nextval for specified sequence.
ActiveRecord::Base.lastval("numbers")

# Set sequence's current value
ActiveRecord::Base.setval("numbers", 1234)
需要 登录 后方可回复, 如果你还没有账号请 注册新账号