Skip to content

Instantly share code, notes, and snippets.

@hkdsun
Last active February 10, 2021 18:29
Show Gist options
  • Save hkdsun/1465b858cdf8fb39df33a45653150063 to your computer and use it in GitHub Desktop.
Save hkdsun/1465b858cdf8fb39df33a45653150063 to your computer and use it in GitHub Desktop.
diff --git c/Gemfile w/Gemfile
index c96f2d6..9fae90d 100644
--- c/Gemfile
+++ w/Gemfile
@@ -6,7 +6,7 @@ ruby '2.7.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.1.1'
# Use sqlite3 as the database for Active Record
-gem 'sqlite3', '~> 1.4'
+gem 'mysql2'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
diff --git c/Gemfile.lock w/Gemfile.lock
index f138bbb..31ebeaa 100644
--- c/Gemfile.lock
+++ w/Gemfile.lock
@@ -101,6 +101,7 @@ GEM
mini_mime (1.0.2)
minitest (5.14.3)
msgpack (1.4.2)
+ mysql2 (0.5.3)
nio4r (2.5.4)
nokogiri (1.11.1-x86_64-linux)
racc (~> 1.4)
@@ -169,7 +170,6 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
- sqlite3 (1.4.2)
thor (1.1.0)
tilt (2.0.10)
turbolinks (5.2.1)
@@ -199,6 +199,7 @@ GEM
zeitwerk (2.4.2)
PLATFORMS
+ ruby
x86_64-linux
DEPENDENCIES
@@ -207,13 +208,13 @@ DEPENDENCIES
capybara (>= 3.26)
jbuilder (~> 2.7)
listen (~> 3.3)
+ mysql2
puma (~> 5.0)
rack-mini-profiler (~> 2.0)
rails (~> 6.1.1)
sass-rails (>= 6)
selenium-webdriver
spring
- sqlite3 (~> 1.4)
turbolinks (~> 5)
tzinfo-data
web-console (>= 4.1.0)
diff --git c/app/models/product.rb w/app/models/product.rb
new file mode 100644
index 0000000..2b2ac32
--- /dev/null
+++ w/app/models/product.rb
@@ -0,0 +1,2 @@
+class Product < ShardedRecord
+end
diff --git c/app/models/sharded_record.rb w/app/models/sharded_record.rb
new file mode 100644
index 0000000..11f83fa
--- /dev/null
+++ w/app/models/sharded_record.rb
@@ -0,0 +1,5 @@
+class ShardedRecord < ApplicationRecord
+ self.abstract_class = true
+
+ connects_to database: { writing: :sharded }
+end
diff --git c/config/database.yml w/config/database.yml
index 4a8a1b2..8265560 100644
--- c/config/database.yml
+++ w/config/database.yml
@@ -1,25 +1,39 @@
-# SQLite. Versions 3.8.0 and up are supported.
-# gem install sqlite3
-#
-# Ensure the SQLite 3 gem is defined in your Gemfile
-# gem 'sqlite3'
-#
default: &default
- adapter: sqlite3
+ adapter: mysql2
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
+ host: 127.0.0.1
+ port: 2225
+ username: vt
+ password:
+ encoding: utf8
development:
- <<: *default
- database: db/development.sqlite3
+ primary:
+ <<: *default
+ database: unsharded
+ sharded:
+ <<: *default
+ database: sharded
+ migrations_paths: "db/migrate_sharded"
# 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: db/test.sqlite3
+ primary:
+ <<: *default
+ database: unsharded
+ sharded:
+ <<: *default
+ database: sharded
+ migrations_paths: "db/migrate_sharded"
production:
- <<: *default
- database: db/production.sqlite3
+ primary:
+ <<: *default
+ database: unsharded
+ sharded:
+ <<: *default
+ database: sharded
+ migrations_paths: "db/migrate_sharded"
diff --git c/db/migrate_sharded/20210210182604_create_products.rb w/db/migrate_sharded/20210210182604_create_products.rb
new file mode 100644
index 0000000..9eae46a
--- /dev/null
+++ w/db/migrate_sharded/20210210182604_create_products.rb
@@ -0,0 +1,9 @@
+class CreateProducts < ActiveRecord::Migration[6.1]
+ def change
+ create_table :products do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
diff --git c/db/schema.rb w/db/schema.rb
new file mode 100644
index 0000000..77bcf0f
--- /dev/null
+++ w/db/schema.rb
@@ -0,0 +1,15 @@
+# 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 `bin/rails
+# db:schema:load`. When creating a new database, `bin/rails db: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.
+
+ActiveRecord::Schema.define(version: 0) do
+
+end
diff --git c/db/sharded_schema.rb w/db/sharded_schema.rb
new file mode 100644
index 0000000..3fd68a3
--- /dev/null
+++ w/db/sharded_schema.rb
@@ -0,0 +1,21 @@
+# 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 `bin/rails
+# db:schema:load`. When creating a new database, `bin/rails db: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.
+
+ActiveRecord::Schema.define(version: 2021_02_10_182604) do
+
+ create_table "products", charset: "utf8", force: :cascade do |t|
+ t.string "name"
+ t.datetime "created_at", precision: 6, null: false
+ t.datetime "updated_at", precision: 6, null: false
+ end
+
+end
diff --git c/db/vitess/schema/sharded/vschema.json w/db/vitess/schema/sharded/vschema.json
new file mode 100644
index 0000000..00f12ba
--- /dev/null
+++ w/db/vitess/schema/sharded/vschema.json
@@ -0,0 +1,51 @@
+{
+ "sharded": true,
+ "vindexes": {
+ "use_shard_0": {
+ "type": "null"
+ },
+ "hash": {
+ "type": "hash"
+ }
+ },
+ "tables": {
+ "schema_migrations": {
+ "column_vindexes": [
+ {
+ "column": "version",
+ "name": "use_shard_0"
+ }
+ ],
+ "columns": [
+ {
+ "name": "version",
+ "type": "VARCHAR"
+ }
+ ]
+ },
+ "ar_internal_metadata": {
+ "column_vindexes": [
+ {
+ "column": "key",
+ "name": "use_shard_0"
+ }
+ ]
+ },
+ "products": {
+ "column_vindexes": [
+ {
+ "column": "id",
+ "name": "hash"
+ }
+ ]
+ },
+ "sharded_products": {
+ "column_vindexes": [
+ {
+ "column": "id",
+ "name": "hash"
+ }
+ ]
+ }
+ }
+}
diff --git c/docker-compose.yml w/docker-compose.yml
new file mode 100644
index 0000000..4e6274d
--- /dev/null
+++ w/docker-compose.yml
@@ -0,0 +1,26 @@
+version: "3.0"
+services:
+ vttest:
+ image: "vitess/vttestserver:mysql57"
+ command:
+ - "/vt/bin/vttestserver"
+ - "-alsologtostderr"
+ - "-port=2222"
+ - "-mysql_bind_host=0.0.0.0"
+ - "-num_shards=1,2"
+ - "-keyspaces=unsharded,sharded"
+ - "-schema_dir=/schema"
+ volumes:
+ - ./db/vitess/schema:/schema
+ ports:
+ # See below for how port numbers are calculated
+ # https://github.com/vitessio/vitess/blob/fbb4300bb37efcfc0c6468f586a5348ac0751c6f/go/vt/vttest/environment.go#L157
+ #
+ # grpc
+ - "2223:2223"
+ # vtcombo http
+ - "2222:2222"
+ # mysqld
+ - "2224:2224"
+ # vtcombo mysql (canonically vtgate)
+ - "2225:2225"
diff --git c/test/fixtures/products.yml w/test/fixtures/products.yml
new file mode 100644
index 0000000..7d41224
--- /dev/null
+++ w/test/fixtures/products.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ name: MyString
+
+two:
+ name: MyString
diff --git c/test/models/product_test.rb w/test/models/product_test.rb
new file mode 100644
index 0000000..3560151
--- /dev/null
+++ w/test/models/product_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class ProductTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment