structure_sql_to_migration 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ Rails 3 structure.sql to migration converter
2
+ =====
3
+
4
+ Converts `db/structure.sql` into a migration with up and down. Only tested with PostgreSQL 9.x. If you have a different version of postgres and/or are using a different database, it may or may not work.
5
+
6
+ ### Installation
7
+
8
+ In your Gemfile, add:
9
+
10
+ gem 'structure_sql_to_migration'
11
+
12
+ Then:
13
+
14
+ bundle install
15
+
16
+ ### Usage
17
+
18
+ #### Getting the initial structure.sql
19
+
20
+ Do not proceed unless you understand what you are doing with the following commands. Some may be destructive.
21
+
22
+ The migration it generates may be destructive as well. Do not run the migration that is generated without ensuring it is safe to use.
23
+
24
+ ##### Option 1: Migrate to the initial state and dump structure.sql
25
+
26
+ 1. If possible, rollback the schema to the point before any existing migrations in your application (if you have any):
27
+
28
+ rake db:migrate VERSION=0
29
+
30
+ 2. Dump the structure.sql:
31
+
32
+ rake db:structure:dump
33
+
34
+ ##### Option 2: Recover your database to the point before you made any migrations and dump structure.sql
35
+
36
+ 1. Recover your database from a dump as necessary that reflected the database before any migrations were run
37
+
38
+ 2. Dump the structure.sql:
39
+
40
+ rake db:structure:dump
41
+
42
+ ##### Option 3: Dump structure.sql and hand-modify to get back to initial state prior to migrations
43
+
44
+ 1. Dump the structure.sql:
45
+
46
+ rake db:structure:dump
47
+
48
+ 2. Edit `db/structure.sql` to undo changes by all migrations:
49
+
50
+ #### Execute the rake task
51
+
52
+ To create: `db/000_create_initial_schema.rb`, run:
53
+
54
+ rake structure_sql_to_migration
55
+
56
+ ### License
57
+
58
+ Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
59
+
60
+ [lic]: http://github.com/garysweaver/structure_sql_to_migration/blob/master/LICENSE
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rake/testtask'
2
+
3
+ #http://nicksda.apotomo.de/2010/10/testing-your-rails-3-engine-sitting-in-a-gem/
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ desc "Run tests"
11
+ task :default => :test
@@ -0,0 +1,9 @@
1
+ require 'structure_sql_to_migration/version'
2
+
3
+ module StructureSqlToMigration
4
+ class Railtie < ::Rails::Railtie
5
+ rake_tasks do
6
+ load File.join(File.dirname(__FILE__), 'tasks/structure_sql_to_migration.rake')
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module StructureSqlToMigration
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,78 @@
1
+ require 'tempfile'
2
+ require 'fileutils'
3
+
4
+ desc 'Attempts to convert db/structure.sql into db/000_create_initial_schema.rb.'
5
+ task :structure_sql_to_migration do
6
+ drop_body = []
7
+ structure_file = File.join('db','structure.sql')
8
+ migration_file = File.join('db','migrate/000_create_initial_schema.rb')
9
+ File.delete(migration_file) if File.exists?(migration_file)
10
+ temp_file = Tempfile.new('000_create_initial_schema.rb')
11
+ begin
12
+ File.open(structure_file, 'r') do |file|
13
+ header = <<-eos1
14
+ class CreateInitialSchema < ActiveRecord::Migration
15
+ def up
16
+ statements = <<-eos
17
+ eos1
18
+ temp_file.puts header
19
+ ignore_next_line = false
20
+ file.each_line do |line|
21
+ unless line.strip == '' || line.starts_with?("--")
22
+ if line.starts_with?("INSERT INTO schema_migrations ") || line.starts_with?("CREATE TABLE schema_migrations") || line.starts_with?("CREATE UNIQUE INDEX unique_schema_migrations")
23
+ ignore_next_line = true
24
+ end
25
+
26
+ if ignore_next_line
27
+ puts "ignoring: #{line}"
28
+ else
29
+ puts " using: #{line}"
30
+ # remove space from end of line and non-unix line-terminators so we can expect ";\n" at the real end of a function, for example
31
+ temp_file.puts "#{line.rstrip}\n"
32
+ if line.starts_with?("CREATE FUNCTION ")
33
+ step1 = line.split(/[\(\)]/).collect{|t| t.strip==t ? t : t.split(' ')}.flatten
34
+ function_name_with_args = "#{step1[0].split.last}(#{step1[1]})"
35
+ drop_body << "DROP FUNCTION #{function_name_with_args} CASCADE;"
36
+ elsif line.starts_with?("CREATE SEQUENCE ")
37
+ name = (line.split)[2]
38
+ if name.starts_with?('"')
39
+ name = (line.split(/"/).collect{|t| t.strip==t ? t : t.split(' ')}.flatten)[2]
40
+ end
41
+ drop_body << "DROP SEQUENCE #{name} CASCADE;"
42
+ elsif line.starts_with?("CREATE TABLE ")
43
+ name = (line.split)[2]
44
+ if name.starts_with?('"')
45
+ name = (line.split(/"/).collect{|t| t.strip==t ? t : t.split(' ')}.flatten)[2]
46
+ end
47
+ drop_body << "DROP TABLE #{name} CASCADE;"
48
+ end
49
+ end
50
+
51
+ ignore_next_line = false if line[';']
52
+ end
53
+ end
54
+ middle = <<-eos1
55
+ eos
56
+ statements.split(";\n").each {|s| execute s}
57
+ end
58
+
59
+ def down
60
+ statements = <<-eos
61
+ eos1
62
+ temp_file.puts middle
63
+ drop_body.reverse.each {|s|temp_file.puts s}
64
+ footer = <<-eos1
65
+ eos
66
+ statements.split(";\n").each {|s| execute s}
67
+ end
68
+ end
69
+ eos1
70
+ temp_file.puts footer
71
+ end
72
+ temp_file.rewind
73
+ FileUtils.mv(temp_file.path, migration_file)
74
+ ensure
75
+ temp_file.close
76
+ temp_file.unlink
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: structure_sql_to_migration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gary S. Weaver
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A rake task to generate a migration with up and down using db/structure.sql
15
+ as input.
16
+ email:
17
+ - garysweaver@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/structure_sql_to_migration/version.rb
23
+ - lib/structure_sql_to_migration.rb
24
+ - lib/tasks/structure_sql_to_migration.rake
25
+ - Rakefile
26
+ - README.md
27
+ homepage: https://github.com/garysweaver/structure_sql_to_migration
28
+ licenses:
29
+ - MIT
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.24
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Converts db/structure.sql into a migration.
52
+ test_files: []