view-model 0.0.1

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 (6) hide show
  1. data/LICENSE +19 -0
  2. data/README +27 -0
  3. data/Rakefile +26 -0
  4. data/init.rb +1 -0
  5. data/lib/view_model.rb +105 -0
  6. metadata +57 -0
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Joachim Glauche
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,27 @@
1
+ = ViewModel
2
+
3
+ Some model magic for ActiveRecord Models & Postgresql 8.x
4
+
5
+ Usage:
6
+ In your Model:
7
+
8
+ class MyView < ActiveRecord::Base
9
+
10
+ include ViewModel
11
+
12
+ @view_definition = "SELECT * from table1 join table2 on table2.id = table1.test_id"
13
+
14
+ end
15
+
16
+ = Migrate this model:
17
+
18
+ # MyView.migrate!
19
+
20
+ Warning: There is no automatic versioning support and no fallback functionality. It is highly recommended
21
+ to store changes in SVN or similar.
22
+
23
+
24
+ = Depending Views:
25
+
26
+ If you have depending views which would block a "DROP VIEW", ViewModel automatically searches the models,
27
+ checks if they have a view_definition, drops them and recreates them after the current view is migrated.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ spec = Gem::Specification.new do |s|
5
+ s.name = "view-model"
6
+ s.version = "0.0.1"
7
+ s.author = "Joachim Glauche"
8
+ s.email = "jg@connection-net.de"
9
+ s.homepage = "http://rubyforge.org/projects/view-model/"
10
+ s.rubyforge_project = "view_model"
11
+ s.platform = Gem::Platform::RUBY
12
+ s.summary = "Plugin for ActiveRecord models (Postgresql 8.x only atm) which provides an easy way to
13
+ migrate and re-create database views."
14
+ s.files = FileList["{lib}/**/*"].to_a + ["init.rb", "LICENSE", "README", "Rakefile"]
15
+ s.require_path = "lib"
16
+
17
+ # s.test_files = FileList["{test}/**/*test.rb"].to_a
18
+ s.has_rdoc = true
19
+ s.extra_rdoc_files = ["README"]
20
+ end
21
+
22
+ Rake::GemPackageTask.new(spec) do |pkg|
23
+ pkg.need_tar = true
24
+ end
25
+
26
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "view_model"
data/lib/view_model.rb ADDED
@@ -0,0 +1,105 @@
1
+ module ViewModel
2
+
3
+ def self.included(model)
4
+ model.extend self
5
+ end
6
+
7
+ # obsolete!
8
+ # If in any case you have other views depending on this view,
9
+ # define them in depending_views.
10
+ # If the current view is migrated, it will drop the views depending on this one.
11
+ #
12
+ # TODO: find a better solution to store this information.
13
+ # def depending_views(*opts)
14
+ # @view_dependencies ||= []
15
+ # unless opts.kind_of? Array
16
+ # @view_dependencies << get_model(opts)
17
+ # else
18
+ # @view_dependencies += opts.map{|o| get_model(o)}
19
+ # end
20
+ # end
21
+ # alias :depending_view :depending_views
22
+ #
23
+ def get_model(sym)
24
+ sym.to_s.classify.constantize
25
+ end
26
+
27
+ def view_definition(sql)
28
+ @view_definition = sql
29
+ end
30
+
31
+ def execute(sql)
32
+ connection.execute(sql)
33
+ end
34
+
35
+ def check_dependencies
36
+ return if @view_dependencies == nil
37
+ @view_dependencies.each do |dep|
38
+ if dep.view_definition_set? == false
39
+ raise "Cannot migrate: #{dep} does not have a view_definition set."
40
+ end
41
+ end
42
+ end
43
+
44
+ def drop_dependencies!
45
+ return if @view_dependencies == nil
46
+ @view_dependencies.each do |dep|
47
+ dep.drop_view!
48
+ end
49
+ end
50
+
51
+ def create_dependencies!
52
+ return if @view_dependencies == nil
53
+ @view_dependencies.each do |dep|
54
+ dep.create_view!
55
+ end
56
+ end
57
+
58
+ def view_definition_set?
59
+ @view_definition != nil
60
+ end
61
+
62
+ # checks Postgresql relations for any view that uses current view.
63
+ def get_dependencies
64
+ sql = "select distinct(cl2.relname) from pg_depend dep
65
+ join pg_class cl ON dep.refobjid = cl.oid
66
+ join pg_rewrite on dep.objid = pg_rewrite.oid
67
+ join pg_class cl2 on pg_rewrite.ev_class = cl2.oid
68
+ where cl.relname='#{table_name}' and cl2.relname != '#{table_name}' and deptype='n' "
69
+ result = execute(sql)
70
+ @view_dependencies = []
71
+ result.each do |row|
72
+ @view_dependencies << get_model(row["relname"])
73
+ end
74
+ if @view_dependencies.size > 0
75
+ puts "found dependencies: "
76
+ puts @view_dependencies.inspect
77
+ end
78
+
79
+ end
80
+
81
+ def migrate!
82
+ raise "Please provide a view_definition inside the model" if !view_definition_set?
83
+ get_dependencies
84
+ check_dependencies
85
+ drop_dependencies!
86
+ drop_view!
87
+ create_view!
88
+ create_dependencies!
89
+ true
90
+ end
91
+
92
+ def create_view!
93
+ puts "creating view #{table_name}"
94
+ sql = "CREATE OR REPLACE VIEW #{table_name} AS "
95
+ sql << @view_definition
96
+ execute(sql)
97
+ end
98
+
99
+ def drop_view!
100
+ puts "dropping view #{table_name}"
101
+ execute("DROP VIEW if exists #{table_name}")
102
+ end
103
+
104
+ end
105
+
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: view-model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joachim Glauche
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-12 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: jg@connection-net.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - lib/view_model.rb
26
+ - init.rb
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ has_rdoc: true
31
+ homepage: http://rubyforge.org/projects/view-model/
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project: view_model
52
+ rubygems_version: 1.3.1
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: Plugin for ActiveRecord models (Postgresql 8.x only atm) which provides an easy way to migrate and re-create database views.
56
+ test_files: []
57
+