oss-gate / workshop

OSSの開発に未参加または参加したことはあるけどまだ自信がない人を後押しするワークショップ用のリポジトリー
125 stars 547 forks source link

OSS Gate Workshop: online: 2023-12-09: obregonia1: ridgepole: Work log #1744

Closed obregonia1 closed 11 months ago

obregonia1 commented 11 months ago

This is a work log of a "OSS Gate workshop". "OSS Gate workshop" is an activity to increase OSS developers. Here's been discussed in Japanese. Thanks.

作業ログ作成時の説明

以下のテンプレートを埋めてタイトルに設定します。埋め方例はスクロールすると見えてきます。

OSS Gate Workshop: ${LOCATION}: ${YEAR}-${MONTH}-${DAY}: ${ACCOUNT_NAME}: ${OSS_NAME}: Work log

タイトル例↓:

OSS Gate Workshop: Tokyo: 2017-01-16: kou: Rabbit: Work log

OSS Gateワークショップ関連情報

obregonia1 commented 11 months ago

https://github.com/ridgepole/ridgepole https://github.com/vifreefly/kimuraframework https://github.com/rubocop/rubocop

obregonia1 commented 11 months ago

ridgepole をやる

obregonia1 commented 11 months ago

ライセンスは MIT License

obregonia1 commented 11 months ago
$ gem install ridgepole
obregonia1 commented 11 months ago

https://github.com/ridgepole/ridgepole#help help の項目に $ ridgepole -h の実行結果が載っているがコマンド自体は省略されている(不要?)

obregonia1 commented 11 months ago
$ mkdir oss-gate
$ cd oss-gate
$ git init
obregonia1 commented 11 months ago

config.yml 作成

adapter: mysql2
encoding: utf8
database: blog
username: root
obregonia1 commented 11 months ago

コマンドがエラーになる

$ ridgepole -c config.yml --export -o Schemafile

/Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/rubygems/dependency.rb:311:in `to_specs': Error loading the 'mysql2' Active Record adapter. Missing a gem it depends on? Could not find 'mysql2' (~> 0.5) among 244 total gem(s) (LoadError)
Checked in 'GEM_PATH=/Users/kentaro/.gem/ruby/3.1.0:/Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0' , execute `gem env` for more information
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/rubygems/dependency.rb:323:in `to_spec'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/mysql2_adapter.rb:6:in `<top (required)>'
    from <internal:/Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
    from <internal:/Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_handler.rb:268:in `resolve_pool_config'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_handler.rb:129:in `establish_connection'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_handling.rb:52:in `establish_connection'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ridgepole-2.0.1/lib/ridgepole/client.rb:8:in `initialize'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ridgepole-2.0.1/bin/ridgepole:181:in `new'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ridgepole-2.0.1/bin/ridgepole:181:in `<top (required)>'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/bin/ridgepole:25:in `load'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/bin/ridgepole:25:in `<main>'
/Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'mysql2' (~> 0.5) among 244 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/Users/kentaro/.gem/ruby/3.1.0:/Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0' , execute `gem env` for more information
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/rubygems/dependency.rb:323:in `to_spec'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/mysql2_adapter.rb:6:in `<top (required)>'
    from <internal:/Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
    from <internal:/Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_handler.rb:268:in `resolve_pool_config'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_handler.rb:129:in `establish_connection'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_handling.rb:52:in `establish_connection'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ridgepole-2.0.1/lib/ridgepole/client.rb:8:in `initialize'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ridgepole-2.0.1/bin/ridgepole:181:in `new'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ridgepole-2.0.1/bin/ridgepole:181:in `<top (required)>'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/bin/ridgepole:25:in `load'
    from /Users/kentaro/.asdf/installs/ruby/3.1.2/bin/ridgepole:25:in `<main>'
obregonia1 commented 11 months ago
$ gem i mysql2
obregonia1 commented 11 months ago
$ ridgepole -c config.yml --export -o Schemafile
[ERROR] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
    /Users/kentaro/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/mysql2_adapter.rb:53:in `rescue in new_client'

mysql サーバーが立ち上がってない?

obregonia1 commented 11 months ago
$ brew services start mysql
obregonia1 commented 11 months ago

Schemafile 作成

create_table "articles", force: :cascade do |t|
  t.string   "title"
  t.text     "text"
  t.datetime "created_at"
  t.datetime "updated_at"
end
obregonia1 commented 11 months ago

rails アプリを作成

rails new oss-gate
obregonia1 commented 11 months ago
$ cd oss-gate
$ bundle install
$ bin/rails db:create
obregonia1 commented 11 months ago

sqlite で作ってしまったので config.yml 変更

- adapter: mysql2
+ adapter: sqlite3
obregonia1 commented 11 months ago
$ ridgepole -c config.yml --export -o Schemafile
Export Schema to `Schemafile`
obregonia1 commented 11 months ago
$ git add .
$ git commit -m 'first commit'  -a
obregonia1 commented 11 months ago

sqlite は使えないみたいなので rails アプリを作り直す

$ rails new oss-gate -d mysql
$ bundle install
$ bin/rails db:create
obregonia1 commented 11 months ago

一旦 articles テーブル作成

$ rails generate migration CreateArticles
      invoke  active_record
      create    db/migrate/20231209072937_create_articles.rb
$ vi db/migrate/20231209072937_create_articles.rb

class CreateArticles < ActiveRecord::Migration[7.0]
  def change
    create_table :articles do |t|
      t.string :title
      t.text :body

      t.timestamps
    end
  end
end
$ rails db:migrate
obregonia1 commented 11 months ago

DB のスキーマをもとに Schemafile を生成

$ ridgepole -c config.yml --export -o Schemafile
Export Schema to `Schemafile`
$ cat Schemafile

# -*- mode: ruby -*-
# vi: set ft=ruby :
create_table "articles", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
  t.string "title"
  t.text "body"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end
obregonia1 commented 11 months ago
$ git add .
$ git commit -m 'first commit'  -a
obregonia1 commented 11 months ago

カラムを追加する

$ vi Schemafile

  t.text 'body'
+  t.text 'author'
  t.datetime 'created_at', null: false
obregonia1 commented 11 months ago

dry-run する

$ ridgepole -c config.yml --apply --dry-run

Apply `Schemafile` (dry-run)
add_column("articles", "author", :text, **{:after=>"body", :limit=>65535})

# ALTER TABLE `articles` ADD `author` text AFTER `body`

カラムが追加されている

obregonia1 commented 11 months ago

apply する

$ ridgepole -c config.yml --apply

Apply `Schemafile`
-- add_column("articles", "author", :text, {:after=>"body", :limit=>65535})
   -> 0.0101s
komagata commented 11 months ago

sqliteは使えなさそうな雰囲気がありました。

obregonia1 commented 11 months ago

@komagata ありがとうございます!

obregonia1 commented 11 months ago

https://github.com/ridgepole/ridgepole/blob/2.0/lib/ridgepole/cli/config.rb ここでアダプターを設定している

komagata commented 11 months ago

https://github.com/ridgepole/ridgepole/blob/4bdef4d16ea7660812f0eb86b87db1dd780712ea/lib/ridgepole/connection_adapters.rb#L5-L11

こう書いてあるのでMySQLとPostgreSQLのみ対応っぽいです。

obregonia1 commented 11 months ago

なるほどー!ありがとうございます!

obregonia1 commented 11 months ago

https://github.com/ridgepole/ridgepole#usage

例を以下のようにするとよさそう

$ cat config.yml
adapter: mysql2(or postgresql)
encoding: utf8
database: blog
username: root
obregonia1 commented 11 months ago

https://github.com/ridgepole/ridgepole/compare/2.0...obregonia1:ridgepole:2.0 プッシュしたので description を考える

obregonia1 commented 11 months ago
obregonia1 commented 11 months ago

ChatGPT に訳してもらった

obregonia1 commented 11 months ago

https://github.com/ridgepole/ridgepole/pull/457 PR 作成

komagata commented 11 months ago

@obregonia1 PRのタイトルを具体的に書いた方がいいかもです。

受け取ったプロジェクトの人からすると「README修正」というPRが来たという感じになってしまうので。

以前のワークショップの時のIssue/PRなどを参考にすると良いかもです〜。 https://github.com/oss-gate/workshop/wiki/Workshop%E3%81%AE%E6%88%90%E6%9E%9C%E7%89%A92023%E2%80%9009%E2%80%9030

obregonia1 commented 11 months ago

テーブル名をリネームしてみる

$ vi Schemafile

- create_table 'articles' do |t|
+ create_table 'user_articles', renamed_from: 'articles' do |t|
$ ridgepole -c config.yml --apply --dry-run
Apply `Schemafile` (dry-run)
rename_table("articles", "user_articles")

# RENAME TABLE `articles` TO `user_articles`
obregonia1 commented 11 months ago

@komagata 了解しました🙆‍♂️

komagata commented 11 months ago

PRを受ける人の気持ちに立って考えると良いかもです。

いきなり「AAAを修正した」とだけくるといろいろ困るかもです。 元になるIssueがない場合は何故その変更が必要なのか(何が困るのか)などを丁寧に書いた方が良いと思います。

これは必須ではないですが、PRする側は変更を取り込んでもらう側なので、初めてIssueやPRを送る場合は感謝など言葉を付け加えてから始めると取り入れられやすいです。

komagata commented 11 months ago

この辺りとかも参考になると思います〜 https://github.com/oss-gate/first-feedback-guidebook/blob/master/chapters/examples.md

komagata commented 11 months ago

設定ファイルの記載例にちょこっと追加するよりも、そもそも対応DBの記述をREADMEに文章で追加した方が僕らのように迷わないかもと思いました〜。

github-actions[bot] commented 11 months ago

おつかれさまでした!

ワークショップの終了にともないissueを閉じますが、このまま作業メモとして使っても構いません :ok_hand:

ワークショップの感想を集めています!

ブログなどに書かれた際は、このページへリンクの追加をお願いします :pray:

またの参加をお待ちしています!