From 14ddf8503ac4c63d48229563cf0f2b8d8a4256cf Mon Sep 17 00:00:00 2001 From: "nicholas a. evans" Date: Mon, 20 Sep 2010 20:25:08 -0400 Subject: [PATCH] a simple gemification of the table_migrator plugin --- .gitignore | 1 + init.rb | 1 - lib/table_migration.rb | 3 +++ lib/table_migrator.rb | 3 +++ lib/table_migrator/base.rb | 5 ++++ lib/table_migrator/change_table_strategy.rb | 3 +++ lib/table_migrator/copy_engine.rb | 2 ++ lib/table_migrator/raw_sql_strategy.rb | 2 ++ table_migrator.gemspec | 26 +++++++++++++++++++++ 9 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 .gitignore delete mode 100644 init.rb create mode 100644 table_migrator.gemspec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c111b33 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.gem diff --git a/init.rb b/init.rb deleted file mode 100644 index 3c19a74..0000000 --- a/init.rb +++ /dev/null @@ -1 +0,0 @@ -# Include hook code here diff --git a/lib/table_migration.rb b/lib/table_migration.rb index 208036e..017776a 100644 --- a/lib/table_migration.rb +++ b/lib/table_migration.rb @@ -1,3 +1,6 @@ +require 'active_record' +require 'table_migrator/base' + class TableMigration < ActiveRecord::Migration class << self attr_reader :table_migrator diff --git a/lib/table_migrator.rb b/lib/table_migrator.rb index d3a419b..1222878 100644 --- a/lib/table_migrator.rb +++ b/lib/table_migrator.rb @@ -1,3 +1,6 @@ +require 'table_migration' +require 'table_migrator/base' + module TableMigrator extend self diff --git a/lib/table_migrator/base.rb b/lib/table_migrator/base.rb index e68f7f1..3354c18 100644 --- a/lib/table_migrator/base.rb +++ b/lib/table_migrator/base.rb @@ -1,3 +1,8 @@ +require 'active_record' +require 'table_migrator/copy_engine' +require 'table_migrator/change_table_strategy' +require 'table_migrator/raw_sql_strategy' + module TableMigrator class Base diff --git a/lib/table_migrator/change_table_strategy.rb b/lib/table_migrator/change_table_strategy.rb index cf6556d..e74f615 100644 --- a/lib/table_migrator/change_table_strategy.rb +++ b/lib/table_migrator/change_table_strategy.rb @@ -1,3 +1,6 @@ +require 'active_record' +require 'table_migrator/copy_strategy' + module TableMigrator class ChangeTableStrategy < CopyStrategy diff --git a/lib/table_migrator/copy_engine.rb b/lib/table_migrator/copy_engine.rb index 3df2391..9ec9e29 100644 --- a/lib/table_migrator/copy_engine.rb +++ b/lib/table_migrator/copy_engine.rb @@ -1,3 +1,5 @@ +require 'active_record' + module TableMigrator class CopyEngine diff --git a/lib/table_migrator/raw_sql_strategy.rb b/lib/table_migrator/raw_sql_strategy.rb index 8285068..d0be0ce 100644 --- a/lib/table_migrator/raw_sql_strategy.rb +++ b/lib/table_migrator/raw_sql_strategy.rb @@ -1,3 +1,5 @@ +require 'table_migrator/copy_strategy' + module TableMigrator class RawSqlStrategy < CopyStrategy attr_accessor :base_copy_query, :schema_changes diff --git a/table_migrator.gemspec b/table_migrator.gemspec new file mode 100644 index 0000000..a53bef6 --- /dev/null +++ b/table_migrator.gemspec @@ -0,0 +1,26 @@ +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = "table_migrator" + s.version = '0.0.1.dev' + s.platform = Gem::Platform::RUBY + s.authors = ["Matt Freels", "Rohith Ravi", "Rick Olson",] + s.email = ["matt@freels.name"] + s.homepage = "http://github.com/freels/table_migrator" + s.summary = "Zero-downtime table migration in MySQL" + s.description = <<-EOF + TableMigrator is a method for altering large MySQL tables while incurring + as little downtime as possible. First, we create a new table like the + original one, and then apply one or more ALTER TABLE statements to the + unused, empty table. Second we copy all rows from the original table into + the new one. All this time, reads and writes are going to the original + table, so the two tables are not consistent. Finally, we acquire a write + lock on the original table before copying over all new/changed rows, and + swapping in the new table. + EOF + + s.add_dependency "activerecord", "~>2.3.5" + + s.files = Dir.glob("lib/**/*") +%w[README.md] + s.require_path = 'lib' +end