Can't drop database using db:drop:_unsafe - ruby-on-rails

I created a Rake test:
namespace :dev do
DEFAULT_PASSWORD = 123456
desc "Sets up dev environment!"
task setup: :environment do
if Rails.env.development?
show_spinner("Deleting database...") { %x(rails db:drop) }
show_spinner("Creating database...") { %x(rails db:create) }
show_spinner("Migrating database...") { %x(rails db:migrate) }
show_spinner("Creating default admin...") { %x(rails dev:add_default_admin) }
show_spinner("Creating default user...") { %x(rails dev:add_default_user) }
# show_spinner("xxxxxxxx...") { %x(rails dev:xxxxxxx) }
else
puts "You are not in the dev enviroment!"
end
end
private
def show_spinner(msg_start, msg_end = "Completed!")
spinner = TTY::Spinner.new("[:spinner] #{msg_start}")
spinner.auto_spin
yield
spinner.success("(#{msg_end})")
end
desc "Adding default admin"
task add_default_admin: :environment do
Admin.create!(
email: "admin#admin.com",
password: DEFAULT_PASSWORD,
password_confirmation: DEFAULT_PASSWORD,
)
end
desc "Adding default user"
task add_default_user: :environment do
User.create!(
email: "user#user.com",
password: DEFAULT_PASSWORD,
password_confirmation: DEFAULT_PASSWORD,
)
end
end
Every time I run rails dev:setup on GitBash or CMD (both having ADMIN permissions) I get :
Errno::EACCES: Permission denied # apply2files
gmitr#DESKTOP-O6F1I8H MINGW64 ~/Documents/ProjectOne/Library (master)
$ rails dev:setup
Permission denied # apply2files - C:/Users/gmitr/Documents/ProjectOne/Library/db/development.sqlite3
Couldn't drop database 'db/development.sqlite3'
rails aborted!
Errno::EACCES: Permission denied # apply2files - C:/Users/gmitr/Documents/ProjectOne/Library/db/development.sqlite3
Tasks: TOP => db:drop:_unsafe
(See full trace by running task with --trace)
Database 'db/development.sqlite3' already exists
Database 'db/test.sqlite3' already exists
rails aborted!
ActiveRecord::RecordInvalid: A validação falhou: Email já está em uso
C:/Users/gmitr/Documents/ProjectOne/Library/lib/tasks/dev.rake:29:in `block (2 levels) in <main>'
Tasks: TOP => dev:add_default_admin
(See full trace by running task with --trace)
rails aborted!
ActiveRecord::RecordInvalid: A validação falhou: Email já está em uso
C:/Users/gmitr/Documents/ProjectOne/Library/lib/tasks/dev.rake:38:in `block (2 levels) in <main>'
Tasks: TOP => dev:add_default_user
(See full trace by running task with --trace)
gmitr#DESKTOP-O6F1I8H MINGW64 ~/Documents/ProjectOne/Library (master)
$
I know about rails db:drop:_unsafe. When I input this code into CMD it does drop the databases, but every time I run rails dev:setup same error occurs even after I drop the databases:
gmitr#DESKTOP-O6F1I8H MINGW64 ~/Documents/ProjectOne/Library (master)
$ rails db:drop:_unsafe
Dropped database 'db/development.sqlite3'
Dropped database 'db/test.sqlite3'
gmitr#DESKTOP-O6F1I8H MINGW64 ~/Documents/ProjectOne/Library (master)
$ rails dev:setup
Permission denied # apply2files - C:/Users/gmitr/Documents/ProjectOne/Library/db/development.sqlite3
Couldn't drop database 'db/development.sqlite3'
rails aborted!
Errno::EACCES: Permission denied # apply2files - C:/Users/gmitr/Documents/ProjectOne/Library/db/development.sqlite3
Tasks: TOP => db:drop:_unsafe
(See full trace by running task with --trace)
Database 'db/development.sqlite3' already exists
gmitr#DESKTOP-O6F1I8H MINGW64 ~/Documents/ProjectOne/Library (master)
$
Is my code wrong? I do have gem tty-spinner installed. I'm using Windows 10, Ruby 3.1.2p20 and Rails 7.0.4.

Related

Where is db:create defined in Rails?

The following command is supposed to create a new database.
rails db:create
Where is this function defined? Or is this a pre-packaged function in rails?
It's in the databases.rake file of the framework:
namespace :create do
task all: :load_config do
ActiveRecord::Tasks::DatabaseTasks.create_all
end
ActiveRecord::Tasks::DatabaseTasks.for_each do |spec_name|
desc "Create #{spec_name} database for current environment"
task spec_name => :load_config do
db_config = ActiveRecord::DatabaseConfigurations.config_for_env_and_spec(Rails.env, spec_name)
ActiveRecord::Tasks::DatabaseTasks.create(db_config.config)
end
end
end
Whenever you doubt or want to know where a task has been defined, you can use the rails -W (or rake) command, passing the task:
$ rails -W db:create
rails db:create /path/databases.rake:26:in `block in <top (required)>'
rails db:create:all /path/databases.rake:20:in `block (2 levels) in <top (required)>'
Note it was introduced in the version 0.9 of Rake. This might or might not work depending on the versions which you're working with.

Uninitialized constant FAKER when "seeding" db [duplicate]

I am trying to run a simple bundle exec rake db:seed for my database in Rails 4. However, when running it, I get the following output:
********-C02MGBVJFD57:myapp ***********$ bundle exec rake db:seed
Your Gemfile lists the gem factory_girl_rails (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
rake aborted!
NameError: uninitialized constant Faker
/Users/**********/workspace/myapp/db/seeds.rb:16:in `block in <top (required)>'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `times'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `<top (required)>'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/railties-4.1.4/lib/rails/engine.rb:543:in `load_seed'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:184:in `load_seed'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
Here is my seeds.rb file:
User.create!(
name: "Example User",
email: "example#railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true
)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(
name: name,
email: email,
password: password,
password_confirmation: password
)
end
Line 16 is:
name = Faker::Name.name
Any ideas why I am getting this error? Thank you.
Just faced similar issue - I was running
rails g model model_name
and getting the error:
uninitialized constant Faker (NameError)
Problem was due to fact, that I had gem added to test group.
Placing it into development and test group solved the problem:
group :development, :test do
# ...
gem 'faker'
# ...
end
I faced the same issue while writing rspec and adding require 'faker' in spec file solved it.
I added gem 'faker' in the Gemfile. Then I run bundle install to get the gem.
As per official faker documentation, it said:
Note: if you are getting a uninitialized constant Faker::[some_class]
error, your version of the gem is behind the one documented here. To
make sure that your gem is the one documented here, change the line in
your Gemfile to:
gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main'
But the problem still persist with mine app, since I have done this and I am getting the error again, but only when try to run the migrations to heroku:
heroku run rails db:migrate db:seed
When I run the command locally I do not have the problem and migrations and seeds are executed.

Error: Rake Aborted. Uninitialized Constant Settings

Newbie here trying to create a development environment for a project. I've installed MySQL and Ruby on Rails on my device already. However, I'm running into the below error message when trying to run the 3 commands below after "bundle install" runs successfully. Any ideas?
bundle exec rake db:setup RAILS_ENV="development"
bundle exec rake db:migrate RAILS_ENV="development"
bundle exec rake db:seed RAILS_ENV=“development"
error message is as follows:
wcai-projects already exists
rake aborted!
uninitialized constant Settings
EDIT-here's the console log:
$ bundle exec rake db:setup RAILS_ENV=development
/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-3.2.12/lib/active_support/values/time_zone.rb:270: warning: circular argument reference - now
wcai-projects already exists
rake aborted!
uninitialized constant Settings
/wcai-web/config/initializers/carrier_wave.rb:4:in `block in <top (required)>'
/wcai-web/config/initializers/carrier_wave.rb:1:in `<top (required)>'
/wcai-web/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:setup => db:schema:load_if_ruby => environment
(See full trace by running task with --trace)
EDIT-contents of config/initializers/carrier_wave.rb:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => Settings.aws_access_key, # required
:aws_secret_access_key => Settings.aws_secret_key # required
# :region => 'eu-west-1' # optional, defaults to 'us-east-1'
}
config.fog_directory = Settings.aws_bucket # required
config.fog_public = false
config.fog_authenticated_url_expiration = 120
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
#config.asset_host = 'https://assets.example.com' # optional, defaults to nil
end
Update your Rails version in Gemfile to 4.2.4. Then type bundle update rails and try again.

NameError: uninitialized constant Faker

I am trying to run a simple bundle exec rake db:seed for my database in Rails 4. However, when running it, I get the following output:
********-C02MGBVJFD57:myapp ***********$ bundle exec rake db:seed
Your Gemfile lists the gem factory_girl_rails (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
rake aborted!
NameError: uninitialized constant Faker
/Users/**********/workspace/myapp/db/seeds.rb:16:in `block in <top (required)>'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `times'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `<top (required)>'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/railties-4.1.4/lib/rails/engine.rb:543:in `load_seed'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:184:in `load_seed'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
Here is my seeds.rb file:
User.create!(
name: "Example User",
email: "example#railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true
)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(
name: name,
email: email,
password: password,
password_confirmation: password
)
end
Line 16 is:
name = Faker::Name.name
Any ideas why I am getting this error? Thank you.
Just faced similar issue - I was running
rails g model model_name
and getting the error:
uninitialized constant Faker (NameError)
Problem was due to fact, that I had gem added to test group.
Placing it into development and test group solved the problem:
group :development, :test do
# ...
gem 'faker'
# ...
end
I faced the same issue while writing rspec and adding require 'faker' in spec file solved it.
I added gem 'faker' in the Gemfile. Then I run bundle install to get the gem.
As per official faker documentation, it said:
Note: if you are getting a uninitialized constant Faker::[some_class]
error, your version of the gem is behind the one documented here. To
make sure that your gem is the one documented here, change the line in
your Gemfile to:
gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main'
But the problem still persist with mine app, since I have done this and I am getting the error again, but only when try to run the migrations to heroku:
heroku run rails db:migrate db:seed
When I run the command locally I do not have the problem and migrations and seeds are executed.

Enhance assets:precompile on Heroku to add non fingerptinted filenames

In a Rails 4 app, I want precompiled assets to have copies of some assets to be available with a non fingerprinted filename. The app is deployed on Heroku.
I have the following rake task in place:
namespace :app do
task :nonfingerprint_assets => :environment do
manifests = Rails.application.config.assets[:precompile]
manifests = manifests.slice(1, manifests.length)
assets = Dir.glob(File.join(Rails.root, 'public/assets/**/*'))
regex = /(-{1}[a-z0-9]{32}*\.{1}){1}/
assets.each do |file|
next if File.directory?(file) || file !~ regex
source = file.split('/')
source.push(source.pop.gsub(regex, '.'))
non_digested = File.join(source)
if manifests.any? {|m|
if m.is_a? Regexp
m.match non_digested
else
m.ends_with?(non_digested)
end
}
puts "** copy source: #{file}"
puts "** copy destination: #{non_digested}"
FileUtils.cp(file, non_digested)
end
end
end
end
Rake::Task['assets:precompile'].enhance do
puts "Invoke nonfingerprint_assets"
Rake::Task['app:nonfingerprint_assets'].invoke
end
This rake task is working fine on my local machine even in production environment. It creates a file in public/assets/application.js which is the copy of the same file with a digest in its name.
When I deploy this code to heroku I get the following error message which I don't understand:
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_
locales = false to avoid this message.
Invoke nonfingerprint_assets
** copy source: /tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/public/assets/application-51d1660b66655782ab137ee98c789fdd.js
** copy destination: /tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/public/assets/application.js
rake aborted!
No such file or directory - /tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/public/assets/application.js
/tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/lib/tasks/production.rake:26:in `block (3 levels) in <top (required)>'
/tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/lib/tasks/production.rake:9:in `each'
/tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/lib/tasks/production.rake:9:in `block (2 levels) in <top (required)>'
/tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/lib/tasks/production.rake:45:in `block in <top (required)>'
Tasks: TOP => app:nonfingerprint_assets
(See full trace by running task with --trace)
Please help me to understand what's going on.

Resources