sql_migrations 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c161cf11a737f57288204c4743855bb7ea87d47
4
- data.tar.gz: 1d0760c754c81ee792c2dd6630182d6f20e82f24
3
+ metadata.gz: 42c3ea2365d623408c9e3f9d5ceb34b46921b900
4
+ data.tar.gz: 2531176c8a89bdeac22974ecd785610e3ca49a02
5
5
  SHA512:
6
- metadata.gz: dad9f6a31134633ac2f14aa74e208dd96292358f34f7214a7ae1230d416ce991b1df78c27691d58a366ff6af67e336d9970f924688bd7d02f0d4d4866de06e32
7
- data.tar.gz: d8ec3f6f6c0bfb69ea0bff00176bb0f24cb33fc8f61e7bad4b9bfe8b560d5fd623e6a40a028aa9673ea5865c9ceedf6e23026851682342dda5819d0fdd93e470
6
+ metadata.gz: da725a80819cd777113d9b186196bfb5f658f6bf852957da9393e07dfe02ded1b34babddab5f1a78d2cc11385fcad8a30cfe990f0f3e33eba216ed3a90c80910
7
+ data.tar.gz: ee34ca3da429f23d42fd71d5e232583413b75c31a309c9681cc848a534b8ea038d8bdfdf14280e826abac7de99cae549b125509d2d9492c6ed191ce446d37921
data/README.md CHANGED
@@ -22,9 +22,11 @@ For example, if you work on old Zend 1 project, and you want to take benefit fro
22
22
  1. First - install Ruby environment, with `rbenv` or `rvm`.
23
23
  2. If your project is not using ruby, create your Gemfile:
24
24
 
25
- source 'https://rubygems.org'
26
- gem 'mysql2'
27
- gem 'sql_migrations'
25
+ ```ruby
26
+ source 'https://rubygems.org'
27
+ gem 'mysql2'
28
+ gem 'sql_migrations'
29
+ ```
28
30
 
29
31
  It is possible to use all database adapters that are supported by `Sequel`.
30
32
  Adapters supported by `Sequel`, by now, are:
@@ -36,60 +38,66 @@ For example, if you work on old Zend 1 project, and you want to take benefit fro
36
38
 
37
39
  If you are using PostgreSQL use
38
40
 
39
- gem 'pg'
41
+ ```ruby
42
+ gem 'pg'
43
+ ```
40
44
 
41
45
  3. Run `bundle install`
42
46
 
43
47
  4. Create database config file in `db/config/databases.yml`
44
48
 
45
- default:
46
- development:
47
- adapter: mysql2
48
- encoding: utf8
49
- database: test_db_dev
50
- username: test_user
51
- password: test_pass
52
- host: 192.168.1.1
53
- test:
54
- adapter: mysql2
55
- encoding: utf8
56
- database: test_db_test
57
- username: test_user
58
- password: test_pass
59
- host: 192.168.1.1
60
-
61
- production:
62
- adapter: mysql2
63
- encoding: utf8
64
- database: test_db_prod
65
- username: test_user
66
- password: test_pass
67
- host: 192.168.1.100
68
- second_db:
69
- development:
70
- adapter: mysql2
71
- encoding: utf8
72
- database: second_db_dev
73
- username: test_user
74
- password: test_pass
75
- host: 127.0.0.1
76
- test:
77
- adapter: mysql2
78
- encoding: utf8
79
- database: second_db_test
80
- username: test_user
81
- password: test_pass
82
- host: 127.0.0.1
83
-
84
- Note that you need to define `default` databases set.
49
+ ```yaml
50
+ default:
51
+ development:
52
+ adapter: mysql2
53
+ encoding: utf8
54
+ database: test_db_dev
55
+ username: test_user
56
+ password: test_pass
57
+ host: 192.168.1.1
58
+ test:
59
+ adapter: mysql2
60
+ encoding: utf8
61
+ database: test_db_test
62
+ username: test_user
63
+ password: test_pass
64
+ host: 192.168.1.1
65
+
66
+ production:
67
+ adapter: mysql2
68
+ encoding: utf8
69
+ database: test_db_prod
70
+ username: test_user
71
+ password: test_pass
72
+ host: 192.168.1.100
73
+ second_db:
74
+ development:
75
+ adapter: mysql2
76
+ encoding: utf8
77
+ database: second_db_dev
78
+ username: test_user
79
+ password: test_pass
80
+ host: 127.0.0.1
81
+ test:
82
+ adapter: mysql2
83
+ encoding: utf8
84
+ database: second_db_test
85
+ username: test_user
86
+ password: test_pass
87
+ host: 127.0.0.1
88
+ ```
89
+
90
+ Note that you need to define `default` database configuration.
85
91
 
86
92
  4. Migrations/seed/fixtures are executed using rake tasks. So you will need to create `Rakefile`:
87
93
 
88
- require 'bundler'
89
- Bundler.require
94
+ ```ruby
95
+ require 'bundler'
96
+ Bundler.require
90
97
 
91
- SqlMigrations.load!('db/config/databases.yml')
92
- SqlMigrations.load_tasks
98
+ SqlMigrations::Config.load!('db/config/databases.yml')
99
+ SqlMigrations.load_tasks
100
+ ```
93
101
 
94
102
  5. It's ready !
95
103
 
@@ -176,7 +184,7 @@ For example, if you work on old Zend 1 project, and you want to take benefit fro
176
184
 
177
185
  ## TODO
178
186
 
179
- 1. Tests
187
+ 1. More tests
180
188
  2. Generator for `databases.yml`
181
189
  3. Generator for migrations
182
190
 
@@ -6,7 +6,7 @@ require 'time'
6
6
 
7
7
  require 'sql_migrations/version'
8
8
  require 'sql_migrations/database'
9
- require 'sql_migrations/supervisor'
9
+ require 'sql_migrations/config'
10
10
  require 'sql_migrations/sql_script'
11
11
  require 'sql_migrations/migration'
12
12
  require 'sql_migrations/seed'
@@ -14,7 +14,6 @@ require 'sql_migrations/fixture'
14
14
 
15
15
  module SqlMigrations
16
16
  class << self
17
- attr_reader :options
18
17
 
19
18
  def load_tasks
20
19
  load "sql_migrations/tasks/migrate.rake"
@@ -23,8 +22,33 @@ module SqlMigrations
23
22
  load "sql_migrations/tasks/list.rake"
24
23
  end
25
24
 
26
- def load!(config_file)
27
- @options = YAML::load_file(config_file)
25
+ def migrate
26
+ databases { |db| db.execute_migrations }
27
+ end
28
+
29
+ def seed
30
+ databases { |db| db.seed_database }
31
+ end
32
+
33
+ def seed_test
34
+ databases { |db| db.seed_with_fixtures }
35
+ end
36
+
37
+ def list_files
38
+ Config.new.databases.each do |db_config|
39
+ name = db_config[:name]
40
+ Migration.find(name).each { |migration| puts migration }
41
+ Seed.find(name).each { |seed| puts seed }
42
+ Fixture.find(name).each { |fixture| puts fixture }
43
+ end
44
+ end
45
+
46
+ private
47
+ def databases
48
+ Config.new.databases.each do |db_config|
49
+ db = Database.new(db_config)
50
+ yield db
51
+ end
28
52
  end
29
53
  end
30
54
  end
@@ -0,0 +1,38 @@
1
+ module SqlMigrations
2
+ class Config
3
+
4
+ class << self
5
+ attr_reader :options
6
+
7
+ def load!(config_file)
8
+ @options = YAML::load_file(config_file)
9
+ end
10
+ end
11
+
12
+ def initialize
13
+ @env = (ENV['ENV'] ||= "development").to_sym
14
+ @options = self.class.options
15
+ @databases = get_databases_from_config
16
+ end
17
+
18
+ def databases
19
+ @databases.map do |db|
20
+ db_options = @options[db.to_s][@env.to_s]
21
+ unless db_options
22
+ raise "Configuration for #{db} in environment #{@env} not found !"
23
+ end
24
+ db_options.merge!(name: db)
25
+ end
26
+ end
27
+
28
+ private
29
+ def get_databases_from_config
30
+ databases = @options.map { |k, v| k.to_sym }
31
+ unless databases.include?(:default)
32
+ raise "Default database configuration not found !"
33
+ end
34
+ databases
35
+ end
36
+
37
+ end
38
+ end
@@ -2,7 +2,7 @@ module SqlMigrations
2
2
  class Database
3
3
 
4
4
  SCHEMA_TABLE = :sqlmigrations_schema
5
- attr_reader :db
5
+ attr_reader :db, :name
6
6
 
7
7
  def initialize(options)
8
8
  @name = options[:name] || :default
@@ -16,7 +16,7 @@ module SqlMigrations
16
16
 
17
17
  def execute(db)
18
18
  @database = db
19
- return unless is_new?
19
+ return false unless is_new?
20
20
  begin
21
21
  @database.db.transaction do
22
22
  @benchmark = Benchmark.measure do
@@ -27,7 +27,7 @@ module SqlMigrations
27
27
  puts "[-] Error while executing #{@type} #{@name} !"
28
28
  raise
29
29
  else
30
- on_success
30
+ true & on_success
31
31
  end
32
32
  end
33
33
 
@@ -2,7 +2,7 @@ namespace :sqlmigrations do
2
2
  namespace :files do
3
3
  desc "List found migration and seed files"
4
4
  task :list do
5
- SqlMigrations::Supervisor.new.list_files
5
+ SqlMigrations.list_files
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ namespace :sqlmigrations do
2
2
  namespace :db do
3
3
  desc "Run migrations"
4
4
  task :migrate do
5
- SqlMigrations::Supervisor.new.migrate
5
+ SqlMigrations.migrate
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ namespace :sqlmigrations do
2
2
  namespace :db do
3
3
  desc "Seed database"
4
4
  task :seed do
5
- SqlMigrations::Supervisor.new.seed
5
+ SqlMigrations.seed
6
6
  end
7
7
  end
8
8
  end
@@ -3,7 +3,7 @@ namespace :sqlmigrations do
3
3
  namespace :test do
4
4
  desc "Seed test database with fixtures"
5
5
  task :seed do
6
- SqlMigrations::Supervisor.new.seed_test
6
+ SqlMigrations.seed_test
7
7
  end
8
8
  end
9
9
  end
@@ -1,3 +1,3 @@
1
1
  module SqlMigrations
2
- VERSION = "1.1.0"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -0,0 +1,49 @@
1
+ describe 'migrations valid order support engine' do
2
+ before do
3
+ $stdout = StringIO.new
4
+ Dir.mkdir('/migrations')
5
+ File.open('/migrations/20150305_154010_test_migration.sql', 'w') do |f|
6
+ f.puts "CREATE TABLE test_table(col_int INTEGER, col_str STRING)"
7
+ end
8
+
9
+ @database = SqlMigrations::Database.new(name: :default, 'adapter' => :sqlite)
10
+ migration = SqlMigrations::Migration.find([ :default ]).first
11
+ migration.execute(@database)
12
+
13
+ File.open('/migrations/20150305_154011_test2_migration.sql', 'w') do |f|
14
+ f.puts "CREATE TABLE test_table2(col_int2 INTEGER, col_str2 STRING)"
15
+ end
16
+ File.open('/migrations/20150305_154012_test3_migration.sql', 'w') do |f|
17
+ f.puts "CREATE TABLE test_table3(col_int3 INTEGER, col_str3 STRING)"
18
+ end
19
+ @migrations = SqlMigrations::Migration.find([ :default ])
20
+ end
21
+
22
+
23
+ it 'should find all migrations' do
24
+ expect(@migrations.count).to be 3
25
+ end
26
+
27
+ it 'should execute only two migrations' do
28
+ statuses = @migrations.map do |migration|
29
+ migration.execute(@database)
30
+ end
31
+ expect(statuses[0]).to be false
32
+ expect(statuses[1]).to be true
33
+ expect(statuses[2]).to be true
34
+ end
35
+
36
+ it 'should create all tables' do
37
+ database = SqlMigrations::Database.new(name: :default, 'adapter' => :sqlite)
38
+ @migrations.each { |migration| migration.execute(@database) }
39
+ expect(@sqlite_db.table_exists?(:test_table)).to be true
40
+ expect(@sqlite_db[:test_table].columns).to include(:col_int)
41
+ expect(@sqlite_db[:test_table].columns).to include(:col_str)
42
+ expect(@sqlite_db.table_exists?(:test_table2)).to be true
43
+ expect(@sqlite_db[:test_table2].columns).to include(:col_int2)
44
+ expect(@sqlite_db[:test_table2].columns).to include(:col_str2)
45
+ expect(@sqlite_db.table_exists?(:test_table3)).to be true
46
+ expect(@sqlite_db[:test_table3].columns).to include(:col_int3)
47
+ expect(@sqlite_db[:test_table3].columns).to include(:col_str3)
48
+ end
49
+ end
@@ -15,13 +15,13 @@ describe 'sql scripts' do
15
15
  f.puts "INSERT INTO second_test_table(col_int2, col_str2) VALUES(456, 'test_string2')"
16
16
  end
17
17
  File.open('/fixtures/20150305_154010_test_test_seed', 'w') do |f|
18
- f.puts "INSERT INTO first_test_table(col_int1, col_str1) VALUES(2123, '2test_string1')"
19
- f.puts "INSERT INTO second_test_table(col_int2, col_str2) VALUES(2456, '2test_string2')"
18
+ f.puts "INSERT INTO first_test2_table(col_int1, col_str1) VALUES(2123, '2test_string1')"
19
+ f.puts "INSERT INTO second_test2_table(col_int2, col_str2) VALUES(2456, '2test_string2')"
20
20
  end
21
21
  end
22
22
 
23
23
  it 'should be found' do
24
- expect { SqlMigrations::Supervisor.new.list_files }.to \
24
+ expect { SqlMigrations.list_files }.to \
25
25
  output("Migration first_test_migration for db: default, datetime: 20150305154010\n" +
26
26
  "Migration second_test_migration for db: default, datetime: 20150305154011\n" +
27
27
  "Seed data test_seed, datetime: 20150305154010\n").to_stdout
data/spec/spec_helper.rb CHANGED
@@ -6,7 +6,7 @@ RSpec.configure do |config|
6
6
  config.before do
7
7
  @sqlite_db = Sequel.sqlite
8
8
  allow(SqlMigrations::Database).to receive(:connect) { @sqlite_db }
9
- allow(SqlMigrations).to receive(:options) { { default: { development: {}}} }
9
+ allow(SqlMigrations::Config).to receive(:options) { { "default" => { "development" => {}}} }
10
10
  MemFs.activate!
11
11
  end
12
12
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sql_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grzegorz Bizon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-06 00:00:00.000000000 Z
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -109,17 +109,18 @@ files:
109
109
  - README.md
110
110
  - Rakefile
111
111
  - lib/sql_migrations.rb
112
+ - lib/sql_migrations/config.rb
112
113
  - lib/sql_migrations/database.rb
113
114
  - lib/sql_migrations/fixture.rb
114
115
  - lib/sql_migrations/migration.rb
115
116
  - lib/sql_migrations/seed.rb
116
117
  - lib/sql_migrations/sql_script.rb
117
- - lib/sql_migrations/supervisor.rb
118
118
  - lib/sql_migrations/tasks/list.rake
119
119
  - lib/sql_migrations/tasks/migrate.rake
120
120
  - lib/sql_migrations/tasks/seed.rake
121
121
  - lib/sql_migrations/tasks/seed_test.rake
122
122
  - lib/sql_migrations/version.rb
123
+ - spec/features/migration_order_spec.rb
123
124
  - spec/features/migration_spec.rb
124
125
  - spec/features/schema_table_spec.rb
125
126
  - spec/features/sql_scripts_spec.rb
@@ -150,6 +151,7 @@ signing_key:
150
151
  specification_version: 4
151
152
  summary: Simple standalone migrations you can use with plain SQL
152
153
  test_files:
154
+ - spec/features/migration_order_spec.rb
153
155
  - spec/features/migration_spec.rb
154
156
  - spec/features/schema_table_spec.rb
155
157
  - spec/features/sql_scripts_spec.rb
@@ -1,50 +0,0 @@
1
- module SqlMigrations
2
- class Supervisor
3
-
4
- def initialize
5
- @env = (ENV['ENV'] ||= "development").to_sym
6
- @options = SqlMigrations.options
7
- @databases = get_databases_from_config
8
- end
9
-
10
- def migrate
11
- databases_run { |db| db.execute_migrations }
12
- end
13
-
14
- def seed
15
- databases_run { |db| db.seed_database }
16
- end
17
-
18
- def seed_test
19
- databases_run { |db| db.seed_with_fixtures }
20
- end
21
-
22
- def list_files
23
- Migration.find(@databases).each { |migration| puts migration }
24
- Seed.find(@databases).each { |seed| puts seed }
25
- Fixture.find(@databases).each { |fixture| puts fixture }
26
- end
27
-
28
- private
29
- def get_databases_from_config
30
- databases = @options.map { |k, v| k.to_sym }
31
- unless databases.include?(:default)
32
- raise "Default database configuration not found !"
33
- end
34
- databases
35
- end
36
-
37
- def databases_run
38
- @databases.each do |db|
39
- db_options = @options[db.to_s][@env.to_s]
40
- db_options.merge!(name: db)
41
- if db_options
42
- yield Database.new(db_options)
43
- else
44
- raise "Configuration for #{db} in environment #{@env} not found !"
45
- end
46
- end
47
- end
48
-
49
- end
50
- end