activerecord-migrations 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +4 -0
- data/README.md +6 -11
- data/lib/active_record/migrations/tasks.rb +0 -2
- data/lib/active_record/migrations/tasks/db.rb +26 -16
- data/lib/active_record/migrations/tasks/db/fixtures.rb +61 -14
- data/lib/active_record/migrations/tasks/db/migrations.rb +2 -1
- data/lib/active_record/migrations/tasks/db/seed.rb +28 -0
- data/lib/active_record/migrations/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f27fbcf202ed3051454f299ecac2250ab3c82671
|
4
|
+
data.tar.gz: 795e2d9c89a56d81086afb33bc5fab8046584c4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06e39eea69ef2f62824906c146332448473a069b8b155f03d5ad629a64e210a6175eb00301ac3dbb8c5073ff536461a17c4d8b0977f30910c33ac36576c2aa70
|
7
|
+
data.tar.gz: bc9c52f8b6ac392378024116782977ba1952219fdbde9a44314f836b01352c5de33dad1086c1a8ab1a97540a267bb1a51b1ac23e8937f647097d2654086fc844
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -49,22 +49,17 @@ end
|
|
49
49
|
|
50
50
|
### Deployment
|
51
51
|
|
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.
|
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, then run any outstanding migrations. If deploying the site to an existing database it will simply run migrations.
|
53
53
|
|
54
|
-
|
54
|
+
The typical usage is to run `db:fixtures:update` which will generate all the required files into `db/fixtures/$DATABASE/`, which includes the contents of all tables and the current `schema.rb`.
|
55
55
|
|
56
|
-
|
56
|
+
### Fixtures
|
57
57
|
|
58
|
-
|
59
|
-
require_relative 'environment'
|
58
|
+
Fixtures are generated into `db/fixtures/$DATABASE_ENV/$TABLE_NAME.yml` by running `rake db:fixtures:dump`. You can limit this to specific tables by editing `db/fixtures/$DATABASE_ENV/tables.conf` which is loaded if present, or by specifying `TABLES=table1,table2` environment variable.
|
60
59
|
|
61
|
-
|
62
|
-
system("rake", "db:fixtures:load")
|
63
|
-
```
|
64
|
-
|
65
|
-
### Fixtures
|
60
|
+
### Seed
|
66
61
|
|
67
|
-
|
62
|
+
This gem replaces the existing data seed mechanism with `db:fixtures:load`.
|
68
63
|
|
69
64
|
## Contributing
|
70
65
|
|
@@ -38,7 +38,7 @@ namespace :db do
|
|
38
38
|
database_tasks.db_dir = File.join(root, 'db')
|
39
39
|
database_tasks.env = DATABASE_ENV.to_s
|
40
40
|
database_tasks.database_configuration = ActiveRecord::Base.configurations
|
41
|
-
database_tasks.migrations_paths = File.join(root, 'db/migrate')
|
41
|
+
database_tasks.migrations_paths = [File.join(root, 'db/migrate')]
|
42
42
|
database_tasks.fixtures_path = File.join(root, 'db/fixtures', DATABASE_ENV.to_s)
|
43
43
|
|
44
44
|
database_tasks.send(:define_method, :load_seed) do
|
@@ -46,17 +46,32 @@ namespace :db do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
task
|
50
|
-
task :deploy => :load_config do
|
49
|
+
task :schema_path => :load_config do
|
51
50
|
database_tasks = ActiveRecord::Tasks::DatabaseTasks
|
52
51
|
|
53
|
-
|
54
|
-
|
55
|
-
|
52
|
+
paths = [
|
53
|
+
ENV['SCHEMA'],
|
54
|
+
File.join(database_tasks.fixtures_path, 'schema.rb'),
|
55
|
+
File.join(database_tasks.db_dir, 'schema.rb'),
|
56
|
+
].compact
|
57
|
+
|
58
|
+
unless @schema_path = paths.select{|path| File.exist?(path)}.first
|
59
|
+
abort "Could not locate schema file: #{paths.inspect}!"
|
56
60
|
end
|
57
61
|
|
62
|
+
puts "Schema path: #{@schema_path}"
|
63
|
+
end
|
64
|
+
|
65
|
+
desc 'Setup a new database if required and run migrations.'
|
66
|
+
task :deploy => [:load_config, :schema_path] do
|
67
|
+
database_tasks = ActiveRecord::Tasks::DatabaseTasks
|
68
|
+
|
58
69
|
unless ActiveRecord::Migrations.database?
|
59
|
-
Rake::Task['db:
|
70
|
+
Rake::Task['db:create'].invoke
|
71
|
+
|
72
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, @schema_path)
|
73
|
+
|
74
|
+
Rake::Task['db:seed'].invoke
|
60
75
|
end
|
61
76
|
|
62
77
|
Rake::Task['db:migrate'].invoke
|
@@ -88,12 +103,7 @@ end
|
|
88
103
|
# 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.
|
89
104
|
load 'active_record/railties/databases.rake'
|
90
105
|
|
91
|
-
#
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
desc 'Load the seed data into the database.'
|
96
|
-
task :seed do
|
97
|
-
ActiveRecord::Tasks::DatabaseTasks.load_seed
|
98
|
-
end
|
99
|
-
end
|
106
|
+
# We load these afterwards as it augments/replace the existing tasks:
|
107
|
+
require_relative 'db/seed'
|
108
|
+
require_relative 'db/fixtures'
|
109
|
+
require_relative 'db/migrations'
|
@@ -1,42 +1,89 @@
|
|
1
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
1
20
|
|
2
21
|
namespace :db do
|
3
22
|
namespace :fixtures do
|
4
|
-
task :
|
23
|
+
task :fixtures_path => :load_config do
|
24
|
+
@fixtures_path = ENV.fetch('FIXTURES_PATH') {ActiveRecord::Tasks::DatabaseTasks.fixtures_path}
|
25
|
+
@tables_path = File.join(@fixtures_path, 'tables.conf')
|
26
|
+
end
|
27
|
+
|
28
|
+
desc 'List the tables to be dumped, either via `ENV[\'TABLES\']` or `tables.conf`.'
|
29
|
+
task :tables => :fixtures_path do
|
5
30
|
all_tables = ActiveRecord::Base.connection.tables
|
6
31
|
|
7
32
|
if tables_string = ENV['TABLES']
|
8
33
|
@tables = tables_string.split(',')
|
34
|
+
elsif File.exist?(@tables_path)
|
35
|
+
puts "Loading tables from: #{@tables_path}"
|
36
|
+
@tables = YAML::load_file(@tables_path)
|
9
37
|
else
|
10
38
|
# We skip update/event tables since these contain a lot of non-critical data.
|
11
|
-
@tables = all_tables.grep_v(
|
39
|
+
@tables = all_tables.grep_v(/ar_internal_metadata/)
|
12
40
|
end
|
13
41
|
|
14
|
-
puts "Dumping
|
15
|
-
puts "Skipping
|
42
|
+
puts "Dumping tables: #{@tables.join(', ')}"
|
43
|
+
puts "Skipping tables: #{(all_tables - @tables).join(', ')}"
|
44
|
+
end
|
45
|
+
|
46
|
+
desc 'Copy the current schema to the fixtures schema.'
|
47
|
+
task :copy_schema => :fixtures_path do
|
48
|
+
database_tasks = ActiveRecord::Tasks::DatabaseTasks
|
49
|
+
|
50
|
+
fixtures_schema_path = File.join(database_tasks.fixtures_path, 'schema.rb')
|
51
|
+
db_schema_path = File.join(database_tasks.db_dir, 'schema.rb')
|
52
|
+
|
53
|
+
FileUtils.cp(db_schema_path, fixtures_schema_path)
|
16
54
|
end
|
17
55
|
|
18
56
|
desc 'Create YAML fixtures from data in an existing database.'
|
19
57
|
task :dump => :tables do
|
20
|
-
sql
|
58
|
+
sql = "SELECT * FROM %s"
|
21
59
|
|
22
|
-
|
23
|
-
FileUtils.mkpath root
|
60
|
+
FileUtils.mkpath @fixtures_path
|
24
61
|
|
25
62
|
@tables.each do |table_name|
|
26
|
-
|
63
|
+
# It's with regret that we have to use .yml here, as that's the only extension supported by ActiveRecord db:fixtures:load
|
64
|
+
fixture_path = "#{@fixtures_path}/#{table_name}.yml"
|
27
65
|
|
28
66
|
puts "Dumping #{table_name} to #{fixture_path}..."
|
29
67
|
|
30
68
|
i = "000"
|
31
69
|
|
32
70
|
File.open(fixture_path, 'w') do |file|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
hash
|
37
|
-
|
71
|
+
rows = ActiveRecord::Base.connection.select_all(sql % table_name)
|
72
|
+
|
73
|
+
records = rows.each_with_object({}) do |row, hash|
|
74
|
+
hash["#{table_name}_#{i.succ!}"] = row
|
75
|
+
end
|
76
|
+
|
77
|
+
file.write records.to_yaml
|
38
78
|
end
|
39
79
|
end
|
40
80
|
end
|
81
|
+
|
82
|
+
desc 'Update the fixtures and copy the schema.'
|
83
|
+
task :update => [:copy_schema, :dump] do
|
84
|
+
unless File.exist? @tables_path
|
85
|
+
File.write(@tables_path, YAML::dump(@tables))
|
86
|
+
end
|
87
|
+
end
|
41
88
|
end
|
42
|
-
end
|
89
|
+
end
|
@@ -35,7 +35,8 @@ namespace :db do
|
|
35
35
|
require "rails/generators"
|
36
36
|
|
37
37
|
parameters = [name] + options
|
38
|
-
|
38
|
+
|
39
|
+
files = Rails::Generators.invoke "active_record:migration", parameters, :destination_root => ActiveRecord::Migrations.root
|
39
40
|
end
|
40
41
|
end
|
41
42
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
# Now we work around some existing broken tasks:
|
22
|
+
Rake::Task['db:seed'].clear
|
23
|
+
|
24
|
+
namespace :db do
|
25
|
+
desc 'Load the seed data into the database.'
|
26
|
+
task :seed => 'db:fixtures:load' do
|
27
|
+
end
|
28
|
+
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.2.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-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- lib/active_record/migrations/tasks/db.rb
|
115
115
|
- lib/active_record/migrations/tasks/db/fixtures.rb
|
116
116
|
- lib/active_record/migrations/tasks/db/migrations.rb
|
117
|
+
- lib/active_record/migrations/tasks/db/seed.rb
|
117
118
|
- lib/active_record/migrations/version.rb
|
118
119
|
homepage: https://github.com/ioquatix/activerecord-migrations
|
119
120
|
licenses:
|