Should I use migrations to create folders? - ruby-on-rails

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.

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

procfile got created twice in 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~.

Rails - where to put files that are not part of the deployment

I have to do a lot of conditioning of the data that will seed my rails database. This is a one-shot activity before deployment, never used after deployment, but I want to keep the programs I use for it within the projects configuration management (mainly for the sake of an audit trail for where the seed data came from).
Where is the canonical place in a Rails app for such support files that don't form part of the application?
Seed data should go in db/seed.rb. You can learn more about seed data in the docs.
The problem with adding all these items to your repository is that not only will it make the checked in code large, also you will have to clean the code each time after deploy.
I do not think such items should be checked in. Personally, I place all such items in public data, upload it for first deploy and then next deploy will no longer have this folder as the deployment using capistrano will not link to the data folder anymore.
This way the data can stay in the shared folder on the server should you need it again but not in your repository.

Ignoring .gitignore

My brother and I are collaborating on an app from two different computers -- one mac and one pc. I can't for the life of me get Postgres to work on his computer, and after a whole bunch of hours, I decided to just have his computer run sqlite3 for development (which is easy as pie), and basically have all the production stuff happen on my mac, while still allowing him to make functional changes from his pc. And merge them to github.
The trouble is, this involves having two different database.yml files, two different db/schema.rb files (I think), and different gemfiles, one with sqlite and the other with pg.
My thought was just to do all that on his computer and add those files to the gitignore file. But if THAT isn't ignored, then when I pull back to my mac, won't I be merging his incorrect configurations to my machine?
At any rate, that's why I was thinking of adding .gitignore to .gitignore. Will this work? Will it create universe-bending paradoxes? Is there a better way to do this that I don't know about?
Are those two schemas really different? They usually aren't.
If they aren't then just ignore config/database.yml and create contig/database_sqlite_example.yml and contig/database_ppostgresql_example.yml. That way, when someone clones repo, he can use SQLite or PostgreSQL by simply copying example file to database.yml (which will be ignored)
No, don't ignore .gitignore
I've always liked the idea of creating local branches for this. I actually go a little local branch crazy... but that's a different issue all together.
If you want to have a private little work bubble then keep your branch local only. You control what gets merged into master (or whatever your development branch is) and you can commit everything in your local branch to git for history sake.
If you want to share what you are working on then just share your branch out. But this way you can keep your environment setups isolated while sharing the local branch so that it is visible for collaboration.
There's plenty of good documentation on Git Branching and Sharing so I'll leave that to you instead of clouding the post with links that surely will get broken.
I'm not sure any of us "really want to" be working directly in the master anyway, especially in collaboration efforts such as yours.

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