[Rails] DB settings and operations (mysql), Rubocop in VSCode, useful extensions

background

We have summarized the DB settings (mysql) used in Rails apps. We've also put together Rubocop settings in VS Code and other useful extensions.

In my environment, I use VS Code on Windows to operate WLS (Ubuntu). The reason I use WLS is that the environment construction method is quite similar to Mac.

In addition, this article assumes that you have Rails installed. The following is a summary of the prerequisite environment construction, so please refer to it. [Rails] Install Rails locally on WSL2 (Ubuntu, VS Code) on Windows and push to github (master) with git command

DB settings (mysql)

(1) Execute the following command to install mysql.

sudo apt install mysql-server mysql-client

(2) Next, install the mysql library with the following command.

sudo apt install libmysqlclient-dev

If you do not install this library, you will get an error when you install the mysql gem described later.

(3) After installation, check the status with the following command.

sudo service mysql status

If it has not started, start it.

sudo service mysql start

④ Log in to mysql.

sudo mysql -u root

⑤ Create a working user of mysql and set the authority. Please change the following user name`` host name password. If you installed mysql locally, the hostname can be localhost.

mysql> create user 'username'@'hostname' identifid by 'password';
mysql> grant all on *.* to 'username'@'hostname';

⑥ Edit /config/database.yml. The following is an example. Set username`` password in step ⑤. database can optionally set the database name you want to create. Please note that YAML does not use Tab for indentation, indents it, and indents it in units of 2 Spaces. Also, username does not include the host part after @.

development:
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: developer
  password: 1234abcd
  host: localhost
  database: example_development

test:
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: developer
  password: 1234abcd
  host: localhost
  database: example_test

production:
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: developer
  password: 1234abcd
  host: localhost
  database: example_production

⑦ Install gem. Add the following to Gemfie.

gem 'mysql2'

Then install (or update)

bundle install

⑦ Finally, create a database with the following command.

bundle exec rails db:create

Let's confirm that the DB has been created with mysql.

DB operation

I created a table in the DB created above by referring to the following article. ▼ Until migration file creation (preparation for table creation) -Active Record [Tutorial for beginners] to put data into DB with Rails -About Rails table data type

▼ Migration execution (table creation execution) -Basic summary of Rails Migration

▼ Delete table Ruby on Rails deletion related summary

Also, Rails OR mapper contains ActiveRecord by default, so use ActiveRecord from Controller to operate DB.

Introduction of Rubocop

Rubocop is a Ruby linter and formatter. ▼ Install according to the following procedure so that format on save can be used. (Actually, I wanted to install locally and format on save, but in my case I could only format on save with global installation, so I'm installing globally.)

(1) Install the extension Ruby on VS Code.

(2) Execute the following on the command line to install Rubocop.

$ gem install rubocop

(3) Open settings.json from the command palette and add the following.

python


    "ruby.lint": {
        "rubocop": true
    },
    "[ruby]": {
        "editor.formatOnSave": true
    },
    "ruby.format": "rubocop"

With the above, format on save of Ruby file works. It seems that it may not work with the above, but in that case it may be solved by adding the following to settings.json and restarting VS Code.

"editor.formatOnSaveTimeout": 5000,

Other useful extensions

I referred to the following article. Getting along with Rails with VS Code extensions

I have added the following extensions. ・ Rails ・ Ruby On Rails ・ Endwise ・ Rainbow End ・ Indent-rainbow ・ Material Icon Theme ・ Rails DB Schema

Indent-rainbow may lose the color of the indent, but the extension was disabled → re-enabled and fixed.

Serpentine

In the future, I would like to write about building an environment using AWS services.

Finally, thank you to all the contributors of the article for your reference. I would appreciate it if you could point out any deficiencies in my article.

Recommended Posts

[Rails] DB settings and operations (mysql), Rubocop in VSCode, useful extensions
[Rails] Introducing Travis CI and getting stuck in db: create
Error in rails db: migrate
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
[Rails] Validation settings and Japanese localization
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
Commands often used in MySQL operations
Rails DB PostgreSQL changed to MySQL