Skip to content

Instantly share code, notes, and snippets.

@dkubb
Created October 11, 2011 06:48
Show Gist options
  • Save dkubb/1277447 to your computer and use it in GitHub Desktop.
Save dkubb/1277447 to your computer and use it in GitHub Desktop.
Example data migration using Veritas
class MyMigration < Veritas::Migration
def call
# Transformations
# ---------------
# 1) remove all columns except id, name and email
# 2) rename user_id to id
# 3) add a boolean active column, setting the default to false
# 4) add a constraint on name where it must be between 1 and 100 characters
schema[:users] = schema[:users].
project([ :user_id, :name, :email ]).
rename(:user_id => :id).
extend { |r| r.add([ :active, Boolean ], false) }.
restrict { {r| r.name.length.gte(1).and(r.name.length.lte(100)) }
# This generates the following DDL statement:
# ALTER TABLE `users`
# DROP COLUMN `notes`,
# RENAME COLUMN `user_id` TO `id`,
# ADD COLUMN `active` BOOLEAN NOT NULL DEFAULT FALSE,
# CHANGE COLUMN `name` VARCHAR(100) NOT NULL CHECK (CHAR_LENGTH(`name`) >= 1)
end
end
@dkubb
Copy link
Author

dkubb commented Oct 14, 2011

@postmodern I think that would be great for a higher level API. I'm thinking about a DSL that's block-based, where you specify the table, and then inside a block specify the transformations you want to apply. I will probably be thinking in terms of a few basic classes of operations:

  • Adding
  • Removing
  • Changing
  • Renaming

I would think that tables, columns and indexes should have one method for each of these actions. Even if the actual operations in the DB consist of multiple DDL statements, I would rather work at a higher level and just think in terms of the operation. I really don't want to make something that isn't an abstraction at all, and just a 1:1 mapping to some DDL statements ... if that's the case we'd be better off just using straight SQL.

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