Menambahkan Database pada Rails Project
Latar Belakang Masalah
Setelah pada blog post sebelumnya saya menulis tentang “Membuat Rails Project tanpa Database”.
Pada tulisan kali ini, saya akan membahas mengenai bagaimana cara menambahkan database engine pada Rails project yang sudah ada.
Tambahkan Gem
Pertama-tama, tentukan dahulu database engine apa yang akan teman-teman gunakan.
Untuk tulisan ini saya akan menggunakan PostgreSQL sebagai contoh.
Tambahkan saja ke dalam file Gemfile
.
1ruby '2.6.3'23# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'4gem 'rails', '~> 5.2.3'5# Use postgresql as the database for Active Record6gem 'pg', '>= 0.18'7# Use Puma as the app server8gem 'puma', '~> 3.11'9# Use SCSS for stylesheets10gem 'sass-rails', '~> 5.0'
Tambahkan Database.yml
Setelah itu, kita perlu membuat file config/database.yml
.
Untuk PostgreSQL engine, memiliki isi sebagia berikut.
1# PostgreSQL. Versions 9.1 and up are supported.2#3# Install the pg driver:4# gem install pg5# On OS X with Homebrew:6# gem install pg -- --with-pg-config=/usr/local/bin/pg_config7# On OS X with MacPorts:8# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config9# On Windows:10# gem install pg11# Choose the win32 build.12# Install PostgreSQL and put its /bin directory on your path.13#14# Configure Using Gemfile15# gem 'pg'16#17default: &default18 adapter: postgresql19 encoding: unicode20 # For details on connection pooling, see Rails configuration guide21 # http://guides.rubyonrails.org/configuring.html#database-pooling22 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>2324development:25 <<: *default26 database: project_mahal_development2728 # The specified database role being used to connect to postgres.29 # To create additional roles in postgres see `$ createuser --help`.30 # When left blank, postgres will use the default role. This is31 # the same name as the operating system user that initialized the database.32 #username: project_mahal3334 # The password associated with the postgres role (username).35 #password:3637 # Connect on a TCP socket. Omitted by default since the client uses a38 # domain socket that doesn't need configuration. Windows does not have39 # domain sockets, so uncomment these lines.40 #host: localhost4142 # The TCP port the server listens on. Defaults to 5432.43 # If your server runs on a different port number, change accordingly.44 #port: 54324546 # Schema search path. The server defaults to $user,public47 #schema_search_path: myapp,sharedapp,public4849 # Minimum log levels, in increasing order:50 # debug5, debug4, debug3, debug2, debug1,51 # log, notice, warning, error, fatal, and panic52 # Defaults to warning.53 #min_messages: notice5455# Warning: The database defined as "test" will be erased and56# re-generated from your development database when you run "rake".57# Do not set this db to the same as development or production.58test:59 <<: *default60 database: project_mahal_test6162# As with config/secrets.yml, you never want to store sensitive information,63# like your database password, in your source code. If your source code is64# ever seen by anyone, they now have access to your database.65#66# Instead, provide the password as a unix environment variable when you boot67# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database68# for a full rundown on how to provide these environment variables in a69# production deployment.70#71# On Heroku and other platform providers, you may have a full connection URL72# available as an environment variable. For example:73#74# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"75#76# You can use this database configuration with:77#78# production:79# url: <%= ENV['DATABASE_URL'] %>80#81production:82 <<: *default83 database: project_mahal_production84 username: project_mahal85 password: <%= ENV['PROJECT_MAHAL_DATABASE_PASSWORD'] %>
Aktifkan Active Record
Selanjutnya, kita perlu mengaktifkan Active Record pada config/application.rb
.
1require "rails"2# Pick the frameworks you want:3require "active_model/railtie"4require "active_job/railtie"5require "active_record/railtie" # <- aktifkan ini6# require "active_storage/engine"7require "action_controller/railtie"8require "action_mailer/railtie"9require "action_view/railtie"10require "action_cable/engine"11require "sprockets/railtie"12require "rails/test_unit/railtie"
Mantap, tinggal langkah terakhir, membuat database.
Create Database
Jalankan dengan perintah rake
.
$ rails db:create
Apabila menghasilkan output seperti di bawah ini.
Created database 'project_mahal_development'
Created database 'project_mahal_test'
Maka, proses menambahkan database pada Rails project kita telah berhasil.
Mudah-mudahan dapat bermanfaat buat teman-teman.
Terima kasih (^_^)v
Referensi
- How to add a Postgre Database to an existing Rails project
Diakses tanggal: 2019/10/09