public
Description: All the extra stuff you could want for the Mack Framework.
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack-more.git
The beginnings of adding proper db:structure:dump and db:structure:load tasks. 
[#67]
markbates (author)
Fri Aug 08 14:45:24 -0700 2008
commit  2263caf43ab79d61970f3e7148dafc39ffbaa0ff
tree    1afcaca76ec16e525181bcc814d6bcb173d5fb04
parent  142b967dcb082b9fcfabf4ea8da85904c3ab2917
...
27
28
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
31
32
...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
0
@@ -27,6 +27,64 @@ module Mack
0
       Mack::Database.establish_connection(env)
0
       drop_database(repis)
0
     end
0
+
0
+    # abcs = ActiveRecord::Base.configurations
0
+    # case abcs[RAILS_ENV]["adapter"]
0
+    # when "mysql", "oci", "oracle"
0
+    #   ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
0
+    #   File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
0
+    # when "postgresql"
0
+    #   ENV['PGHOST']     = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"]
0
+    #   ENV['PGPORT']     = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"]
0
+    #   ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"]
0
+    #   search_path = abcs[RAILS_ENV]["schema_search_path"]
0
+    #   search_path = "--schema=#{search_path}" if search_path
0
+    #   `pg_dump -i -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{search_path} #{abcs[RAILS_ENV]["database"]}`
0
+    #   raise "Error dumping database" if $?.exitstatus == 1
0
+    # when "sqlite", "sqlite3"
0
+    #   dbfile = abcs[RAILS_ENV]["database"] || abcs[RAILS_ENV]["dbfile"]
0
+    #   `#{abcs[RAILS_ENV]["adapter"]} #{dbfile} .schema > db/#{RAILS_ENV}_structure.sql`
0
+    # when "sqlserver"
0
+    #   `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r`
0
+    #   `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r`
0
+    # when "firebird"
0
+    #   set_firebird_env(abcs[RAILS_ENV])
0
+    #   db_string = firebird_db_string(abcs[RAILS_ENV])
0
+    #   sh "isql -a #{db_string} > db/#{RAILS_ENV}_structure.sql"
0
+    # else
0
+    #   raise "Task not supported by '#{abcs["test"]["adapter"]}'"
0
+    # end
0
+    
0
+    def self.structure_dump(env = Mack.env, repis = :default)
0
+      adapter = repository(repis).adapter
0
+      uri = adapter.uri
0
+      structure = ""
0
+      output_file = File.join(Mack.root, "db", "#{env}_#{repis}_schema_structure.sql")
0
+      case adapter.class.name
0
+        when /Mysql/
0
+          sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
0
+          sql = "SHOW TABLES"
0
+          adapter.query(sql).each do |res|
0
+            show = adapter.query("SHOW CREATE TABLE #{res}").first
0
+            structure += show.attributes["create table".to_sym]
0
+            structure += ";\n\n"
0
+          end
0
+          # puts structure
0
+          File.open(output_file, "w") {|f| f.puts structure}
0
+        when /Postgres/
0
+          `pg_dump -i -U "#{uri.user}" -s -x -O -f #{output_file} #{uri.basename}`
0
+          # setup_temp(uri, "postgres")
0
+          # repository(:tmp) do |repo|
0
+          #   puts "Creating (PostgreSQL): #{uri.basename}"
0
+          #   repo.adapter.execute "CREATE DATABASE #{uri.basename} ENCODING = 'utf8'"
0
+          # end
0
+        when /Sqlite3/
0
+          db_dir = File.join(Mack.root, "db")
0
+          `sqlite3 #{File.join(db_dir, uri.basename)} .schema > #{output_file}`
0
+        else
0
+          raise "Task not supported for '#{repository(repis).adapter.class.name}'"
0
+      end
0
+    end
0
     
0
     private
0
     def self.setup_temp(uri, adapter)
...
37
38
39
 
 
 
 
 
 
 
 
 
40
41
42
...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
0
@@ -37,6 +37,15 @@ namespace :db do
0
     DataMapper::MigrationRunner.migrations.clear
0
   end
0
   
0
+  namespace :structure do
0
+    
0
+    desc "Dump the database structure to a SQL file"
0
+    task :dump => "mack:environment" do
0
+      Mack::Database.structure_dump
0
+    end
0
+    
0
+  end
0
+  
0
   private
0
   
0
   def migration_files

Comments