From 243e0fa4609a4ccdbdcbf10550a17f6202cf89f8 Mon Sep 17 00:00:00 2001 From: zhs722 <90595458+zhs722@users.noreply.github.com> Date: Thu, 30 May 2024 23:26:43 +0800 Subject: [PATCH] feat: add Ruby activerecord sample (#64) --- .github/workflows/ci.yml | 3 ++ ruby/activerecord/README-CN.md | 61 ++++++++++++++++++++++++++++++++++ ruby/activerecord/README.md | 58 ++++++++++++++++++++++++++++++++ ruby/activerecord/example.rb | 31 +++++++++++++++++ ruby/activerecord/run.sh | 3 ++ 5 files changed, 156 insertions(+) create mode 100644 ruby/activerecord/README-CN.md create mode 100644 ruby/activerecord/README.md create mode 100644 ruby/activerecord/example.rb create mode 100644 ruby/activerecord/run.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 01c2e5e..8ef3d50 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,6 +58,9 @@ jobs: - name: 'sequel' language: 'ruby' with_oceanbase_container: true + - name: 'activerecord' + language: 'ruby' + with_oceanbase_container: true uses: ./.github/workflows/basic-workflow.yml with: module: ${{ matrix.module.name }} diff --git a/ruby/activerecord/README-CN.md b/ruby/activerecord/README-CN.md new file mode 100644 index 0000000..9fee1fe --- /dev/null +++ b/ruby/activerecord/README-CN.md @@ -0,0 +1,61 @@ +# Ruby连接 OceanBase 指南(activerecord) + +[English](README.md) | 简体中文 + +本文介绍如何通过activerecord连接 OceanBase 数据库。 + +## 快速开始 + +在开始之前,需要先确保 mysql2,activerecord已安装。 + +安装命令 + +``` +gem install activerecord +gem install mysql2 +``` + +以 [example.rb](example.rb) 为例。 + +``` +require 'active_record' + +# 数据库配置 +db_config = { + adapter: 'mysql2', + host: '127.0.0.1', + port: 2881, + username: 'root', + password: '', + database: 'test' +} + +begin + # 建立连接 + ActiveRecord::Base.establish_connection(db_config) + + # 测试连接是否成功 + connection = ActiveRecord::Base.connection + result = connection.active? # 检查数据库连接是否有效 + + if result + puts "成功连接到OceanBase数据库" + else + puts "连接到OceanBase数据库失败" + end + +rescue StandardError => e + puts "连接到OceanBase数据库时发生错误: #{e.message}" +ensure + ActiveRecord::Base.connection.close if ActiveRecord::Base.connected? +end +``` + +修改代码中的连接信息,之后你就可以直接使用命令行运行示例代码。 + +```bash +sh run.sh +``` + + ActiveRecord::Base.connection.close if ActiveRecord::Base.connected? +end diff --git a/ruby/activerecord/README.md b/ruby/activerecord/README.md new file mode 100644 index 0000000..9eea9db --- /dev/null +++ b/ruby/activerecord/README.md @@ -0,0 +1,58 @@ +# Ruby Connection OceanBase Guide (activerecord) + +English | [简体中文](README-CN.md) + +This article introduces how to connect to OceanBase database through activerecord. + +## Quick Start + +Before starting, it is necessary to ensure that mysql2,activerecord is installed. + +Installation command + +``` +gem install activerecord +gem install mysql2 +``` + +Taking [example.rb](example.rb) as an example. + +``` +require 'active_record' + +# 数据库配置 +db_config = { + adapter: 'mysql2', + host: '127.0.0.1', + port: 2881, + username: 'root', + password: '', + database: 'test' +} + +begin + # 建立连接 + ActiveRecord::Base.establish_connection(db_config) + + # 测试连接是否成功 + connection = ActiveRecord::Base.connection + result = connection.active? # 检查数据库连接是否有效 + + if result + puts "成功连接到OceanBase数据库" + else + puts "连接到OceanBase数据库失败" + end + +rescue StandardError => e + puts "连接到OceanBase数据库时发生错误: #{e.message}" +ensure + ActiveRecord::Base.connection.close if ActiveRecord::Base.connected? +end +``` + +Modify the connection information in the code, and then you can directly run the example code using the command line. + +```bash +sh run.sh +``` diff --git a/ruby/activerecord/example.rb b/ruby/activerecord/example.rb new file mode 100644 index 0000000..b52080b --- /dev/null +++ b/ruby/activerecord/example.rb @@ -0,0 +1,31 @@ +require 'active_record' + +# 数据库配置 +db_config = { + adapter: 'mysql2', + host: '127.0.0.1', + port: 2881, + username: 'root', + password: '', + database: 'test' +} + +begin + # 建立连接 + ActiveRecord::Base.establish_connection(db_config) + + # 测试连接是否成功 + connection = ActiveRecord::Base.connection + result = connection.active? # 检查数据库连接是否有效 + + if result + puts "成功连接到OceanBase数据库" + else + puts "连接到OceanBase数据库失败" + end + +rescue StandardError => e + puts "连接到OceanBase数据库时发生错误: #{e.message}" +ensure + ActiveRecord::Base.connection.close if ActiveRecord::Base.connected? +end diff --git a/ruby/activerecord/run.sh b/ruby/activerecord/run.sh new file mode 100644 index 0000000..62249b7 --- /dev/null +++ b/ruby/activerecord/run.sh @@ -0,0 +1,3 @@ +gem install activerecord +gem install mysql2 +ruby example.rb