From 606b86ab47c8215a15fa53a8742f98b55bb037db Mon Sep 17 00:00:00 2001 From: Masaki Suketa Date: Wed, 4 Sep 2019 19:54:08 +0900 Subject: [PATCH] try082 Allow `truncate` for SQLite3 adapter and add `rails db:seed:replant` https://github.com/rails/rails/pull/34779 --- Gemfile | 4 +- Gemfile.lock | 2 + app/models/book.rb | 2 + app/models/user.rb | 2 + config/database.yml | 109 ++++++------------ .../20190904113005_create_books.rb | 9 ++ db/migrate/20190904112936_create_users.rb | 9 ++ docker-compose.yml | 12 +- test/fixtures/books.yml | 7 ++ test/fixtures/users.yml | 7 ++ test/models/book_test.rb | 7 ++ test/models/user_test.rb | 7 ++ 12 files changed, 100 insertions(+), 77 deletions(-) create mode 100644 app/models/book.rb create mode 100644 app/models/user.rb create mode 100644 db/library_migrate/20190904113005_create_books.rb create mode 100644 db/migrate/20190904112936_create_users.rb create mode 100644 test/fixtures/books.yml create mode 100644 test/fixtures/users.yml create mode 100644 test/models/book_test.rb create mode 100644 test/models/user_test.rb diff --git a/Gemfile b/Gemfile index 812081a..ff648a8 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,9 @@ ruby '2.6.4' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 6.0.0' -# Use postgresql as the database for Active Record +# Use mysql as the library database for Active Record +gem 'mysql2', '>= 0.4.4' +# Use postgresql as the backbone database for Active Record gem 'pg', '>= 0.18', '< 2.0' # Use Puma as the app server gem 'puma', '~> 3.11' diff --git a/Gemfile.lock b/Gemfile.lock index 83f8d46..3f732dc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -100,6 +100,7 @@ GEM mini_portile2 (2.4.0) minitest (5.11.3) msgpack (1.3.1) + mysql2 (0.5.2) nio4r (2.5.1) nokogiri (1.10.4) mini_portile2 (~> 2.4.0) @@ -206,6 +207,7 @@ DEPENDENCIES capybara (>= 2.15) jbuilder (~> 2.7) listen (>= 3.0.5, < 3.2) + mysql2 (>= 0.4.4) pg (>= 0.18, < 2.0) puma (~> 3.11) rails (~> 6.0.0) diff --git a/app/models/book.rb b/app/models/book.rb new file mode 100644 index 0000000..03d56b4 --- /dev/null +++ b/app/models/book.rb @@ -0,0 +1,2 @@ +class Book < ApplicationRecord +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..379658a --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,2 @@ +class User < ApplicationRecord +end diff --git a/config/database.yml b/config/database.yml index c9eb01a..9067c4e 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,88 +1,47 @@ -# PostgreSQL. Versions 9.3 and up are supported. -# -# Install the pg driver: -# gem install pg -# On macOS with Homebrew: -# gem install pg -- --with-pg-config=/usr/local/bin/pg_config -# On macOS with MacPorts: -# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config -# On Windows: -# gem install pg -# Choose the win32 build. -# Install PostgreSQL and put its /bin directory on your path. -# -# Configure Using Gemfile -# gem 'pg' -# -default: &default +default_postgresql: &default_postgresql adapter: postgresql encoding: unicode # For details on connection pooling, see Rails configuration guide # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> - host: db + host: postgres user: <%= ENV.fetch("POSTGRES_USER") %> password: <%= ENV.fetch("POSTGRES_PASSWORD") %> -development: - <<: *default - database: app_development - - # The specified database role being used to connect to postgres. - # To create additional roles in postgres see `$ createuser --help`. - # When left blank, postgres will use the default role. This is - # the same name as the operating system user that initialized the database. - #username: app - - # The password associated with the postgres role (username). - #password: - - # Connect on a TCP socket. Omitted by default since the client uses a - # domain socket that doesn't need configuration. Windows does not have - # domain sockets, so uncomment these lines. - #host: localhost +default_mysql: &default_mysql + adapter: mysql2 + encoding: utf8mb4 + username: root + password: + host: mysql - # The TCP port the server listens on. Defaults to 5432. - # If your server runs on a different port number, change accordingly. - #port: 5432 - - # Schema search path. The server defaults to $user,public - #schema_search_path: myapp,sharedapp,public - - # Minimum log levels, in increasing order: - # debug5, debug4, debug3, debug2, debug1, - # log, notice, warning, error, fatal, and panic - # Defaults to warning. - #min_messages: notice +development: + backbone: + <<: *default_postgresql + database: backbone_development + library: + <<: *default_mysql + database: library_development + migrations_paths: db/library_migrate -# 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: app_test + backbone: + <<: *default_postgresql + database: backbone_test + library: + <<: *default_mysql + database: library_test + migrations_paths: db/library_migrate -# As with config/credentials.yml, you never want to store sensitive information, -# like your database password, in your source code. If your source code is -# ever seen by anyone, they now have access to your database. -# -# Instead, provide the password as a unix environment variable when you boot -# the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database -# for a full rundown on how to provide these environment variables in a -# production deployment. -# -# On Heroku and other platform providers, you may have a full connection URL -# available as an environment variable. For example: -# -# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" -# -# You can use this database configuration with: -# -# production: -# url: <%= ENV['DATABASE_URL'] %> -# production: - <<: *default - database: app_production - username: app - password: <%= ENV['APP_DATABASE_PASSWORD'] %> + backbone: + <<: *default_postgresql + database: backbone_production + username: backbone + password: <%= ENV['BACKBONE_DATABASE_PASSWORD'] %> + library: + <<: *default_mysql + database: library_production + migrations_paths: db/library_migrate + username: library + password: <%= ENV['LIBRARY_DATABASE_PASSWORD'] %> diff --git a/db/library_migrate/20190904113005_create_books.rb b/db/library_migrate/20190904113005_create_books.rb new file mode 100644 index 0000000..bb6c374 --- /dev/null +++ b/db/library_migrate/20190904113005_create_books.rb @@ -0,0 +1,9 @@ +class CreateBooks < ActiveRecord::Migration[6.0] + def change + create_table :books do |t| + t.string :title + + t.timestamps + end + end +end diff --git a/db/migrate/20190904112936_create_users.rb b/db/migrate/20190904112936_create_users.rb new file mode 100644 index 0000000..6c10d9c --- /dev/null +++ b/db/migrate/20190904112936_create_users.rb @@ -0,0 +1,9 @@ +class CreateUsers < ActiveRecord::Migration[6.0] + def change + create_table :users do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/docker-compose.yml b/docker-compose.yml index 825d427..1b2061f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,7 +12,7 @@ services: environment: - POSTGRES_USER - POSTGRES_PASSWORD - db: + postgres: image: postgres:10.7-alpine volumes: - pgsqldb:/var/lib/postgresql/data @@ -20,8 +20,18 @@ services: - POSTGRES_USER - POSTGRES_PASSWORD + mysql: + image: mysql:8.0.17 + command: --default-authentication-plugin=mysql_native_password + volumes: + - mysqldb:/var/lib/mysql + environment: + MYSQL_ALLOW_EMPTY_PASSWORD: 'yes' + volumes: pgsqldb: driver: local + mysqldb: + driver: local bundle: driver: local diff --git a/test/fixtures/books.yml b/test/fixtures/books.yml new file mode 100644 index 0000000..64d88ef --- /dev/null +++ b/test/fixtures/books.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + +two: + title: MyString diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..7d41224 --- /dev/null +++ b/test/fixtures/users.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 a/test/models/book_test.rb b/test/models/book_test.rb new file mode 100644 index 0000000..e48079d --- /dev/null +++ b/test/models/book_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class BookTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..82f61e0 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end