db_structure 1.0.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.
Files changed (3) hide show
  1. data/USAGE +38 -0
  2. data/lib/db_structure.rb +67 -0
  3. metadata +49 -0
data/USAGE ADDED
@@ -0,0 +1,38 @@
1
+ NAME
2
+ db_structure - rails database mmanagement utilities
3
+
4
+ SYNOPSIS
5
+ db_structure
6
+
7
+ DESCRIPTION
8
+ This gem adds some utilities for managing generic database schemas.
9
+
10
+ EXAMPLE
11
+
12
+ You need to call build_db("path/to/your/rails/app") from a script.
13
+ All schemas in your db directory of the form *.erbsql will be parsed.
14
+ Following is an example schema:
15
+
16
+ CREATE TABLE sample (
17
+ id <%= @pk %>,
18
+ when <%= datetime %>,
19
+ name VARCHAR (60) NOT NULL
20
+ ) <%= options %>;
21
+
22
+ If you are running a MySQL database, this will create the following table:
23
+
24
+ CREATE TABLE sample (
25
+ id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
26
+ when DATETIME,
27
+ name VARCHAR (60) NOT NULL
28
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
29
+
30
+ And for PostgreSQL:
31
+
32
+ CREATE TABLE sample (
33
+ id SERIAL PRIMARY KEY,
34
+ when TIMESTAMP,
35
+ name VARCHAR (60) NOT NULL
36
+ );
37
+
38
+ Additionally, all tables found will be dropped and recreated.
@@ -0,0 +1,67 @@
1
+ require 'erb'
2
+
3
+ module DBStructure
4
+ def self.db_helper(db)
5
+ @db = db
6
+ case db
7
+ when 'postgresql'
8
+ @pk = 'SERIAL PRIMARY KEY'
9
+ @datetime = 'TIMESTAMP'
10
+ @options = ''
11
+ when 'sqlite'
12
+ @pk = 'INTEGER PRIMARY KEY'
13
+ @datetime = 'DATETIME'
14
+ @options = ''
15
+ when 'mysql'
16
+ @pk = 'INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY'
17
+ @datetime = 'DATETIME'
18
+ @options = 'ENGINE=InnoDB DEFAULT CHARSET=utf8'
19
+ else
20
+ raise "Unknown db type #{db}"
21
+ end
22
+
23
+ s = ''
24
+ Dir['db/*.erbsql'].each do |filename|
25
+ s += ERB.new(File.read(filename)).result(binding)
26
+ end
27
+ s
28
+ end
29
+
30
+ def self.db_structure
31
+ config = ActiveRecord::Base.configurations
32
+
33
+ ['production', 'test', 'development'].each do |target|
34
+ begin
35
+ ENV['RAILS_ENV'] = target
36
+ db_helper(config[target]['adapter']).split(/\s*;\s*/).each do |sql|
37
+ begin
38
+ puts "Recreating database #{target}"
39
+ ActiveRecord::Base.connection.execute("DROP DATABASE #{target}")
40
+ rescue
41
+ puts "Database #{target} doesn't exist yet"
42
+ ensure
43
+ ActiveRecord::Base.connection.execute("CREATE DATABASE #{target}")
44
+ end
45
+ Dir['db/*.erbsql'].each do |filename|
46
+ File.open(filename, "r") do |file|
47
+ file.each_line do |line|
48
+ if line =~ /(create|CREATE).+?(table|TABLE).+?(\w+)/
49
+ begin
50
+ ActiveRecord::Base.connection.execute("DROP TABLE #{$3}")
51
+ rescue
52
+ puts "Table #{$3} does not exist"
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ puts "Creating tables for #{target}..."
59
+ ActiveRecord::Base.connection.execute(sql)
60
+ end
61
+ puts "done."
62
+ rescue => e
63
+ puts "failed: " + e.inspect
64
+ end
65
+ end
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.8
3
+ specification_version: 1
4
+ name: db_structure
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2005-04-19
8
+ summary: "[Rails] Database utilities."
9
+ require_paths:
10
+ - lib
11
+ email: jhosteny@mac.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description: Database utility for Rails apps
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ authors:
28
+ - Andreas Schwarz
29
+ - Joe Hosteny
30
+ files:
31
+ - USAGE
32
+ - lib/db_structure.rb
33
+ test_files: []
34
+ rdoc_options: []
35
+ extra_rdoc_files: []
36
+ executables: []
37
+ extensions: []
38
+ requirements: []
39
+ dependencies:
40
+ - !ruby/object:Gem::Dependency
41
+ name: rails
42
+ version_requirement:
43
+ version_requirements: !ruby/object:Gem::Version::Requirement
44
+ requirements:
45
+ -
46
+ - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 0.10.0
49
+ version: