0

Brand new to DataMapper and wondering if I can use DataMapper.auto_updgrade! to change a column name of an existing column in a SQLite database?

If I have the following in a song.rb

require 'date'
require 'dm-core'
require 'dm-migrations'

DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")

class Song
  include DataMapper::Resource
  property :id, Serial
  property :title, String
  property :music_by, String
  property :lryics_by, String
  property :lyrics, Text
  property :length, Integer
  property :released_on, Date

  def released_on=date
    super Date.strptime(date, '%m/%d/%Y')
  end

end

DataMapper.finalize

After a Song.auto_migrate!

2.0.0-p598 :004 > Song.new
 => #<Song @id=nil @title=nil @music_by=nil @lyrics_by=nil @lyrics=nil @length=nil @released_on=nil> 

Is it possible to change the

property :lryics_by, String

to

property :words_by, String

and have the database column name change, but keep any existing data?

I've tried with Song.auto_upgrade! and it adds an empty new column and leaves the original column and data in place. On the other hand, my Song.new object looks right.

2.0.0-p598 :004 > Song.new
 => #<Song @id=nil @title=nil @music_by=nil @words_by=nil @lyrics=nil @length=nil @released_on=nil> 

It seems like I need a migration in the way that ActiveRecord (I've played around a little with that ORM) handles migrations. Or I would need to change the column name with SQL or an app or the Firefox SQLlite plugin.

UPDATE: I'm wondering now if this is more a SQLite thing than a DataMapper thing. When I went to delete a column in Firefox's SQLite Manager plugin I got this message:

This is a potentially dangerous operation. SQLite does not support statements that can alter a column in a table. Here, we attempt to reconstruct the new CREATE SQL statement by looking at the pragma table_info which does not contain complete information about the structure of the existing table.

Do you still want to proceed?

kaplan
  • 4,109
  • 6
  • 30
  • 35
  • Found this on StackOverflow to make a change in SQLite: http://stackoverflow.com/a/805508/740044 – kaplan Jan 15 '15 at 20:58

1 Answers1

0

dm-migrations can do this but not for SQLite, mostly because SQLite doesn't support renaming columns, as it has a limited ALTER TABLE implementation (http://www.sqlite.org/lang_altertable.html)

There's a rename_column migration but as you can see from the dm-migrations TableModifier class (https://github.com/datamapper/dm-migrations/blob/master/lib/dm-migrations/sql/table_modifier.rb), it's not available for SQLite:

def rename_column(name, new_name, opts = {})
    # raise NotImplemented for SQLite3
    @statements << @adapter.rename_column_type_statement(table_name, name, new_name)
end
Bogdan
  • 101
  • 5