Permalink
Browse files

Merge pull request #64 from zawaideh/master

Rails generator for AR migrations
  • Loading branch information...
2 parents 22cc752 + 391b51b commit 7b91b25a4ba23d12a93af96c088d952526c10916 @assaf assaf committed Jul 29, 2011
View
@@ -6,3 +6,4 @@ test/myapp/log/
html
*.gem
.yardoc
+.DS_Store
View
@@ -23,7 +23,9 @@ http://farm3.static.flickr.com/2540/4099665871_497f274f68_o.jpg
== Rails 3 configuration
gem 'vanity' # in Gemfile, either outside of a group or inside of a bundler group
-
+ rails generate vanity
+ rake db:migrate
+
And:
class ApplicationController < ActionController::Base
@@ -0,0 +1,53 @@
+class VanityMigration < ActiveRecord::Migration
+ def self.up
+ create_table :vanity_metrics do |t|
+ t.string :metric_id
+ t.datetime :updated_at
+ end
+ add_index :vanity_metrics, [:metric_id]
+
+ create_table :vanity_metric_values do |t|
+ t.integer :vanity_metric_id
+ t.integer :index
+ t.integer :value
+ t.string :date
+ end
+ add_index :vanity_metric_values, [:vanity_metric_id]
+
+ create_table :vanity_experiments do |t|
+ t.string :experiment_id
+ t.integer :outcome
+ t.datetime :created_at
+ t.datetime :completed_at
+ end
+ add_index :vanity_experiments, [:experiment_id]
+
+ create_table :vanity_conversions do |t|
+ t.integer :vanity_experiment_id
+ t.integer :alternative
+ t.integer :conversions
+ end
+ add_index :vanity_conversions, [:vanity_experiment_id, :alternative], :name => "by_experiment_id_and_alternative"
+
+ create_table :vanity_participants do |t|
+ t.string :experiment_id
+ t.string :identity
+ t.integer :shown
+ t.integer :seen
+ t.integer :converted
+ end
+ add_index :vanity_participants, [:experiment_id]
+ add_index :vanity_participants, [:experiment_id, :identity], :name => "by_experiment_id_and_identity"
+ add_index :vanity_participants, [:experiment_id, :shown], :name => "by_experiment_id_and_shown"
+ add_index :vanity_participants, [:experiment_id, :seen], :name => "by_experiment_id_and_seen"
+ add_index :vanity_participants, [:experiment_id, :converted], :name => "by_experiment_id_and_converted"
+ end
+
+ def self.down
+ drop_table :vanity_metrics
+ drop_table :vanity_metric_values
+ drop_table :vanity_experiments
+ drop_table :vanity_conversions
+ drop_table :vanity_participants
+ end
+end
@@ -0,0 +1,15 @@
+require 'rails/generators'
+require 'rails/generators/migration'
+
+class VanityGenerator < Rails::Generators::Base
+ include Rails::Generators::Migration
+ source_root File.expand_path('../templates', __FILE__)
+
+ def self.next_migration_number(path)
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
+ end
+
+ def create_model_file
+ migration_template "vanity_migration.rb", "db/migrate/vanity_migration.rb"
+ end
+end
@@ -12,61 +12,6 @@ def active_record_connection(spec)
class ActiveRecordAdapter < AbstractAdapter
# Base model, stores connection and defines schema
class VanityRecord < ActiveRecord::Base
- def self.define_schema
- # Create a schema table to store the schema version
- unless connection.tables.include?('vanity_schema')
- connection.create_table :vanity_schema do |t|
- t.integer :version
- end
- end
-
- # Migrate
- unless VanitySchema.find_by_version(1)
- connection.create_table :vanity_metrics do |t|
- t.string :metric_id
- t.datetime :updated_at
- end
- connection.add_index :vanity_metrics, [:metric_id]
-
- connection.create_table :vanity_metric_values do |t|
- t.integer :vanity_metric_id
- t.integer :index
- t.integer :value
- t.string :date
- end
- connection.add_index :vanity_metric_values, [:vanity_metric_id]
-
- connection.create_table :vanity_experiments do |t|
- t.string :experiment_id
- t.integer :outcome
- t.datetime :created_at
- t.datetime :completed_at
- end
- connection.add_index :vanity_experiments, [:experiment_id]
-
- connection.create_table :vanity_conversions do |t|
- t.integer :vanity_experiment_id
- t.integer :alternative
- t.integer :conversions
- end
- connection.add_index :vanity_conversions, [:vanity_experiment_id, :alternative], :name => "by_experiment_id_and_alternative"
-
- connection.create_table :vanity_participants do |t|
- t.string :experiment_id
- t.string :identity
- t.integer :shown
- t.integer :seen
- t.integer :converted
- end
- connection.add_index :vanity_participants, [:experiment_id]
- connection.add_index :vanity_participants, [:experiment_id, :identity], :name => "by_experiment_id_and_identity"
- connection.add_index :vanity_participants, [:experiment_id, :shown], :name => "by_experiment_id_and_shown"
- connection.add_index :vanity_participants, [:experiment_id, :seen], :name => "by_experiment_id_and_seen"
- connection.add_index :vanity_participants, [:experiment_id, :converted], :name => "by_experiment_id_and_converted"
-
- VanitySchema.create(:version => 1)
- end
- end
end
# Schema model
@@ -142,7 +87,6 @@ def initialize(options)
options[:adapter] = options[:active_record_adapter] if options[:active_record_adapter]
VanityRecord.establish_connection(options)
- VanityRecord.define_schema
end
def active?

0 comments on commit 7b91b25

Please sign in to comment.