Skip to content

Instantly share code, notes, and snippets.

@wbailey
Created February 8, 2011 10:47
Show Gist options
  • Save wbailey/816265 to your computer and use it in GitHub Desktop.
Save wbailey/816265 to your computer and use it in GitHub Desktop.
For when you want migrations that are just straight sql files. This is useful when creating databases for working with partner data and matching your data to it another database that is for a rails app.
drop table if exists schema_migrations;
create table schema_migrations (
version varchar(255) primary key
);
insert into schema_migrations (version) values ('0');
drop table loaded_providers;
update schema_migrations set version = '0';
create table loaded_providers (
service_id int
, name varchar(255)
, degree_1 varchar(255)
, degree_2 varchar(255)
, sub_name varchar(255)
, address1 varchar(255)
, address2 varchar(255)
, city varchar(255)
, code varchar(255)
, zip_code varchar(255)
, phone_1 varchar(255)
, contact_name varchar(255)
, contact_email varchar(255)
, service_weblink_1 varchar(255)
, name_random varchar(255)
, information varchar(255)
, short_name varchar(255)
, range_start varchar(255)
, range_end varchar(255)
, category_1 varchar(255)
, category_2 varchar(255)
, category_3 varchar(255)
, category_4 varchar(255)
, category_5 varchar(255)
, category_6 varchar(255)
, tag_1 varchar(255)
, tag_2 varchar(255)
, tag_3 varchar(255)
, tag_4 varchar(255)
, tag_5 varchar(255)
, tag_6 varchar(255)
, tag_7 varchar(255)
, tag_8 varchar(255)
, tag_9 varchar(255)
, tag_10 varchar(255)
, tag_11 varchar(255)
, photo_url varchar(255)
, id int auto_increment primary key
);
load data infile '/Users/wesbailey/autism_data/loaded_providers.csv' INTO TABLE providers FIELDS TERMINATED BY ',' optionally enclosed by '"' LINES TERMINATED BY '\n';"';
update schema_migrations set version = '1';
drop table if exists categories;
UPDATE schema_migrations SET version = '1';
create table categories (
id int auto_increment primary key
, name varchar(255) not null
, mat_id int
);
insert into categories (name)
select distinct category_1 from loaded_providers
union
select distinct category_2 from loaded_providers
union
select distinct category_3 from loaded_providers
union
select distinct category_4 from loaded_providers
union
select distinct category_5 from loaded_providers
union
select distinct category_6 from loaded_providers
order by 1
;
delete from categories where name = '';
update schema_migrations set version = '2';
#!/usr/bin/ruby -w
#
# usage: ruby migrate.rb [up|down] [version]
require 'yaml'
require 'ostruct'
module Migrate
def index
File.basename(self).match(/^\d{2}/)[0].to_i
end
def sort_by file, order
order == 'up' ? self.index <=> file.index : file.index <=> self.index
end
def migrate user, password, database, host = 'localhost'
migration = "mysql -u #{user}#{password.nil? ? '' : '-p' + password} #{database} -h #{host} < #{self}"
p migration
raise SystemError unless system migration
end
end
order = ARGV[0] || 'down'
path = File.dirname __FILE__
env = ENV['DATABASE_ENV'] || 'development'
yml = ENV['DATABASE_YAML'] || File.join(path, 'database.yml')
raise ArgumentError unless %w{up down}.include? order
raise ArgumentError, 'must define database environment' if env.empty?
settings = OpenStruct.new YAML::load_file(yml)[env]
if order == 'down'
version = ARGV[1].to_i
else
begin
sql = "select version from schema_migrations"
cmd = "echo #{sql}| mysql -Nu #{settings.user} #{settings.database}"
version = IO.popen(cmd) {|p| p.read}.chomp.to_i
rescue Exception
raise SystemError, 'please run the migration that creates the schema_migrations table'
end
end
Dir.glob(File.join(path, "*.#{order}.sql")).select do |f|
f.extend(Migrate).index > version
end.sort {|a,b| a.sort_by(b, order)}.each do |file|
file.migrate settings.user, settings.password, settings.database
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment