citrusbyte-schemer 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +22 -0
- data/lib/schemer/migrator.rb +18 -0
- data/lib/schemer.rb +1 -1
- data/tasks/schemer.rake +22 -0
- data/test/schemer_test.rb +19 -1
- metadata +3 -1
data/README.markdown
CHANGED
|
@@ -50,6 +50,28 @@ Works just fine, and you can drop it at any time!
|
|
|
50
50
|
|
|
51
51
|
Removes the `user_id` column the next time the `Widget` class is loaded.
|
|
52
52
|
|
|
53
|
+
Feeling more confident and ready to make that big leap to migrations? Just run:
|
|
54
|
+
|
|
55
|
+
rake schemer:migration
|
|
56
|
+
|
|
57
|
+
To get:
|
|
58
|
+
|
|
59
|
+
Migration from schema declarations in User, Widget
|
|
60
|
+
|
|
61
|
+
create_table :users do |t|
|
|
62
|
+
t.string :username
|
|
63
|
+
t.string :password
|
|
64
|
+
t.string :email
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
create_table :widgets do |t|
|
|
68
|
+
t.string :name
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
Then you can paste the output into a migration file, setup your columns (don't
|
|
72
|
+
forget your indexes!) and get rid of your `schema` calls in the listed classes
|
|
73
|
+
and you're on your way to the big leagues!
|
|
74
|
+
|
|
53
75
|
**NOTE:** All columns are created as string columns.
|
|
54
76
|
|
|
55
77
|
Installation
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Schemer
|
|
2
|
+
class Migrator
|
|
3
|
+
class << self
|
|
4
|
+
# Outputs the Rails migration for the schema defined in the given class.
|
|
5
|
+
#
|
|
6
|
+
# Outputs an empty string if no schema is defined on the class.
|
|
7
|
+
def migration(klass)
|
|
8
|
+
return nil unless klass.respond_to?(:schema_columns)
|
|
9
|
+
|
|
10
|
+
"create_table :#{klass.table_name} do |t|\n" +
|
|
11
|
+
(klass.schema_columns - klass.protected_columns).collect do |column|
|
|
12
|
+
" t.string :#{column}"
|
|
13
|
+
end.join("\n") +
|
|
14
|
+
"\nend"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
data/lib/schemer.rb
CHANGED
|
@@ -35,7 +35,7 @@ module Schemer
|
|
|
35
35
|
create_table unless table_exists?
|
|
36
36
|
(schema_columns - column_names).each { |column| ActiveRecord::Migration.add_column(table_name, column, :string) }
|
|
37
37
|
(column_names - protected_columns - schema_columns).each { |column| ActiveRecord::Migration.remove_column(table_name, column) }
|
|
38
|
-
end
|
|
38
|
+
end
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
|
data/tasks/schemer.rake
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
namespace :schemer do
|
|
2
|
+
desc "Outputs the basis of a Rails migration from your schema declarations"
|
|
3
|
+
task :migration => :environment do
|
|
4
|
+
klasses = []
|
|
5
|
+
|
|
6
|
+
Dir.glob(File.join(Rails.root, "app", "models", "**.rb")).each do |file|
|
|
7
|
+
lineno = 0
|
|
8
|
+
result = File.readlines(file).each do |line|
|
|
9
|
+
lineno += 1
|
|
10
|
+
next unless line =~ /^class (.*) \<.*$/
|
|
11
|
+
klasses << $1.constantize
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
migration = klasses.collect do |klass|
|
|
16
|
+
Schemer::Migrator.migration(klass)
|
|
17
|
+
end.join("\n\n")
|
|
18
|
+
|
|
19
|
+
puts "\nMigration from schema declarations in #{klasses.join(', ')}"
|
|
20
|
+
puts "\n#{migration}\n\n"
|
|
21
|
+
end
|
|
22
|
+
end
|
data/test/schemer_test.rb
CHANGED
|
@@ -2,12 +2,15 @@ require 'rubygems'
|
|
|
2
2
|
require 'contest'
|
|
3
3
|
require 'override'
|
|
4
4
|
require File.join(File.dirname(__FILE__), '..', 'lib', 'schemer')
|
|
5
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'schemer', 'migrator')
|
|
5
6
|
|
|
6
7
|
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'test/schemer_test.db'
|
|
7
8
|
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
|
|
8
9
|
|
|
9
10
|
class Foo < ActiveRecord::Base;end;
|
|
10
11
|
ActiveRecord::Migration.drop_table(Foo.table_name) if Foo.table_exists?
|
|
12
|
+
class Bar < ActiveRecord::Base;end;
|
|
13
|
+
ActiveRecord::Migration.drop_table(Bar.table_name) if Bar.table_exists?
|
|
11
14
|
|
|
12
15
|
class FooTest < Test::Unit::TestCase
|
|
13
16
|
context "defining the schema" do
|
|
@@ -31,7 +34,7 @@ class FooTest < Test::Unit::TestCase
|
|
|
31
34
|
should "create the bar column" do
|
|
32
35
|
assert @foo.respond_to?(:bar)
|
|
33
36
|
end
|
|
34
|
-
end
|
|
37
|
+
end
|
|
35
38
|
end
|
|
36
39
|
|
|
37
40
|
context "updating the schema" do
|
|
@@ -45,4 +48,19 @@ class FooTest < Test::Unit::TestCase
|
|
|
45
48
|
assert !@foo.respond_to?(:bar)
|
|
46
49
|
end
|
|
47
50
|
end
|
|
51
|
+
|
|
52
|
+
context "building a Rails migration" do
|
|
53
|
+
setup do
|
|
54
|
+
Foo.schema :foo, :bar
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
should "output the migration for Foo" do
|
|
58
|
+
assert_equal(
|
|
59
|
+
%Q{create_table :foos do |t|
|
|
60
|
+
t.string :foo
|
|
61
|
+
t.string :bar
|
|
62
|
+
end}, Schemer::Migrator.migration(Foo)
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
48
66
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: citrusbyte-schemer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ben Alavi
|
|
@@ -25,9 +25,11 @@ files:
|
|
|
25
25
|
- README.markdown
|
|
26
26
|
- LICENSE
|
|
27
27
|
- Rakefile
|
|
28
|
+
- lib/schemer/migrator.rb
|
|
28
29
|
- lib/schemer.rb
|
|
29
30
|
- rails/init.rb
|
|
30
31
|
- test/schemer_test.rb
|
|
32
|
+
- tasks/schemer.rake
|
|
31
33
|
has_rdoc: false
|
|
32
34
|
homepage: http://github.com/citrusbyte/schemer
|
|
33
35
|
post_install_message:
|