standalone_migrations 0.1.0

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/.gitignore ADDED
@@ -0,0 +1 @@
1
+ schema.rb
data/README.markdown ADDED
@@ -0,0 +1,65 @@
1
+ Rails migrations in non-Rails (and non Ruby) projects.
2
+ For this code to work you need Ruby, Gems, ActiveRecord, Rake and a suitable database driver installed.
3
+
4
+ USAGE
5
+ =====
6
+ Install Ruby, RubyGems then:
7
+ sudo gem install standalone_migrations
8
+
9
+ Add to `Rakefile` in your projects base directory:
10
+ begin
11
+ require 'standalone_migrations'
12
+ StandaloneMigrations.tasks
13
+ rescue LoadError
14
+ puts 'gem install standalone_migrations to get db:migrate:* tasks!'
15
+ end
16
+
17
+ Add database configuration to `config/database.yml` in your projects base directory e.g.:
18
+ development:
19
+ adapter: mysql
20
+ encoding: utf8
21
+ reconnect: false
22
+ database: somedatabase_dev
23
+ pool: 5
24
+ username: root
25
+ password:
26
+ socket: /var/run/mysqld/mysqld.sock
27
+
28
+ test:
29
+ ...something similar...
30
+
31
+ To create a new database migration run:
32
+
33
+ rake db:new_migration name=FooBarMigration
34
+ edit migrations/20081220234130_foo_bar_migration.rb
35
+
36
+ and fill in the up and down migrations. To apply your newest migration
37
+
38
+ rake db:migrate
39
+
40
+ To migrate to a specific version (for example to rollback)
41
+
42
+ rake db:migrate VERSION=20081220234130
43
+
44
+ To migrate a specific database (for example your "testing" database)
45
+
46
+ rake db:migrate RAILS_ENV=test
47
+
48
+ CREDIT
49
+ ======
50
+ This work is based on Lincoln Stoll's blog post: http://lstoll.net/2008/04/stand-alone-activerecord-migrations/
51
+ and David Welton's post http://journal.dedasys.com/2007/01/28/using-migrations-outside-of-rails
52
+
53
+ FURTHER HELP
54
+ ============
55
+ A good source to learn how to use migrations is:
56
+ http://dizzy.co.uk/ruby_on_rails/cheatsheets/rails-migrations
57
+ or if you're lazy and want to just execute raw SQL
58
+
59
+ def self.up
60
+ execute "insert into foo values (123,'something');"
61
+ end
62
+
63
+ def self.down
64
+ execute "delete from foo where field='something';"
65
+ end
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # rake install -> install gem locally (for tests)
2
+ # rake release -> push to github and release to gemcutter
3
+ # rake version:bump:patch -> increase version and add a git-tag
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = 'standalone_migrations'
8
+ gem.summary = "A thin wrapper to use Rails Migrations in non Rails projects"
9
+ gem.email = "grosser.michael@gmail.com"
10
+ gem.homepage = "http://github.com/thuss/standalone-migrations"
11
+ gem.authors = ["Michael Grosser"]
12
+ %w[rake activerecord].each{|d| gem.add_dependency d}
13
+ end
14
+
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
18
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/database.yml ADDED
@@ -0,0 +1,29 @@
1
+ development:
2
+ adapter: mysql
3
+ encoding: utf8
4
+ reconnect: false
5
+ database: somedatabase_dev
6
+ pool: 5
7
+ username: root
8
+ password:
9
+ socket: /var/run/mysqld/mysqld.sock
10
+
11
+ test:
12
+ adapter: mysql
13
+ encoding: utf8
14
+ reconnect: false
15
+ database: somedatabase_test
16
+ pool: 5
17
+ username: root
18
+ password:
19
+ socket: /var/run/mysqld/mysqld.sock
20
+
21
+ production:
22
+ adapter: mysql
23
+ encoding: utf8
24
+ reconnect: false
25
+ database: somedatabase
26
+ pool: 5
27
+ username: root
28
+ password:
29
+ socket: /var/run/mysqld/mysqld.sock
@@ -0,0 +1,5 @@
1
+ class StandaloneMigrations
2
+ def self.tasks
3
+ load "#{File.dirname(__FILE__)}/../tasks/standalone_migrations.rake"
4
+ end
5
+ end
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{standalone_migrations}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Grosser"]
12
+ s.date = %q{2010-03-15}
13
+ s.email = %q{grosser.michael@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "README.markdown"
16
+ ]
17
+ s.files = [
18
+ ".gitignore",
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "database.yml",
23
+ "lib/standalone_migrations.rb",
24
+ "standalone_migrations.gemspec",
25
+ "tasks/standalone_migrations.rake",
26
+ "vendor/migration_helpers/MIT-LICENSE",
27
+ "vendor/migration_helpers/README.markdown",
28
+ "vendor/migration_helpers/init.rb",
29
+ "vendor/migration_helpers/lib/migration_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/thuss/standalone-migrations}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.6}
35
+ s.summary = %q{A thin wrapper to use Rails Migrations in non Rails projects}
36
+
37
+ if s.respond_to? :specification_version then
38
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<rake>, [">= 0"])
43
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
44
+ else
45
+ s.add_dependency(%q<rake>, [">= 0"])
46
+ s.add_dependency(%q<activerecord>, [">= 0"])
47
+ end
48
+ else
49
+ s.add_dependency(%q<rake>, [">= 0"])
50
+ s.add_dependency(%q<activerecord>, [">= 0"])
51
+ end
52
+ end
53
+
@@ -0,0 +1,75 @@
1
+ APP_BASE = File.expand_path('.') unless defined? APP_BASE
2
+
3
+ # Add to load_path every "lib/" directory in vendor
4
+ Dir["#{File.dirname(__FILE__)}/../vendor/**/lib"].each { |p| $LOAD_PATH << p }
5
+
6
+ namespace :db do
7
+ task :ar_init do
8
+ require 'active_record'
9
+ ENV['RAILS_ENV'] ||= 'development'
10
+ config = YAML.load_file(APP_BASE + '/config/database.yml')[ENV['RAILS_ENV']]
11
+ ActiveRecord::Base.establish_connection(config)
12
+ logger = Logger.new $stderr
13
+ logger.level = Logger::INFO
14
+ ActiveRecord::Base.logger = logger
15
+ end
16
+
17
+ desc "Migrate the database using the scripts in the migrate directory. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
18
+ task :migrate => :ar_init do
19
+ require 'migration_helpers/init'
20
+ ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
21
+ ActiveRecord::Migrator.migrate(APP_BASE + "/migrations/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
22
+ Rake::Task[ "db:schema:dump" ].execute
23
+ end
24
+
25
+ namespace :schema do
26
+ desc "Create schema.rb file that can be portably used against any DB supported by AR"
27
+ task :dump => :ar_init do
28
+ require 'active_record/schema_dumper'
29
+ File.open(ENV['SCHEMA'] || APP_BASE + "/schema.rb", "w") do |file|
30
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
31
+ end
32
+ end
33
+
34
+ desc "Load a ar_schema.rb file into the database"
35
+ task :load => :ar_init do
36
+ file = ENV['SCHEMA'] || APP_BASE + "/schema.rb"
37
+ load(file)
38
+ end
39
+ end
40
+
41
+ desc "Create a new migration"
42
+ task :new_migration do |t|
43
+ unless ENV['name']
44
+ puts "Error: must provide name of migration to generate."
45
+ puts "For example: rake #{t.name} name=add_field_to_form"
46
+ exit 1
47
+ end
48
+
49
+ underscore = lambda { |camel_cased_word|
50
+ camel_cased_word.to_s.gsub(/::/, '/').
51
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
52
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
53
+ tr("-", "_").
54
+ downcase
55
+ }
56
+
57
+ migration = underscore.call( ENV['name'] )
58
+ file_name = "migrations/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_#{migration}.rb"
59
+ class_name = migration.split('_').map { |s| s.capitalize }.join
60
+
61
+ file_contents = <<eof
62
+ class #{class_name} < ActiveRecord::Migration
63
+ def self.up
64
+ end
65
+
66
+ def self.down
67
+ raise ActiveRecord::IrreversibleMigration
68
+ end
69
+ end
70
+ eof
71
+ File.open(file_name, 'w') { |f| f.write file_contents }
72
+
73
+ puts "Created migration #{file_name}"
74
+ end
75
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Jesús García Sáez
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.
@@ -0,0 +1,92 @@
1
+
2
+ DESCRIPTION
3
+ ===========
4
+
5
+ Helpers for migrations of ActiveRecord for dealing with foreign keys and primary keys.
6
+
7
+ FEATURES
8
+ ========
9
+
10
+ * **foreign keys**
11
+ * foreign_key(table, field, referenced_table, referenced_field, on_cascade)
12
+ * drop_foreign_key(table, field)
13
+ * **primary keys**
14
+ * primary_key(table, field)
15
+
16
+ Examples
17
+ ========
18
+
19
+ Typical use:
20
+
21
+ def self.up
22
+ create_table :profiles do |t|
23
+ t.string :first_name
24
+ t.string :last_name
25
+ t.string :email
26
+ t.boolean :is_disabled
27
+ end
28
+ create_table :users do |t|
29
+ t.string :login
30
+ t.string :crypted_password
31
+ t.string :salt
32
+ t.integer :profile_id
33
+ end
34
+
35
+ foreign_key :users, :profile_id, :profiles
36
+ end
37
+
38
+ def self.down
39
+ drop_foreign_key :users, :profile_id
40
+ drop_table :users
41
+ drop_table :profiles
42
+ end
43
+
44
+
45
+ Also, if we don't defined a common :id (exactly it's rails who define it), we should create a primary key:
46
+
47
+ def self.up
48
+ create_table :foo, :id => false do |t|
49
+ t.string :foo, :bar
50
+ end
51
+
52
+ primary_key :foo, [ :foo, :bar ]
53
+ end
54
+
55
+ In the parameter where a field is required (like the second parameter in *primary_key*) you can specified and symbol (or string) or an array of symbols (or strings).
56
+
57
+
58
+ REQUIREMENTS
59
+ ============
60
+
61
+ * It's been tested with Mysql adapter and Jdbcmysql adapter
62
+
63
+ INSTALL
64
+ =======
65
+
66
+ * script/plugin install git://github.com/blaxter/migration_helpers.git
67
+
68
+ LICENSE
69
+ =======
70
+
71
+ (The MIT License)
72
+
73
+ Copyright (c) 2008 Jesús García Sáez <jgarcia@warp.es>
74
+
75
+ Permission is hereby granted, free of charge, to any person obtaining
76
+ a copy of this software and associated documentation files (the
77
+ 'Software'), to deal in the Software without restriction, including
78
+ without limitation the rights to use, copy, modify, merge, publish,
79
+ distribute, sublicense, and/or sell copies of the Software, and to
80
+ permit persons to whom the Software is furnished to do so, subject to
81
+ the following conditions:
82
+
83
+ The above copyright notice and this permission notice shall be
84
+ included in all copies or substantial portions of the Software.
85
+
86
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
87
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
88
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
89
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
90
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
91
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
92
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ require 'migration_helper'
2
+
3
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.
4
+ send :include, MigrationConstraintHelpers
@@ -0,0 +1,51 @@
1
+ module MigrationConstraintHelpers
2
+
3
+ # Creates a foreign key from +table+.+field+ against referenced_table.referenced_field
4
+ #
5
+ # table: The tablename
6
+ # field: A field of the table
7
+ # referenced_table: The table which contains the field referenced
8
+ # referenced_field: The field (which should be part of the primary key) of the referenced table
9
+ # cascade: delete & update on cascade?
10
+ def foreign_key(table, field, referenced_table, referenced_field = :id, cascade = true)
11
+ execute "ALTER TABLE #{table} ADD CONSTRAINT #{constraint_name(table, field)}
12
+ FOREIGN KEY #{constraint_name(table, field)} (#{field_list(field)})
13
+ REFERENCES #{referenced_table}(#{field_list(referenced_field)})
14
+ #{(cascade ? 'ON DELETE CASCADE ON UPDATE CASCADE' : '')}"
15
+ end
16
+
17
+ # Drops a foreign key from +table+.+field+ that has been created before with
18
+ # foreign_key method
19
+ #
20
+ # table: The table name
21
+ # field: A field (or array of fields) of the table
22
+ def drop_foreign_key(table, field)
23
+ execute "ALTER TABLE #{table} DROP FOREIGN KEY #{constraint_name(table, field)}"
24
+ end
25
+
26
+ # Creates a primary key for +table+, which right now HAS NOT primary key defined
27
+ #
28
+ # table: The table name
29
+ # field: A field (or array of fields) of the table that will be part of the primary key
30
+ def primary_key(table, field)
31
+ execute "ALTER TABLE #{table} ADD PRIMARY KEY(#{field_list(field)})"
32
+ end
33
+
34
+ private
35
+
36
+ # Creates a constraint name for table and field given as parameters
37
+ #
38
+ # table: The table name
39
+ # field: A field of the table
40
+ def constraint_name(table, field)
41
+ "fk_#{table}_#{field_list_name(field)}"
42
+ end
43
+
44
+ def field_list(fields)
45
+ fields.is_a?(Array) ? fields.join(',') : fields
46
+ end
47
+
48
+ def field_list_name(fields)
49
+ fields.is_a?(Array) ? fields.join('_') : fields
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: standalone_migrations
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Michael Grosser
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-15 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rake
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: activerecord
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ description:
45
+ email: grosser.michael@gmail.com
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ extra_rdoc_files:
51
+ - README.markdown
52
+ files:
53
+ - .gitignore
54
+ - README.markdown
55
+ - Rakefile
56
+ - VERSION
57
+ - database.yml
58
+ - lib/standalone_migrations.rb
59
+ - standalone_migrations.gemspec
60
+ - tasks/standalone_migrations.rake
61
+ - vendor/migration_helpers/MIT-LICENSE
62
+ - vendor/migration_helpers/README.markdown
63
+ - vendor/migration_helpers/init.rb
64
+ - vendor/migration_helpers/lib/migration_helper.rb
65
+ has_rdoc: true
66
+ homepage: http://github.com/thuss/standalone-migrations
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --charset=UTF-8
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.6
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: A thin wrapper to use Rails Migrations in non Rails projects
95
+ test_files: []
96
+