dm-migrations 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,136 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
+
4
+ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
5
+ describe DataMapper::Migration, 'interface' do
6
+ before do
7
+ @migration = DataMapper::Migration.new(1, :create_people_table, :verbose => false) { }
8
+ end
9
+
10
+ it "should have a postition attribute" do
11
+ @migration.should respond_to(:position)
12
+ @migration.should respond_to(:position=)
13
+ @migration.position.should == 1
14
+ end
15
+
16
+ it "should have a name attribute" do
17
+ @migration.should respond_to(:name)
18
+ @migration.should respond_to(:name=)
19
+ @migration.name.should == :create_people_table
20
+ end
21
+
22
+ it "should have a :database option" do
23
+ DataMapper.setup(:other, "sqlite3://#{Dir.pwd}/migration_other.db")
24
+
25
+ m = DataMapper::Migration.new(2, :create_dogs_table, :database => :other) {}
26
+ m.instance_variable_get(:@database).name.should == :other
27
+ end
28
+
29
+ it "should use the default database by default" do
30
+ @migration.instance_variable_get(:@database).name.should == :default
31
+ end
32
+
33
+ it "should have a verbose option" do
34
+ m = DataMapper::Migration.new(2, :create_dogs_table, :verbose => false) {}
35
+ m.instance_variable_get(:@verbose).should == false
36
+ end
37
+
38
+ it "should be verbose by default" do
39
+ m = DataMapper::Migration.new(2, :create_dogs_table) {}
40
+ m.instance_variable_get(:@verbose).should == true
41
+ end
42
+
43
+ it "should be sortable, first by position, then name" do
44
+ m1 = DataMapper::Migration.new(1, :create_people_table) {}
45
+ m2 = DataMapper::Migration.new(2, :create_dogs_table) {}
46
+ m3 = DataMapper::Migration.new(2, :create_cats_table) {}
47
+ m4 = DataMapper::Migration.new(4, :create_birds_table) {}
48
+
49
+ [m1, m2, m3, m4].sort.should == [m1, m3, m2, m4]
50
+ end
51
+
52
+ if HAS_SQLITE3
53
+ it "should extend with SQL::Sqlite3 when adapter is Sqlite3Adapter" do
54
+ DataMapper.setup(:sqlite3, "sqlite3::memory:")
55
+ migration = DataMapper::Migration.new(1, :sqlite3_adapter_test, :database => :sqlite3) { }
56
+ (class << migration.adapter; self; end).included_modules.should include(SQL::Sqlite3)
57
+ end
58
+ end
59
+
60
+ if HAS_MYSQL
61
+ it "should extend with SQL::Mysql when adapter is MysqlAdapter" do
62
+ DataMapper.setup(:mysql, "mysql://localhost/migration_test")
63
+ migration = DataMapper::Migration.new(1, :mysql_adapter_test, :database => :mysql) { }
64
+ (class << migration.adapter; self; end).included_modules.should include(SQL::Mysql)
65
+ end
66
+ end
67
+
68
+ if HAS_POSTGRES
69
+ it "should extend with SQL::Postgres when adapter is PostgresAdapter" do
70
+ DataMapper.setup(:postgres, "postgres://localhost/migration_test")
71
+ migration = DataMapper::Migration.new(1, :postgres_adapter_test, :database => :postgres) { }
72
+ (class << migration.adapter; self; end).included_modules.should include(SQL::Postgresql)
73
+ end
74
+ end
75
+ end
76
+
77
+ describe DataMapper::Migration, 'defining actions' do
78
+ before do
79
+ @migration = DataMapper::Migration.new(1, :create_people_table, :verbose => false) { }
80
+ end
81
+
82
+ it "should have an #up method" do
83
+ @migration.should respond_to(:up)
84
+ end
85
+
86
+ it "should save the block passed into the #up method in @up_action" do
87
+ action = lambda {}
88
+ @migration.up(&action)
89
+
90
+ @migration.instance_variable_get(:@up_action).should == action
91
+ end
92
+
93
+ it "should have a #down method" do
94
+ @migration.should respond_to(:down)
95
+ end
96
+
97
+ it "should save the block passed into the #down method in @down_action" do
98
+ action = lambda {}
99
+ @migration.down(&action)
100
+
101
+ @migration.instance_variable_get(:@down_action).should == action
102
+ end
103
+
104
+ it "should make available an #execute method" do
105
+ @migration.should respond_to(:execute)
106
+ end
107
+
108
+ it "should run the sql passed into the #execute method"
109
+ # TODO: Find out how to stub the DataMapper::database.execute method
110
+ end
111
+
112
+ describe DataMapper::Migration, "output" do
113
+ before do
114
+ @migration = DataMapper::Migration.new(1, :create_people_table) { }
115
+ @migration.stub!(:write) # so that we don't actually write anything to the console!
116
+ end
117
+
118
+ it "should #say a string with an indent" do
119
+ @migration.should_receive(:write).with(" Foobar")
120
+ @migration.say("Foobar", 2)
121
+ end
122
+
123
+ it "should #say with a default indent of 4" do
124
+ @migration.should_receive(:write).with(" Foobar")
125
+ @migration.say("Foobar")
126
+ end
127
+
128
+ it "should #say_with_time the running time of a block" do
129
+ @migration.should_receive(:write).with(/Block/)
130
+ @migration.should_receive(:write).with(/-> [\d]+/)
131
+
132
+ @migration.say_with_time("Block"){ }
133
+ end
134
+
135
+ end
136
+ end
@@ -0,0 +1,148 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
+
4
+ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
5
+ describe DataMapper::Migration, "#create_table helper" do
6
+ before do
7
+ @creator = DataMapper::Migration::TableCreator.new(repository(:default).adapter, :people) do
8
+ column :name, String
9
+ end
10
+ end
11
+
12
+ it "should have a #create_table helper" do
13
+ @migration = DataMapper::Migration.new(1, :create_people_table, :verbose => false) { }
14
+ @migration.should respond_to(:create_table)
15
+ end
16
+
17
+ it "should have a table_name" do
18
+ @creator.table_name.should == "people"
19
+ end
20
+
21
+ it "should have an adapter" do
22
+ @creator.instance_eval("@adapter").should == repository(:default).adapter
23
+ end
24
+
25
+ it "should have an options hash" do
26
+ @creator.opts.should be_kind_of(Hash)
27
+ @creator.opts.should == {}
28
+ end
29
+
30
+ it "should have an array of columns" do
31
+ @creator.instance_eval("@columns").should be_kind_of(Array)
32
+ @creator.instance_eval("@columns").should have(1).item
33
+ @creator.instance_eval("@columns").first.should be_kind_of(DataMapper::Migration::TableCreator::Column)
34
+ end
35
+
36
+ it "should quote the table name for the adapter" do
37
+ @creator.quoted_table_name.should == '"people"'
38
+ end
39
+
40
+ end
41
+
42
+ describe DataMapper::Migration, "#modify_table helper" do
43
+ before do
44
+ @migration = DataMapper::Migration.new(1, :create_people_table, :verbose => false) { }
45
+ end
46
+
47
+ it "should have a #modify_table helper" do
48
+ @migration.should respond_to(:modify_table)
49
+ end
50
+
51
+ end
52
+
53
+ describe DataMapper::Migration, "other helpers" do
54
+ before do
55
+ @migration = DataMapper::Migration.new(1, :create_people_table, :verbose => false) { }
56
+ end
57
+
58
+ it "should have a #drop_table helper" do
59
+ @migration.should respond_to(:drop_table)
60
+ end
61
+
62
+ end
63
+
64
+ describe DataMapper::Migration, "version tracking" do
65
+ before do
66
+ @migration = DataMapper::Migration.new(1, :create_people_table, :verbose => false) do
67
+ up { :ran_up }
68
+ down { :ran_down }
69
+ end
70
+
71
+ @migration.send(:create_migration_info_table_if_needed)
72
+ end
73
+
74
+ def insert_migration_record
75
+ DataMapper.repository.adapter.execute("INSERT INTO migration_info (migration_name) VALUES ('create_people_table')")
76
+ end
77
+
78
+ it "should know if the migration_info table exists" do
79
+ @migration.send(:migration_info_table_exists?).should be_true
80
+ end
81
+
82
+ it "should know if the migration_info table does not exist" do
83
+ repository.adapter.execute("DROP TABLE migration_info") rescue nil
84
+ @migration.send(:migration_info_table_exists?).should be_false
85
+ end
86
+
87
+ it "should be able to find the migration_info record for itself" do
88
+ insert_migration_record
89
+ @migration.send(:migration_record).should_not be_empty
90
+ end
91
+
92
+ it "should know if a migration needs_up?" do
93
+ @migration.send(:needs_up?).should be_true
94
+ insert_migration_record
95
+ @migration.send(:needs_up?).should be_false
96
+ end
97
+
98
+ it "should know if a migration needs_down?" do
99
+ @migration.send(:needs_down?).should be_false
100
+ insert_migration_record
101
+ @migration.send(:needs_down?).should be_true
102
+ end
103
+
104
+ it "should properly quote the migration_info table for use in queries" do
105
+ @migration.send(:migration_info_table).should == '"migration_info"'
106
+ end
107
+
108
+ it "should properly quote the migration_info.migration_name column for use in queries" do
109
+ @migration.send(:migration_name_column).should == '"migration_name"'
110
+ end
111
+
112
+ it "should properly quote the migration's name for use in queries"
113
+ # TODO how to i call the adapter's #escape_sql method?
114
+
115
+ it "should create the migration_info table if it doesn't exist" do
116
+ repository.adapter.execute("DROP TABLE migration_info")
117
+ @migration.send(:migration_info_table_exists?).should be_false
118
+ @migration.send(:create_migration_info_table_if_needed)
119
+ @migration.send(:migration_info_table_exists?).should be_true
120
+ end
121
+
122
+ it "should insert a record into the migration_info table on up" do
123
+ @migration.send(:migration_record).should be_empty
124
+ @migration.perform_up.should == :ran_up
125
+ @migration.send(:migration_record).should_not be_empty
126
+ end
127
+
128
+ it "should remove a record from the migration_info table on down" do
129
+ insert_migration_record
130
+ @migration.send(:migration_record).should_not be_empty
131
+ @migration.perform_down.should == :ran_down
132
+ @migration.send(:migration_record).should be_empty
133
+ end
134
+
135
+ it "should not run the up action if the record exists in the table" do
136
+ insert_migration_record
137
+ @migration.perform_up.should_not == :ran_up
138
+ end
139
+
140
+ it "should not run the down action if the record does not exist in the table" do
141
+ @migration.perform_down.should_not == :ran_down
142
+ end
143
+
144
+ after do
145
+ repository.adapter.execute("DELETE FROM migration_info") rescue Sqlite3Error
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,2 @@
1
+ --format specdoc
2
+ --colour
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ gem 'rspec', '>=1.1.3'
3
+ require 'spec'
4
+ require 'pathname'
5
+ require Pathname(__FILE__).dirname.parent.expand_path + 'lib/dm-migrations'
6
+ require Pathname(__FILE__).dirname.parent.expand_path + 'lib/migration_runner'
7
+
8
+ #require 'fileutils'
9
+
10
+ #DB_FILE = "#{Dir.pwd}/migration_test.db"
11
+ #FileUtils.touch DB_FILE
12
+
13
+ #DataMapper.setup(:default, "sqlite3://#{DB_FILE}")
14
+ #DataMapper::Logger.new(nil, :debug)
15
+
16
+ def load_driver(name, default_uri)
17
+ return false if ENV['ADAPTER'] != name.to_s
18
+
19
+ lib = "do_#{name}"
20
+
21
+ begin
22
+ gem lib, '=0.9.2'
23
+ require lib
24
+ DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
25
+ DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
26
+ true
27
+ rescue Gem::LoadError => e
28
+ warn "Could not load #{lib}: #{e}"
29
+ false
30
+ end
31
+ end
32
+
33
+ ENV['ADAPTER'] ||= 'sqlite3'
34
+
35
+ HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
36
+ HAS_MYSQL = load_driver(:mysql, 'mysql://localhost/dm_core_test')
37
+ HAS_POSTGRES = load_driver(:postgres, 'postgres://postgres@localhost/dm_core_test')
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-migrations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.2
5
+ platform: ruby
6
+ authors:
7
+ - Paul Sadauskas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-25 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dm-core
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.2
23
+ version:
24
+ description: DataMapper plugin for writing and specing migrations
25
+ email: psadauskas@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - LICENSE
33
+ - TODO
34
+ files:
35
+ - lib/dm-migrations.rb
36
+ - lib/migration.rb
37
+ - lib/migration_runner.rb
38
+ - lib/spec/example/migration_example_group.rb
39
+ - lib/spec/matchers/migration_matchers.rb
40
+ - lib/sql/column.rb
41
+ - lib/sql/mysql.rb
42
+ - lib/sql/postgresql.rb
43
+ - lib/sql/sqlite3.rb
44
+ - lib/sql/table.rb
45
+ - lib/sql.rb
46
+ - spec/integration/migration_runner_spec.rb
47
+ - spec/integration/migration_spec.rb
48
+ - spec/integration/sql_spec.rb
49
+ - spec/spec_helper.rb
50
+ - spec/spec.opts
51
+ - Rakefile
52
+ - README
53
+ - LICENSE
54
+ - TODO
55
+ has_rdoc: true
56
+ homepage: http://github.com/sam/dm-more/tree/master/dm-migrations
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.0.1
78
+ signing_key:
79
+ specification_version: 2
80
+ summary: DataMapper plugin for writing and specing migrations
81
+ test_files: []
82
+