Running db:migrate kills the schema file - ruby-on-rails

Every time I run rake db:migrate, my schema file is always changed to this
# A bunch of comments up here
ActiveRecord::Schema.define(version: WHATEVER_MY_LATEST_VERSION_IS) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
end
It will update the schema version, and run the migration. The tables will be created, indexes are created. It just leaves my schema.rb with nothing but this enable_extension in it. Running rake db:schema:dumpwill create the same schema file. I'm running on rails 4.1.10.
EDIT I do have the pg gem installed (0.18.1). There's no outstanding migrations. This app has been in production for over 2 years now. It's been doing this issue for the last year or so, but we only do a new migration maybe twice a year so we haven't put much effort in to it until now. My DB yml is pretty standard:
defaults: &defaults
encoding: utf8
adapter: postgresql
database: <%= ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
host: <%= ENV.fetch('DATABASE_HOST', 'localhost') %>
min_messages: WARNING
development:
<<: *defaults

Related

Rails Heroku change column data type

I run this command for deploying database to heroku
heroku run rails db:migrate
But I got this error
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedObject: ERROR: type "dateti" does not exist
LINE 1: ALTER TABLE "users" ADD "activated_at" dateti
^
After that I changed wrong datatype "dateti" to "datetime"
class AddActivationToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :activation_digest, :string
add_column :users, :activated, :boolean, default: false
add_column :users, :activated_at, :datetime
end
end
But nothing changed. Still get this error. For development and test I use sqlite3 and for production postgresql. This error appear only when I try to deploy to heroku. On local database everything works.
database.yml file
# 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
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# 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
production:
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 } %>
database: sample_app_production
username: sample_app
password: <%= ENV['SAMPLE_APP_DATABASE_PASSWORD'] %>
Before change activited_at (datetime) in AddActivationToUsers file. You must rollback AddActivationToUsers in db.
rails db:rollback STEP=n (n migrations where n is the number of recent migrations you want to rollback)
You change activited_at :datetime and save
rails db:migration

make rake db:create setup another database besides development, test or production

I'm using rails 4.2 and trying to configure (in a already established application) the Audited Gem following this second database approach.
My config/database.yml file was as follows:
default: &default
adapter: mysql2
pool: 5
timeout: 5000
development:
<<: *default
host: <%= ENV["MYSQL_HOST"] %>
username: <%= ENV["MYSQL_USER"] %>
password: <%= ENV["MYSQL_PASSWORD"] %>
database: <%= ENV["MYSQL_DATABASE"] %>
test:
<<: *default
host: <%= ENV["MYSQL_HOST"] %>
username: <%= ENV["MYSQL_USER"] %>
password: <%= ENV["MYSQL_PASSWORD"] %>
database: <%= ENV['TEST_ENV_DB'] %>
And I intend to make it work for another db, besides development, test or production. However the task rake db:create only creates my development and test database. Is this possible to accomplish in my rails version?
audition:
<<: *default
host: <%= ENV["MYSQL_HOST"] %>
username: <%= ENV["MYSQL_USER"] %>
password: <%= ENV["MYSQL_PASSWORD"] %>
database: <%= ENV["AUDITION_DATABASE"] %>
Note the new name for audition database
if you want to read/write to a seconds database in rails < 6
create a module
module AuditionConn
def self.included(base)
base.class_eval do
if Rails.env == 'development'
establish_connection "audition-development" # database.yml
else
establish_connection "audition-production" # database.yml
end
end
end
end
then include it in any model you want to read/write from/to auditions database
class AuditionDBModel < ActiveRecord::Base
include AuditionConn
end
migration for second database
def up
AuditionDBModel.connection.create_table ... do |t|
...
AuditionDBModel.connection.change_column ...
end
I think you want to create a new environment call audition, Right?!.
Clone an existing environment file for instance, config/environments/test.rb and rename it config/environments/audition.rb
Add a new configuration block in config/database.yml for your environment.
Update any other configuration file you might have under the config folder with your new environment, some gems need to config it.
Now you can start the server
rails server -e audition
I think this may help you:
create another model for audit:
class AuditModel < ActiveRecord::Base
connects_to database: { writing: :audit_db, reading: :audit_db}
end
or
ActiveRecord::Base.establish_connection(
adapter: "mysql2",
host: "localhost",
username: "myuser",
password: "mypass",
database: "somedatabase"
)
for details:
https://guides.rubyonrails.org/active_record_multiple_databases.html
https://edgeapi.rubyonrails.org/classes/ActiveRecord/ConnectionHandling.html

Launch rspec tests with sqlite :memory: - "migrations are pending"

I want to run tests using rspec on my ruby on rails app using sqlite with the memory database. However, everytime I launch rspec it tells me that migrations are pending, event if I run the migrations before hand. Is there a way to do the migrations everytime before I run the tests ?
Here's my database configuration
test:
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
database: ":memory:"
You need to load schema in your tests instead of relying on migrations.
As advised in this blogpost replace
ActiveRecord::Migration.maintain_test_schema!
with
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.verbose = false
load "#{Rails.root.to_s}/db/schema.rb"

undefined method `geometry' for ActiveRecord::ConnectionAdapters::PostgreSQL

I have created a rails application using postgres database. I am using postgis extension for geo queries. The app is running successfully on my development(local) machine but after deploying my code on heroku server when I run heroku run rake db:migrate it is throwing an error, saying undefined method geometry for ActiveRecord ConnectionAdapters PostgreSQL.
I have geometry datatype in some migrations for storing latitude and longitude.
Note that I have also created PostGIS extension on heroku. And migrations that does not contain geometry datatype executed successfully.
My files are:
Gemfile
ruby "2.3.0"
gem 'rails', '>= 5.0.0.beta3', '< 5.1'
gem 'pg', '~> 0.18'
gem 'rgeo'
gem 'rgeo-activerecord', "~> 5.0.0.beta"
gem "activerecord-postgis-adapter", "~> 4.0.0.beta2"
psql --version is: 9.5.2 on heroku server
psql --version is: 9.4.7 on local server
database.yml
default: &default
adapter: postgis
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: ad_development
production:
<<: *default
database: ad_production
username: ad
password: <%= ENV['DATABASE_PASSWORD'] %>
create_cities migration
def change
create_table :cities do |t|
t.string :name
t.references :state, foreign_key: true
t.geometry :lat_lan
end
heroku run rake db:migrate stops here only.
I am totally confused whether I have used inappropriate gems or I have misconfigured something. Please help!
If using the DATABASE_URL environment variable to set the database connection string, ensure it has the postgis:// (not postgres://) prefix.
ie. postgis://username:password#db_server_url:5432/dbname
url: <%= ENV.fetch('DATABASE_URL', '').sub(/^postgres/, 'postgis') %>
EDIT:
It replaces the url scheme postgres://somewhere.com to postgis://somewhere.com.
It changes to the GIS "protocol", like changing http to https.
You have to replace postgresql with postgis in your database.yml file for the adapter option.
E.g.
default: &default
adapter: postgis
encoding: unicode
pool: <%= ENV["DB_POOL"] || ENV['MAX_THREADS'] || 5 %>
url: <%= ENV['DATABASE_URL'] %>
development:
<<: *default
database: yourapp_development

Using Sqlite3 test database and Postgres dev/production database in Rails

I currently have a rails project which I deploy to a production server which uses a postgres database. I develop my rails project in Windows, which means that if I want to test locally, I have to change all of the databases in my database.yml file from postgres over to sqlite3 (because setting up Windows to run a postgres server appears to be a pain).
What I would like to be able to do is format my database.yml something like this:
development:
adapter: postgresql
encoding: utf8
database: <%= begin IO.read("/home/www-data/.db/.dev_name") rescue "" end %>
pool: 5
username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>
# 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:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: utf8
database: <%= begin IO.read("/home/www-data/.db/.prod_name") rescue "" end %>
pool: 5
username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>
That way I can run rails s -e test locally and test with an sqlite3 database, but when I deploy to my development and production servers I can use postgres.
The problem I am having is that, with the changes to my database.yml shown above, when I run rails s -e test locally I get an error saying that rails could not find the pg gem which seems to imply that it is still trying to use either the development or the production server.
With all the warnings acknowledged, the answer to the question would be to use group in your Gemfile like
gem 'pg', group: [:development, :production]
gem 'sqlite3', group: :test

Resources