jcnetdev-auto_migrations 1.0.200807042
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README +51 -0
- data/Rakefile +24 -0
- data/auto_migrations.gemspec +30 -0
- data/init.rb +1 -0
- data/lib/auto_migrations.rb +142 -0
- data/rails/init.rb +2 -0
- data/tasks/auto_migrations_tasks.rake +15 -0
- data/test/auto_migrations_test.rb +8 -0
- metadata +68 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 PJ Hyett
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
== AutoMigrations
|
2
|
+
|
3
|
+
Forget migrations, auto-migrate!
|
4
|
+
|
5
|
+
|
6
|
+
== Usage
|
7
|
+
|
8
|
+
Write out your schema (or use an existing one)
|
9
|
+
|
10
|
+
$ cat db/schema.rb
|
11
|
+
|
12
|
+
ActiveRecord::Schema.define do
|
13
|
+
|
14
|
+
create_table :posts do |t|
|
15
|
+
t.string :title
|
16
|
+
t.text :body
|
17
|
+
t.timestamps
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
$ rake db:auto:migrate
|
23
|
+
|
24
|
+
Created posts table
|
25
|
+
|
26
|
+
...a few days later
|
27
|
+
|
28
|
+
$ cat db/schema.rb
|
29
|
+
|
30
|
+
ActiveRecord::Schema.define do
|
31
|
+
|
32
|
+
create_table :posts do |t|
|
33
|
+
t.string :title
|
34
|
+
t.text :content
|
35
|
+
t.timestamps
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
$ rake db:auto:migrate
|
41
|
+
-- add_column("posts", :content, :text)
|
42
|
+
-> 0.0307s
|
43
|
+
-- remove_column("posts", "body")
|
44
|
+
-> 0.0311s
|
45
|
+
|
46
|
+
Found a bug? Sweet. Add it at the Lighthouse:
|
47
|
+
http://err.lighthouseapp.com/projects/466-plugins/tickets/new
|
48
|
+
|
49
|
+
Feature requests are welcome.
|
50
|
+
|
51
|
+
* PJ Hyett [ pjhyett@gmail.com ]
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the auto_migrations plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the auto_migrations plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
files = ['README', 'LICENSE', 'lib/**/*.rb']
|
18
|
+
rdoc.rdoc_files.add(files)
|
19
|
+
rdoc.main = "README" # page to start on
|
20
|
+
rdoc.title = "auto_migrations"
|
21
|
+
rdoc.template = File.exists?(t="/Users/chris/ruby/projects/err/rock/template.rb") ? t : "/var/www/rock/template.rb"
|
22
|
+
rdoc.rdoc_dir = 'doc' # rdoc output folder
|
23
|
+
rdoc.options << '--inline-source'
|
24
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'auto_migrations'
|
3
|
+
s.version = '1.0.200807042'
|
4
|
+
s.date = '2008-07-04'
|
5
|
+
|
6
|
+
s.summary = "Allows migrations to be run automatically based on updating the schema.rb"
|
7
|
+
s.description = "Forget migrations, auto-migrate!"
|
8
|
+
|
9
|
+
s.authors = ['PJ Hyett']
|
10
|
+
s.email = 'pjhyett@gmail.com'
|
11
|
+
s.homepage = 'http://github.com/pjhyett/auto_migrations'
|
12
|
+
|
13
|
+
s.has_rdoc = false
|
14
|
+
# s.rdoc_options = ["--main", "README"]
|
15
|
+
#s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
16
|
+
|
17
|
+
s.add_dependency 'rails', ['>= 2.1']
|
18
|
+
|
19
|
+
s.files = ["MIT-LICENSE",
|
20
|
+
"README",
|
21
|
+
"Rakefile",
|
22
|
+
"auto_migrations.gemspec",
|
23
|
+
"init.rb",
|
24
|
+
"lib/auto_migrations.rb",
|
25
|
+
"rails/init.rb",
|
26
|
+
"tasks/auto_migrations_tasks.rake"]
|
27
|
+
|
28
|
+
s.test_files = ["test/auto_migrations_test.rb"]
|
29
|
+
|
30
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init"
|
@@ -0,0 +1,142 @@
|
|
1
|
+
module AutoMigrations
|
2
|
+
|
3
|
+
def self.run
|
4
|
+
# Turn off schema_info code for auto-migration
|
5
|
+
class << ActiveRecord::Schema
|
6
|
+
alias :old_define :define
|
7
|
+
attr_accessor :version
|
8
|
+
def define(info={}, &block) @version=info[:version]; instance_eval(&block) end
|
9
|
+
end
|
10
|
+
|
11
|
+
load(File.join(RAILS_ROOT, 'db', 'schema.rb'))
|
12
|
+
ActiveRecord::Migration.drop_unused_tables
|
13
|
+
ActiveRecord::Migration.drop_unused_indexes
|
14
|
+
ActiveRecord::Migration.update_schema_version(ActiveRecord::Schema.version) if ActiveRecord::Schema.version
|
15
|
+
|
16
|
+
class << ActiveRecord::Schema
|
17
|
+
alias :define :old_define
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.schema_to_migration
|
22
|
+
schema = File.read(File.join(RAILS_ROOT, "db", "schema.rb"))
|
23
|
+
schema.gsub!(/#(.)+\n/, '')
|
24
|
+
schema.sub!(/ActiveRecord::Schema.define(.+)do[ ]?\n/, '')
|
25
|
+
schema.gsub!(/^/, ' ')
|
26
|
+
schema = "class InitialSchema < ActiveRecord::Migration\n def self.up\n" + schema
|
27
|
+
schema << "\n def self.down\n"
|
28
|
+
schema << (ActiveRecord::Base.connection.tables - %w(schema_info schema_migrations)).map do |table|
|
29
|
+
" drop_table :#{table}\n"
|
30
|
+
end.join
|
31
|
+
schema << " end\nend\n"
|
32
|
+
migration_file = File.join(RAILS_ROOT, "db", "migrate", "001_initial_schema.rb")
|
33
|
+
File.open(migration_file, "w") { |f| f << schema }
|
34
|
+
puts "Migration created at db/migrate/001_initial_schema.rb"
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.included(base)
|
38
|
+
base.extend ClassMethods
|
39
|
+
class << base
|
40
|
+
cattr_accessor :tables_in_schema, :indexes_in_schema
|
41
|
+
self.tables_in_schema, self.indexes_in_schema = [], []
|
42
|
+
alias_method_chain :method_missing, :auto_migration
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module ClassMethods
|
47
|
+
|
48
|
+
def method_missing_with_auto_migration(method, *args, &block)
|
49
|
+
case method
|
50
|
+
when :create_table
|
51
|
+
auto_create_table(method, *args, &block)
|
52
|
+
when :add_index
|
53
|
+
auto_add_index(method, *args, &block)
|
54
|
+
else
|
55
|
+
method_missing_without_auto_migration(method, *args, &block)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def auto_create_table(method, *args, &block)
|
60
|
+
table_name = args.shift.to_s
|
61
|
+
options = args.pop || {}
|
62
|
+
|
63
|
+
(self.tables_in_schema ||= []) << table_name
|
64
|
+
|
65
|
+
# Table doesn't exist, create it
|
66
|
+
unless ActiveRecord::Base.connection.tables.include?(table_name)
|
67
|
+
return method_missing_without_auto_migration(method, *[table_name, options], &block)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Grab database columns
|
71
|
+
fields_in_db = ActiveRecord::Base.connection.columns(table_name).inject({}) do |hash, column|
|
72
|
+
hash[column.name] = column
|
73
|
+
hash
|
74
|
+
end
|
75
|
+
|
76
|
+
# Grab schema columns (lifted from active_record/connection_adapters/abstract/schema_statements.rb)
|
77
|
+
table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(ActiveRecord::Base.connection)
|
78
|
+
table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false
|
79
|
+
yield table_definition
|
80
|
+
fields_in_schema = table_definition.columns.inject({}) do |hash, column|
|
81
|
+
hash[column.name.to_s] = column
|
82
|
+
hash
|
83
|
+
end
|
84
|
+
|
85
|
+
# Add fields to db new to schema
|
86
|
+
(fields_in_schema.keys - fields_in_db.keys).each do |field|
|
87
|
+
column = fields_in_schema[field]
|
88
|
+
add_column table_name, column.name, column.type.to_sym, :limit => column.limit, :precision => column.precision,
|
89
|
+
:scale => column.scale, :default => column.default, :null => column.null
|
90
|
+
end
|
91
|
+
|
92
|
+
# Remove fields from db no longer in schema
|
93
|
+
(fields_in_db.keys - fields_in_schema.keys & fields_in_db.keys).each do |field|
|
94
|
+
column = fields_in_db[field]
|
95
|
+
remove_column table_name, column.name
|
96
|
+
end
|
97
|
+
|
98
|
+
# Change field type if schema is different from db
|
99
|
+
(fields_in_schema.keys & fields_in_db.keys).each do |field|
|
100
|
+
if (field != 'id') && (fields_in_schema[field].type.to_sym != fields_in_db[field].type.to_sym)
|
101
|
+
change_column table_name, fields_in_schema[field].name.to_sym, fields_in_schema[field].type.to_sym
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def auto_add_index(method, *args, &block)
|
107
|
+
table_name = args.shift.to_s
|
108
|
+
fields = Array(args.shift).map(&:to_s)
|
109
|
+
options = args.shift
|
110
|
+
|
111
|
+
index_name = options[:name] if options
|
112
|
+
index_name ||= "index_#{table_name}_on_#{fields.join('_and_')}"
|
113
|
+
|
114
|
+
(self.indexes_in_schema ||= []) << index_name
|
115
|
+
|
116
|
+
unless ActiveRecord::Base.connection.indexes(table_name).detect { |i| i.name == index_name }
|
117
|
+
method_missing_without_auto_migration(method, *[table_name, fields, options], &block)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def drop_unused_tables
|
122
|
+
(ActiveRecord::Base.connection.tables - tables_in_schema - %w(schema_info schema_migrations)).each do |table|
|
123
|
+
drop_table table
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def drop_unused_indexes
|
128
|
+
tables_in_schema.each do |table_name|
|
129
|
+
indexes_in_db = ActiveRecord::Base.connection.indexes(table_name).map(&:name)
|
130
|
+
(indexes_in_db - indexes_in_schema & indexes_in_db).each do |index_name|
|
131
|
+
remove_index table_name, :name => index_name
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def update_schema_version(version)
|
137
|
+
ActiveRecord::Base.connection.update("UPDATE #{ActiveRecord::Migrator.schema_info_table_name} SET version = #{version}")
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
namespace :db do
|
2
|
+
namespace :auto do
|
3
|
+
desc "Use schema.rb to auto-migrate"
|
4
|
+
task :migrate => :environment do
|
5
|
+
AutoMigrations.run
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
namespace :schema do
|
10
|
+
desc "Create migration from schema.rb"
|
11
|
+
task :to_migration => :environment do
|
12
|
+
AutoMigrations.schema_to_migration
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jcnetdev-auto_migrations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.200807042
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- PJ Hyett
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-07-04 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "2.1"
|
23
|
+
version:
|
24
|
+
description: Forget migrations, auto-migrate!
|
25
|
+
email: pjhyett@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- MIT-LICENSE
|
34
|
+
- README
|
35
|
+
- Rakefile
|
36
|
+
- auto_migrations.gemspec
|
37
|
+
- init.rb
|
38
|
+
- lib/auto_migrations.rb
|
39
|
+
- rails/init.rb
|
40
|
+
- tasks/auto_migrations_tasks.rake
|
41
|
+
has_rdoc: false
|
42
|
+
homepage: http://github.com/pjhyett/auto_migrations
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.2.0
|
64
|
+
signing_key:
|
65
|
+
specification_version: 2
|
66
|
+
summary: Allows migrations to be run automatically based on updating the schema.rb
|
67
|
+
test_files:
|
68
|
+
- test/auto_migrations_test.rb
|