Rake parallel:prepare changes schema - ruby-on-rails

I'm using parallel_tests on a Rails 3.2 app and we have two developers, everytime I do a 'rake parallel:prepare' I get this change and it's the opposite for the other programmer.
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at"
+ t.datetime "updated_at"
I really want rails to keep those constraints on and not try and remove them.

You or your colleague should reload schema (or all migrations) to database in development env.

Related

can not create database when using "rails db:migrate" command

I am newbie with Rails and trying to make a very simple Rails program. My problem is, when I made a Rails program, at first, I had a mistake and created a database named image by this command rails g scaffold image video title star:integer description:text then I made this command rails db:migrate, when I realized that problem, I deleted image folder and create another folder named Course by this command rails g scaffold image video title star:integer description:text. Everything seem ok.
But, my problem is, when I tried this command rails db:migrate, I saw that nothing happen.
When I accessed the website http://localhost:3000/courses/new to try to make a new thing, I saw that I can not make anything new.
So, I think that maybe my problem is, I made the image folder before the course folder.
How can I work with this problem ?
Could you please give me some ideas ? Thank you very much.
#Juanse Gimenez here is my schema.rb, sorry for my shortcomings
ActiveRecord::Schema.define(version: 2021_01_08_042733) do
create_table "courses", force: :cascade do |t|
t.string "image"
t.string "video"
t.string "title"
t.integer "star"
t.text "description"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "images", force: :cascade do |t|
t.string "video"
t.string "title"
t.integer "star"
t.text "description"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end
#borjagvo thank you for your comment, here is my new.html.erb <h1>New Course</h1> it is very simple, so I do not think that it is the reason ?

SchemaDumper generating different output on Mac & Linux

We're using activerecord 5.1.4 in our project.
When running migrations the SchemaDumper generates different output on different colleagues machines. Some are using Linux others using Mac.
We're all running the same postgres version 9.6
For example Macs generate:
create_table "appliances", force: cascade do |t|
t.string "brand"
t.integer "property_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Linux creates:
create_table "appliances", id: :serial, force: cascade do |t|
t.string "brand"
t.integer "property_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Linux output seems to have id: :serial and it doesn't pad columns to align.
It means we're constantly bouncing changes to schema.rb in version control
Why would the output be different?

random changes in schema.rb

I've noticed this for months and I just didn't have the time to deal with it until now. Whenever my CI server does an automatic git pull and restarts the rails servers, the schema.rb gets randomly modified. As the example shows below, the api_name column of a certain table got dropped. I dropped this column about 3 months ago. Same with transportation_charges. And very often, the spacing in this file changes: see created_at and updated_at.
It's especially annoying since on the next run, when my CI does an initial git pull, it complains about changes to schema.rb and stops execution until they get pushed or reverted. And it's not just the CI server. I've seen this on other developer machines as well. Did anyone run into this before?
diff --git a/db/schema.rb b/db/schema.rb
index 470d3bf..166e3ee 100644
--- a/db/schema.rb
+++ b/db/schema.rb
## -883,7 +883,6 ## ActiveRecord::Schema.define(version: 20170720211740) do
create_table "ups_package_service_options", force: :cascade do |t|
t.string "name"
- t.string "api_name"
t.string "type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
## -910,9 +909,8 ## ActiveRecord::Schema.define(version: 20170720211740) do
t.string "code"
t.string "name"
t.string "api_name"
- t.decimal "transportation_charges"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
t.boolean "domestic"
end
When you run a migration, the schema gets updated not just by the migration, but also the current database. I'm guessing some of your developers are using databases inconsistent with the schema. Then you'll get unexpected changes every time they run a migration.

Rails migration is always inserting whitespaces or changing the ordering of columns in the schema

There is a frustrating issue where my rails migrations update the schema with whitespaces and the position of the table's columns.
So most times when I run bundle exec rake db:migrate it will do one of the below scenarios. When I merge this into our main branch and other developers work off this, then their rails migration reverts tabs and position ordering.
We have noticed that all three developers on the team have the same issue when running a migration if I have been the last committer of the schema.
I just updated postgres to v9.2.4 that is the same as the other devs. Any ideas of what else I could try?
Examples
Below are git diffs to demonstrate what is happening.
Example of re-ordering the schema:
create_table "accounts", :force => true do |t|
t.integer "organisation_id"
- t.boolean "active", :default => false
t.text "notes"
+ t.boolean "active", :default => false
end
Example of adding tabs to the schema:
create_table "comments", :force => true do |t|
- t.integer "commentable_id", :default => 0
- t.string "commentable_type", :default => ""
+ t.integer "commentable_id", :default => 0
+ t.string "commentable_type", :default => ""
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
I built a gem to solve this problem.
It sorts columns, index names and foreign keys, removes excess whitespace and runs Rubocop for some formatting to unify the output of your schema.rb file.
https://github.com/jakeonrails/fix-db-schema-conflicts
After you add it to your Gemfile you just run rake db:migrate or rake db:schema:dump like normal.
Good news! Rails finally pushed out an update which solves exactly this. Starting from Rails v5.1.0, the default (and only option) is to remove any extra spaces between the columns.
You just should ignore schema.rb from your version control. It changes differently depending on th order of migrations and who generated them. You only need the migrations to generate the appropiated schema for each developer involved in the project.
Regards.

rails: table already exists on new git branch

I'm trying to play around with different messaging gems that all use devise. Therefore, after installing Devise on master branch of git, I checked out a new branch to try one gem "rails messaging" https://github.com/frodefi/rails-messaging, and when I couldn't get it to work, I committed the changes to that branch, and then I went back to master and checked out a new branch to try another gem "mailboxer" https://github.com/ging/mailboxer/tree/master/lib. In the gemfile of this new branch there was no trace of the gems from the first branch, however when I tried rake db:migrate for this second branch after installing the gems, I got this error message, which seemed to suggest that a table from the first branch was interfering with the rake of the second branch, unless by doing
rails g mailboxer:install
It automatically runs the rake db:migrate. However, the README says that I have to run rake db:migrate, so I'm not sure...
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "conversations" already exists: CREATE TABLE "conversations" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "subject" varchar(255) DEFAULT '', "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL
)
Can anyone suggest how I can work around this? I don't really know what commands to run to figure this out.
This is the scheme.rb file. Based on the index "messaging users" i'm guessing it's the setup created by the rails messaging gem (on the first branch) but I'm not totally sure. I looked at the source code on git but got kinda lost because I'm not very experienced.
ActiveRecord::Schema.define(:version => 20120302041333) do
create_table "conversations", :force => true do |t|
t.string "subject", :default => ""
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "messaging_users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "messaging_users", ["email"], :name => "index_messaging_users_on_email", :unique => true
add_index "messaging_users", ["reset_password_token"], :name => "index_messaging_users_on_reset_password_token", :unique => true
create_table "notifications", :force => true do |t|
t.string "type"
t.text "body"
t.string "subject", :default => ""
t.integer "sender_id"
t.string "sender_type"
t.integer "conversation_id"
t.boolean "draft", :default => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.integer "notified_object_id"
t.string "notified_object_type"
t.string "notification_code"
end
add_index "notifications", ["conversation_id"], :name => "index_notifications_on_conversation_id"
create_table "receipts", :force => true do |t|
t.integer "receiver_id"
t.string "receiver_type"
t.integer "notification_id", :null => false
t.boolean "read", :default => false
t.boolean "trashed", :default => false
t.boolean "deleted", :default => false
t.string "mailbox_type", :limit => 25
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "receipts", ["notification_id"], :name => "index_receipts_on_notification_id"
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
The issue here is that while your code, schemas, gems, and migrations are stored in git, the database itself is not. So when you switch between branches, the database still retains its state.
These solutions will all work:
drop and re-seed your database
rake db:reset
migrate down (while on the old branch) to undo the migration. I'm not sure if the gem installed a migration in db/migrate or ran a migration directly from the gem, so this isn't as easy, but is better for data integrity if you don't want to wipe your data.
reload the schema you have stored in git (this has the same effects as the first one)
rake db:schema:load
use an sqlite database for now - while not appropriate for production, if you check in the database to git, it will behave as you're wishing your current database behaved. I should warn you though that large files like this will be a pain. Better to use one of the other approaches.
Yeah switching branches with different migration levels is a mess. There is no 'one-liner' to do this properly. There are four things that can be out of sync. You'll need to:
1) Determine what migrations your branch has:
(look at the contents of db/migrations)
2) Determine what your app thinks your database looks like:
(look at db/schema.rb)
3) Determine what migrations your database thinks it has by looking at the schema_migrations table:
~> rails db console
(opens up your db console, in my case mysql...)
mysql> SELECT * FROM schema_migrations; # don't forget the ';' at the end!
(outputs big huge list of migration timestamps)
4) Look at the actual tables to see if the migrations have been applied.
mysql> describe table_name;
(outputs the column names, see if they have been updated per the migration)
5) Now the part that requires thinking. There are a couple possible scenarios. I can only speak to the one I ran into:
Your migration files (1), your schema.rb(2) and your actual tables (4) match, but your schema_migrations table (3) is missing the migration timestamp. This was my issue.
If you don't care about the data: rake db:reset. It will trash your whole database and recreate the structure from scratch
Please suggest other scenarios with solutions..

Resources