procfile got created twice in rails - ruby-on-rails

I created a Procfile for my rails app. When I commit the change to github I can see that 2 Procfiles are there. One is the one I created Procfile and the other Procfile~(This one was hidden). I can see that the contents are same when I check on remote repository. Is it normal or should I delete one?

You just need one Procfile. Usually a file finished with a tilde ~ is a backup copy made by your editor.
The systems relying on the Procfile will just look at the one with that name, they won't care about Procfile~, so it's up to you if you want to delete it or not.
A common thing to do is to add the following in your .gitignore:
*~

On many unix based systems, a tilde is added to a filename when a temporary save is made in order to avoid data loss and concurrent editings of a single file. If the contents are the same, you can probably delete Procfile~.

Related

Rails 5 Upgrade: routes.rb file cleared out

I am upgrading a Rails app from 4.2.x to 5.0.x. After I updated all my Rails-related gems in Gemfile, I ran the rails task for updating all my files to conform to the newest version, as per the upgrade guide:
rails app:update
There were many conflicts in this command, so I pressed a to accept all conflicts and then review them manually before committing them. My main concern is what happened to the config/routes.rb file. Basically, the entire contents of the file, save for the Rails.application.routes.draw block and a single comment about the DSL added to the end of the file, were kept. All routes that have been added to the app over the years were cleared out, not to be found in any other file.
This issue doesn't block me, I'll simply checkout the file to bring it back to its former state. However, what especially concerns me is that an essential file was cleared out in what appears to be a normal situation for upgrading a Rails version. I'd like to see if anyone else has run into this issue, whether this is expected, what's going on. Comments from Rails maintainers are welcome. Thank you.
The app:update task is just a slightly modified version of the task that creates a new Rails application. It writes out files under config/ and bin/ based on the templates from the new Rails version. If you selected a to accept all conflicts, then that simply means that Rails is going to overwrite any differing files with its own copy without asking you. This is normal and expected. If you don't want that behavior, then don't press a.
config/routes.rb is almost certainly the one file under config/ that will differ almost completely from the empty boilerplate file.
Realistically, Rails can't be expected to parse your custom changes and merge them with a new template.
For what it's worth, I like to start out doing exactly what you did, with a clean working directory just let the app:update task overwrite anything it wants to, and then go through all the changes with a side-by-side, SCM-aware diff tool like vim-fugitive to stage or discard the differences.
Well, just don't accept all conflicts, take a look in each one of it. Making version upgrades of 1 level of magnitude may change a lot of things and the process takes a while.
First take a look on the changes in the framework and do the process carefully. You may be breaking a lot more stuff that you are not noticing:
http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-4-2-to-rails-5-0

rails generate model(s) through configuration file

There are possibilities to generate models (and migration) through a configuration file? I have so much table and i want create them (all the time) with a simple step.
I'm holding all my migration commands in a bash shell file. This allows me to "recycle" certain parts, and prevents me from making typo's... Once a command has been run, I outcomment it, but keep it present in the file

Why is config/database.yml included in the .gitignore file by default for my Rails app?

Why would config/database.yml be included in the .gitignore file by default? This small item cost me an hour or two.
The way I stumbled into this trouble:
I thought I was confused about how git handled reset and revert.
I had been experimenting with code changes that affected config/database.yml
I didn't like the result, so I abandoned the changes.
It was odd, because it seemed I could not checkout an unedited version of config/database.yml
I then spent way too much time trying to figure out if I was misunderstanding git's reset and merge commands.
Turns out config/database.yml was included in the .gitignore file by default.
Not sure why. Cost me an hour or so of fiddling with reset and merge options, when it turns out I wasn't even tracking the file. As I write this, I realize I am not entirely sure when .gitignore was written/modified to include config/database.yml
Is there some reason it should be there?
That file usually won't contain much interesting content for tracking in a version control system.
But, it will often contain passwords for connecting to a database server, which should not be made public.
It will also often contain information which needs to differ between different working copies, different user or database names for a database server, different paths to a SQLite file. These differences would make it impossible to track this file in the version control system.
If you are seeing a file for which 'git reset' and 'git revert' don't seem to have any effect, you may find the file is listed in the .gitignore file in your project root.

Should I use migrations to create folders?

I created a feature that requires two folders to work correctly. These folders didn't existed in the rails project so I had to create them. Now I need to push this functionality so that the other developers can use it as well.
What do you think is the best practice:
Create the folders locally and leave a note somewhere explaining that these folders needs to be created
Create a migration that will create these folders
Create the folders if they don't exist on execution
So what are the best practices regarding this?
Two choices
1. Put them under version control.
When the developers next check out the folders will get created. How you add them to VC depends on the nature of the tool you're using but many tools require that the directories contain a file (possibly hidden).
That works great if the folders are in the source tree. You also have the potential on a Unix-like os to soft-link from within the source tree to a well known location outside of the source tree. Depending on the VC system (we use Mercurial), you can make the soft link a versioned item.
2. Create them as your process starts
If you're going to do this, you might as well go the extra mile and make their location a configurable option. On start up, read the config file, make the folders and continue.
I would think that as part of the dev builds there must be rake tasks that get invoked to do certain activities; it would be appropriate to either include such activity in a rake tasks where it fits most appropriately.
It should check for folders and if it doesn't exists; just create them. Migrations for creating folder is really not the right way I guess.
I'd include this in a bootstrap rake task.
This is a good question.
For me I would put the folder creation in the migration if and only if the folder was required as part of that migration. So for instance you were adding or updating the table dealing with attachments (such as with attachment_fu etc), then I would consider putting the directory creation/deletion in there.
However creating directories is an environment specific thing (different on Windows vs *nix etc), especially where permissions are involved. Therefore it would make sense to go where these issues are handled in an environment-abstracted way. Perhaps in the deployment script (if using Capistrano) or as other people have suggested in a rake task.
I'll add them to the repository. Maybe if it holds temporary data I'll ignore the contents.

How to efficiently handle the changes between production and development when updating from the repository

I have inherited a project with a local development environment that has code specific to that machine, and which is different for the production server. Even though the majority of it is contained in constants and the rest is in the tests, every time I commit from development and update in production I'm going to have to make the same changes in production. Fortunately this is an internal tool with low volume.
I guess I could write a script to automate it but I'm hoping there's a better solution. Anyone else solved this problem?
These questions are similar but not asking the same thing, just so you know I looked:
(1) make changes to a production database
(2) transferring changes from dev to prod
Edit: Nelson LaQuet put me on what I believe is the right track, which led me to Configuring Rails Applications. However, I am unsure how to reference my FormController < ApplicationController constants, such as MyExternalCodeDir, in config/environments/production.rb and config/environments/development.rb.
Also I do not want to be required to change every reference to MyExternalCodeDir to something like config.MyExternalCodeDir.
You abstract all environment settings (database connection/pathing/URIs) into a single file. Let's call it "config.ini"
Then you simply commit a "template" called "config.ini.template" that contains the structure of the config file clearly documenting what is expected at each value - and sensable defaults. You then commit this file.
After you do that, delete the current config.ini file that is specific to your location, and add it to svn:ignore. Now, when you copy and paste config.ini.template to config.ini, and change your settings, it is not going to be committed to the repository.
It adds an extra step per deployment, but must be done only once (unless you add/remove config options). This is the best and most standard way of accomplishing what you want.
I would move the constants' values that are environment specific in a configuration file, which would make it easier to handle. I would also keep the code in just one repository in the version-control system and manage the build outputs in two separate repositories: one for the test environment and one for production. That way I can manage my code base however I choose, and when I want to deploy I'll first commit to test, and then merge from test to production and at that point just diff the configuration file and keep the correct configuration for the production environment.

Resources