How do I test or debug my migration scripts in rails? - mysql

I am current developing a big migration script, where I need to do the following:
1- Create new columns in table_1
2- Copy the values form table_2 to the new columns created in table_1
3- Delete table_2
I developed the up and down methods for this migration. My problem is that step 2 is just not copying my data correctly I need to be able to test/debug my code as it runs.
If there is a way please let me know. I am currently using Rails 2.3.8 but If there is a way for Rails 3 or 3.1 also please let me know.
Thanks a lot

To debug your data migration, you can use the ruby-debug gem and insert a breakpoint in the migration code:
require 'ruby-debug'; debugger
Then run the rake task:
rake db:migrate:redo VERSION=whatever_version_number_your_migration_has
Quick ruby-debug tutorial here.

Manipulating data in migrations is not a good idea. They should be used only to edit database structure. But you can always rename existing table, it`s columns and add new columns to it or remove some of them. List of available actions that can be performed on database via migrations can be found at http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

Related

Domain classes and database are out of sync in Grails

I've this weird problem. I have a Grails app in which some database changelog files are missing. Therefore, the database has gone out of sync with the domain classes. I've done some changes in the domain classes. When I try to run the database migration plugin, it is creating a diff betweeb the current domain classes and the database and try to execute all the sql commands that has already been run which is causing error in executing the commands that I want to execute.
Is there is a solution for this problem?
If I understand your problem correctly, you can re-create all of the missing changelogs using dbm-generate-changelog. This will create changelogs based on the current data model. Then you can use dbm-changelog-sync to mark those changelogs as EXECUTED (which will populate the DATABASECHANGELOG table). Once the DATABASECHANGELOG table is in sync with the current data model, you can use dbm-gorm-diff to make sure you're not missing any other data model changes.
https://grails-plugins.github.io/grails-database-migration/1.4.0/ref/Maintenance%20Scripts/dbm-changelog-sync.html
NOTE: My answer assumes you're using Grails 2.x and Database Migration plugin 1.4.x, but I believe the process is similar in Grails 3.x with Database Migration Plugin 2.x or 3.x.

Django update models.py when database changes

I know this might be a sill question but I've been half an hour trying to figure this out and couldn't find anything :S
I have a django app and I significantly changed my database tables. I want to update my models.py file, but I tried the following commands and nothing happens.
syncdb, migrate, makemigrations...
I want to delete my previous models file and create a new one.
Thank you!
You got the workflow the wrong way around.
Django has an ORM that manages your database. If you want to make a change, you edit your models.py file. The migrations will automatically alter the database table to match your new model. It doesn't work the other way around: Django does not use database introspection to pick up manual changes in the database and edit your models file.
Now, there is a workaround, but it's not a long-term solution. In time you'll want to add custom functionality to your models, and you don't want to rewrite that functionality after each change. The introspection Django provides isn't perfect either, it's only meant as a tool to quickly start developing your application on top of a legacy database.
You can use manage.py inspectdb to generate the Django code for all existing tables. You can then copy the code for your specific model over to your models.py file. You should then delete the managed = False and db_table = ... options, remove any migrations, double-check the fields, and rerun makemigrations and migrate --fake-initial. This will get the database, your models, and your migrations back in sync, and then you'll be able to use the migrations framework for any additional changes.
Be sure to read the docs on migrations. That should leave you with a good understanding of how Django manages the database, and what the workflow is to make changes to your database.

Update Table if source modified

I know the title is not self-explaining but I don't know how to express this.
There is a site which keeps updating a small database opensource in many different formats, and I have a table in my DB that I want to be "linked" to that DB, so that if the DB gets updated the table gets cleared and repopulated with new data.
I am working with Rails and MySql as Database I don't know if this is possible, but any help would be appreciated, even a hint on what to google...
Thanks!
I assume you have access to codebase of both apps.
Suppose you have app A & B. You can create a rake task in A which will be triggered periodically via cronjob. The rake task will populate database of B through exposed api of B (accessible through secret api-key).
Hope you got the idea.

Rails Development with MySQL:Inserting data into tables

I downloaded a rails project.
Bundled it.
Run migrations.
Now the tables are empty. Which is the best way to fill it. irb or migrations itself. ?
You can use seeds.rb to fill the tables.
Create /db/seeds.rb
5.times do |i|
Model.create(name: "xxx")
end
And run rake db:seed.
rake db:seed
Look "Migrations and Seed Data" in rails guide.
http://guides.rubyonrails.org/migrations.html#migrations-and-seed-data
Rails has a seeds feature to populate the db with initial data, check out this guide, paragraph 8
Active Record Migrations
hope that helps
Neither of those is best way to do.You should be doing CRUD operations in order to keep your Rails project up and running.
By doing so,you should be able to create,edit,update and destroy the Records.
You have to read these Guides.You need it.

Trying to build rails 3.2 app over existing mysql db

I am trying to figure out how to build a rails app on top of an existing mysql db. I think the best method would just be to create a migration with the same layout as the existing db, but I am not quite sure how to do this, then connect it. I am aware of this post Building Ruby on Rails App with an existing Mysql db
but am still unsure; do I just do this but with the columns I need? Also the main answer to this question is saying that I should make my db a csv and then import it, does anyone have a tutorial or gem they recommend for that?
I have not done this exact task personally although when I modify my databases manually through my mysql client and create backup tables for example, they magically appear in my schema.rb file later down the road when I run some future migrations.
So the following post should help or at least point you in the right direction:
http://tianhsky.ueuo.com/blog/2012/02/20/generate-schema-rb-from-existing-database/
Before that, try to learn more about rails and it's conventions. Probably you'll need to adapt your database scheme.
Or you could start an application and then import the data, even by SQL or by CSV as you mentioned. Migrating data can be a tedious work, but a necessary one.
You can check this gem to see if it helps on your case, because it will depend on your actual schema.