Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
DataMapper.auto_upgrade! Question
# Departamento Version 1.0
class Departamento
include DataMapper::Resource
property :id, Serial
property :nombre, String, :index => true
end
# Then I run rackup with DataMapper.auto_upgrade!, which creates the tables with the structure above.
# What if I change something in the structure, eg.
# Departamento Version 1.0
class Departamento
include DataMapper::Resource
property :id, Serial
property :nombre, String, :index => true, :required => true
end
# Then I run rackup again. Will DataMapper.auto_upgrade!, run the proper SQL statements to change the column?

dkubb commented May 31, 2011

The DataMapper.auto_upgrade! call is currently additive only. It adds new tables, columns and indexes but does not change anything pre-existing. In the future it may try to identify "safe" changes it can make without causing an exception and perform those.

Please note though, this might not be a "safe" change. If the table has NULL values for any columns it may not always be safe to change it to a NOT NULL column without providing a default value. MySQL will probably silently coerce those fields to an empty string but other databases may explode if you try to do this.

One idea I had was to inspect the data with queries before event starting on a conversion to make sure data won't be silently truncated or information lost. In this case we'd do a SELECT COUNT(*) FROM departmentos WHERE nombre IS NULL and if it returned 0 we'd apply the change, otherwise we'd raise an exception telling you to either supply a default value or fix the data beforehand.

The typical use case for developing locally is to use DataMapper.auto_migrate! in combination with a seed script, so you can just blow away the local database then repopulate it using the seed script. You never really want to use that method or DataMapper.auto_upgrate! in production though; you would write a dm-migrations migration script and apply it when you deploy your app.

Owner

etagwerker commented Jun 8, 2011

Great, this makes sense. It just wasn't clear enough in the getting started page.

I like your idea about inspecting the data and then allowing or not an auto_upgrade!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment