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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eae2931191d7a98df4c19bea4bb55d100be3573b
4
- data.tar.gz: d25d1db9c8fecc378e71c5de060ac27c87595acc
3
+ metadata.gz: f27fbcf202ed3051454f299ecac2250ab3c82671
4
+ data.tar.gz: 795e2d9c89a56d81086afb33bc5fab8046584c4e
5
5
  SHA512:
6
- metadata.gz: af0538820698e7750c130aeb99fd64e81a091497695fb2a8d03c82c4c50058ba6046e84d4751983e77596147c4da354b08ad2fb7368ad65379a7e0fe0b7c018e
7
- data.tar.gz: 4fc9b25f826fdbc65b23ad286867b9dfa74fe84431ceaa650def7a666609d1d881ef9e91d45b0937ac2445bdb4e22f844ad193d0d5cddb52e9721b43a77663da
6
+ metadata.gz: 06e39eea69ef2f62824906c146332448473a069b8b155f03d5ad629a64e210a6175eb00301ac3dbb8c5073ff536461a17c4d8b0977f30910c33ac36576c2aa70
7
+ data.tar.gz: bc9c52f8b6ac392378024116782977ba1952219fdbde9a44314f836b01352c5de33dad1086c1a8ab1a97540a267bb1a51b1ac23e8937f647097d2654086fc844
data/.gitignore CHANGED
@@ -7,3 +7,7 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+
11
+ spec/active_record/project/db/development.db
12
+ spec/active_record/project/db/schema.rb
13
+
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
- ### `db/seed.rb`
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
- This file should contain the logic for loading your fixtures or other seed data. Something like this:
56
+ ### Fixtures
57
57
 
58
- ```ruby
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
- puts "Loading fixtures..."
62
- system("rake", "db:fixtures:load")
63
- ```
64
-
65
- ### Fixtures
60
+ ### Seed
66
61
 
67
- Fixtures are generated into `db/fixtures/#{environment}/table.yml` by running `rake db:fixtures:dump`.
62
+ This gem replaces the existing data seed mechanism with `db:fixtures:load`.
68
63
 
69
64
  ## Contributing
70
65
 
@@ -19,8 +19,6 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  require_relative 'tasks/db'
22
- require_relative 'tasks/db/migrations'
23
- require_relative 'tasks/db/fixtures'
24
22
 
25
23
  module ActiveRecord
26
24
  module Migrations
@@ -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 'Setup a new database if required and run migrations.'
50
- task :deploy => :load_config do
49
+ task :schema_path => :load_config do
51
50
  database_tasks = ActiveRecord::Tasks::DatabaseTasks
52
51
 
53
- schema_path = File.join(database_tasks.db_dir, 'schema.rb')
54
- unless File.exist? schema_path
55
- abort "Missing #{schema_path}, cannot deploy!"
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:setup'].invoke
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
- # Now we work around some existing broken tasks:
92
- Rake::Task['db:seed'].clear
93
-
94
- namespace :db do
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 :tables => :environment do
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(/.*?(_update|_event|_notification)/)
39
+ @tables = all_tables.grep_v(/ar_internal_metadata/)
12
40
  end
13
41
 
14
- puts "Dumping Tables: #{@tables.join(', ')}"
15
- puts "Skipping Tables: #{(all_tables - @tables).join(', ')}"
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 = "SELECT * FROM %s"
58
+ sql = "SELECT * FROM %s"
21
59
 
22
- root = ENV.fetch('FIXTURES_PATH') {ActiveRecord::Tasks::DatabaseTasks.fixtures_path}
23
- FileUtils.mkpath root
60
+ FileUtils.mkpath @fixtures_path
24
61
 
25
62
  @tables.each do |table_name|
26
- fixture_path = "#{root}/#{table_name}.yml"
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
- 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
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
- Rails::Generators.invoke "active_record:migration", parameters, :destination_root => ActiveRecord::Migrations.root
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
@@ -20,6 +20,6 @@
20
20
 
21
21
  module ActiveRecord
22
22
  module Migrations
23
- VERSION = "1.1.2"
23
+ VERSION = "1.2.0"
24
24
  end
25
25
  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.1.2
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-13 00:00:00.000000000 Z
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: