activerecord-migrations 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.simplecov +9 -0
- data/.travis.yml +1 -0
- data/Gemfile +5 -0
- data/README.md +35 -7
- data/Rakefile +7 -1
- data/lib/active_record/migrations/tasks.rb +16 -0
- data/lib/active_record/migrations/tasks/db.rb +33 -3
- data/lib/active_record/migrations/tasks/db/fixtures.rb +42 -0
- data/lib/active_record/migrations/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c797227ba7ce39490972e3b4359009704d86e783
|
4
|
+
data.tar.gz: 90e597ea6837e8334d1754e1805078a7ffb61230
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6bca1e89dc669172ecc88ec4f2f3d4b76cbc6b2dcd152464f776ea4e6029b476ffdc5fff4f28c9f889b1ac6020a247c0ccafba7690fd005523cec1e7e524f356
|
7
|
+
data.tar.gz: 89992a27d37e351c1352a0827c4489417efa7d2d9d7e978fa0dfca3ec6966cc9256f421e646fc38493ba26740257e79f763cc34571f0d3445dde63b57133b565
|
data/.simplecov
ADDED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# ActiveRecord::Migrations
|
2
2
|
|
3
|
-
This gem provides opinionated support for ActiveRecord migrations in non-Rails applications. It's
|
3
|
+
This gem provides opinionated support for ActiveRecord migrations in non-Rails applications. It's more opinionated than [active_record_migrations](https://github.com/rosenfeld/active_record_migrations) and [standalone-migrations](https://github.com/thuss/standalone-migrations), and therefore simpler to configure.
|
4
|
+
|
5
|
+
[![Build Status](https://secure.travis-ci.org/ioquatix/activerecord-migrations.svg)](http://travis-ci.org/ioquatix/activerecord-migrations)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/ioquatix/activerecord-migrations.svg)](https://codeclimate.com/github/ioquatix/activerecord-migrations)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/ioquatix/activerecord-migrations/badge.svg)](https://coveralls.io/r/ioquatix/activerecord-migrations)
|
4
8
|
|
5
9
|
## Installation
|
6
10
|
|
@@ -14,6 +18,14 @@ gem 'activerecord-migrations'
|
|
14
18
|
|
15
19
|
Add something like this to `tasks/db.rake`
|
16
20
|
|
21
|
+
```ruby
|
22
|
+
require 'active_record/migrations/tasks'
|
23
|
+
# The root must be the root of your application, and contain `db/`.
|
24
|
+
ActiveRecord::Migrations.root = File.dirname(__dir__)
|
25
|
+
```
|
26
|
+
|
27
|
+
Somewhere else, ensure you have a suitable `environment` task, e.g:
|
28
|
+
|
17
29
|
```ruby
|
18
30
|
task :environment do
|
19
31
|
# This must be a symbol... or establish_connection thinks it's a URL.
|
@@ -22,9 +34,9 @@ task :environment do
|
|
22
34
|
ActiveRecord::Base.configurations = {
|
23
35
|
# This key must be a string or it will not be matched by ActiveRecord:
|
24
36
|
'development' => {
|
25
|
-
|
26
|
-
|
27
|
-
|
37
|
+
# Using symbols for keys is fixed by this gem.
|
38
|
+
adapter: => 'sqlite3',
|
39
|
+
database: => 'db/development.db'
|
28
40
|
}
|
29
41
|
}
|
30
42
|
|
@@ -33,15 +45,27 @@ task :environment do
|
|
33
45
|
ActiveRecord::Base.establish_connection(DATABASE_ENV)
|
34
46
|
end
|
35
47
|
end
|
36
|
-
|
37
|
-
require 'active_record/migrations'
|
38
|
-
ActiveRecord::Migrations.root = File.expand_path("../db", __dir__)
|
39
48
|
```
|
40
49
|
|
41
50
|
### Deployment
|
42
51
|
|
43
52
|
This gem includes an additional task `db:deploy` which is designed to assist with deployment of sites with databases. When deploying a site for the first time, this task will create the database and load the seed data. If deploying the site to an existing database it will simply run migrations.
|
44
53
|
|
54
|
+
### `db/seed.rb`
|
55
|
+
|
56
|
+
This file should contain the logic for loading your fixtures or other seed data. Something like this:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
require_relative 'environment'
|
60
|
+
|
61
|
+
puts "Loading fixtures..."
|
62
|
+
system("rake", "db:fixtures:load")
|
63
|
+
```
|
64
|
+
|
65
|
+
### Fixtures
|
66
|
+
|
67
|
+
Fixtures are generated into `db/fixtures/#{environment}/table.yml` by running `rake db:fixtures:dump`.
|
68
|
+
|
45
69
|
## Contributing
|
46
70
|
|
47
71
|
1. Fork it
|
@@ -50,6 +74,10 @@ This gem includes an additional task `db:deploy` which is designed to assist wit
|
|
50
74
|
4. Push to the branch (`git push origin my-new-feature`)
|
51
75
|
5. Create new Pull Request
|
52
76
|
|
77
|
+
## See Also
|
78
|
+
|
79
|
+
- [ActiveRecord::Rack](https://github.com/ioquatix/activerecord-rack)
|
80
|
+
|
53
81
|
## License
|
54
82
|
|
55
83
|
Released under the MIT license.
|
data/Rakefile
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "rspec/core/rake_task"
|
3
3
|
|
4
|
-
RSpec::Core::RakeTask.new(:spec)
|
4
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
5
|
+
begin
|
6
|
+
require('simplecov/version')
|
7
|
+
task.rspec_opts = %w{--require simplecov} if ENV['COVERAGE']
|
8
|
+
rescue LoadError
|
9
|
+
end
|
10
|
+
end
|
5
11
|
|
6
12
|
task :default => :spec
|
@@ -20,6 +20,7 @@
|
|
20
20
|
|
21
21
|
require_relative 'tasks/db'
|
22
22
|
require_relative 'tasks/db/migrations'
|
23
|
+
require_relative 'tasks/db/fixtures'
|
23
24
|
|
24
25
|
module ActiveRecord
|
25
26
|
module Migrations
|
@@ -33,4 +34,19 @@ module ActiveRecord
|
|
33
34
|
false
|
34
35
|
end
|
35
36
|
end
|
37
|
+
|
38
|
+
if defined? Tasks::DatabaseTasks
|
39
|
+
module Tasks
|
40
|
+
module DatabaseTasks
|
41
|
+
def each_current_configuration(environment)
|
42
|
+
unless configuration = ActiveRecord::Base.configurations[environment]
|
43
|
+
raise ArgumentError.new("Cannot find configuration for environment #{environment}")
|
44
|
+
end
|
45
|
+
|
46
|
+
# This is a hack because DatabaseTasks functionality uses string for keys.
|
47
|
+
yield configuration.stringify_keys
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
36
52
|
end
|
@@ -39,6 +39,7 @@ namespace :db do
|
|
39
39
|
database_tasks.env = DATABASE_ENV.to_s
|
40
40
|
database_tasks.database_configuration = ActiveRecord::Base.configurations
|
41
41
|
database_tasks.migrations_paths = File.join(root, 'db/migrate')
|
42
|
+
database_tasks.fixtures_path = File.join(root, 'db/fixtures', DATABASE_ENV.to_s)
|
42
43
|
|
43
44
|
database_tasks.send(:define_method, :load_seed) do
|
44
45
|
load File.join(root, 'db/seed.rb')
|
@@ -54,19 +55,48 @@ namespace :db do
|
|
54
55
|
abort "Missing #{schema_path}, cannot deploy!"
|
55
56
|
end
|
56
57
|
|
57
|
-
|
58
|
-
Rake::Task['db:migrate'].invoke
|
59
|
-
else
|
58
|
+
unless ActiveRecord::Migrations.database?
|
60
59
|
Rake::Task['db:setup'].invoke
|
61
60
|
end
|
61
|
+
|
62
|
+
Rake::Task['db:migrate'].invoke
|
63
|
+
end
|
64
|
+
|
65
|
+
task :connection_config => :environment do
|
66
|
+
require 'pp'
|
67
|
+
|
68
|
+
pp ActiveRecord::Base.connection_config
|
62
69
|
end
|
63
70
|
|
64
71
|
task :connection_config => :environment do
|
65
72
|
require 'pp'
|
66
73
|
|
74
|
+
puts "Connection Configuration:"
|
67
75
|
pp ActiveRecord::Base.connection_config
|
68
76
|
end
|
77
|
+
|
78
|
+
desc 'Print out connection configuration and available tables/data sources.'
|
79
|
+
task :info => :connection_config do
|
80
|
+
puts "Available Data Sources:"
|
81
|
+
if ActiveRecord::Migrations.database?
|
82
|
+
ActiveRecord::Base.connection.data_sources.each do |data_source|
|
83
|
+
puts "\t#{data_source}"
|
84
|
+
end
|
85
|
+
else
|
86
|
+
puts "\tDatabase does not exist."
|
87
|
+
end
|
88
|
+
end
|
69
89
|
end
|
70
90
|
|
71
91
|
# Loading this AFTER we define our own load_config task is critical, we need to make sure things are set up correctly before we run the task of the same name from ActiveRecord.
|
72
92
|
load 'active_record/railties/databases.rake'
|
93
|
+
|
94
|
+
# Now we work around some existing broken tasks:
|
95
|
+
Rake::Task['db:seed'].clear
|
96
|
+
|
97
|
+
namespace :db do
|
98
|
+
desc 'Load the seed data into the database.'
|
99
|
+
task :seed do
|
100
|
+
ActiveRecord::Tasks::DatabaseTasks.load_seed
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
namespace :db do
|
3
|
+
namespace :fixtures do
|
4
|
+
task :tables => :environment do
|
5
|
+
all_tables = ActiveRecord::Base.connection.tables
|
6
|
+
|
7
|
+
if tables_string = ENV['TABLES']
|
8
|
+
@tables = tables_string.split(',')
|
9
|
+
else
|
10
|
+
# We skip update/event tables since these contain a lot of non-critical data.
|
11
|
+
@tables = all_tables.grep_v(/.*?(_update|_event|_notification)/)
|
12
|
+
end
|
13
|
+
|
14
|
+
puts "Dumping Tables: #{@tables.join(', ')}"
|
15
|
+
puts "Skipping Tables: #{(all_tables - @tables).join(', ')}"
|
16
|
+
end
|
17
|
+
|
18
|
+
desc 'Create YAML fixtures from data in an existing database.'
|
19
|
+
task :dump => :tables do
|
20
|
+
sql = "SELECT * FROM %s"
|
21
|
+
|
22
|
+
root = ENV.fetch('FIXTURES_PATH') {ActiveRecord::Tasks::DatabaseTasks.fixtures_path}
|
23
|
+
FileUtils.mkpath root
|
24
|
+
|
25
|
+
@tables.each do |table_name|
|
26
|
+
fixture_path = "#{root}/#{table_name}.yml"
|
27
|
+
|
28
|
+
puts "Dumping #{table_name} to #{fixture_path}..."
|
29
|
+
|
30
|
+
i = "000"
|
31
|
+
|
32
|
+
File.open(fixture_path, 'w') do |file|
|
33
|
+
data = ActiveRecord::Base.connection.select_all(sql % table_name)
|
34
|
+
file.write data.inject({}) { |hash, record|
|
35
|
+
hash["#{table_name}_#{i.succ!}"] = record
|
36
|
+
hash
|
37
|
+
}.to_yaml
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-migrations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -89,6 +89,7 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
91
|
- ".rspec"
|
92
|
+
- ".simplecov"
|
92
93
|
- ".travis.yml"
|
93
94
|
- Gemfile
|
94
95
|
- README.md
|
@@ -97,6 +98,7 @@ files:
|
|
97
98
|
- lib/active_record/migrations.rb
|
98
99
|
- lib/active_record/migrations/tasks.rb
|
99
100
|
- lib/active_record/migrations/tasks/db.rb
|
101
|
+
- lib/active_record/migrations/tasks/db/fixtures.rb
|
100
102
|
- lib/active_record/migrations/tasks/db/migrations.rb
|
101
103
|
- lib/active_record/migrations/version.rb
|
102
104
|
homepage: https://github.com/ioquatix/activerecord-migrations
|