tern 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -178,7 +178,39 @@ Multiple sequences may be specified and they will be run in the order they are
178
178
  listed.
179
179
 
180
180
  tern migrate --definition-sequences=expensive default
181
+
182
+ ERB
183
+ ===
181
184
 
185
+ Alterations and definitions are run through ERB. Subtemplates may be
186
+ included with the render_file method. Instance variables will be
187
+ available in the subtemplates.
188
+
189
+ definitions/render_file.sql
190
+
191
+ <% @num_from_outer_template = 7 %>
192
+ <%= render_file "definitions/rendered_function.sql" %>
193
+
194
+ definitions/rendered_function.sql
195
+
196
+ CREATE FUNCTION rendered_function() RETURNS integer AS $$
197
+ SELECT <%= @num_from_outer_template %>;
198
+ $$ LANGUAGE SQL;
199
+
200
+ ---- CREATE above / DROP below ----
201
+
202
+ DROP FUNCTION rendered_function();
203
+
204
+ This will result in:
205
+
206
+ CREATE FUNCTION rendered_function() RETURNS integer AS $$
207
+ SELECT 7;
208
+ $$ LANGUAGE SQL;
209
+
210
+ ---- CREATE above / DROP below ----
211
+
212
+ DROP FUNCTION rendered_function();
213
+
182
214
  License
183
215
  =======
184
216
 
data/lib/tern.rb CHANGED
@@ -1,11 +1,23 @@
1
1
  require 'sequel'
2
2
  require 'yaml'
3
+ require 'erb'
4
+
5
+ class Parser
6
+ def render_text(text)
7
+ ERB.new(text).result(binding)
8
+ end
9
+
10
+ def render_file(file_path)
11
+ string = File.read file_path
12
+ render_text(string)
13
+ end
14
+ end
3
15
 
4
16
  class Change
5
17
  SPLIT_MARKER = '---- CREATE above / DROP below ----'
6
18
 
7
19
  def self.parse(string)
8
- create_sql, drop_sql = string.split('---- CREATE above / DROP below ----')
20
+ create_sql, drop_sql = Parser.new.render_text(string).split(SPLIT_MARKER)
9
21
  [create_sql, drop_sql]
10
22
  end
11
23
  end
@@ -0,0 +1,16 @@
1
+ Sequel.migration do
2
+ up do
3
+ run <<-END_SQL
4
+ CREATE TABLE people(
5
+ id serial PRIMARY KEY,
6
+ name varchar NOT NULL
7
+ );
8
+ END_SQL
9
+ end
10
+
11
+ down do
12
+ run <<-END_SQL
13
+ DROP TABLE people;
14
+ END_SQL
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ alterations:
2
+ table: tern_alterations
3
+ column: version
4
+ definitions:
5
+ table: tern_definitions
6
+ environments:
7
+ development:
8
+ adapter: postgres
9
+ database: tern_test_development
10
+ test:
11
+ adapter: postgres
12
+ database: tern_test_test
13
+ production:
14
+ adapter: postgres
15
+ database: tern_test_production
@@ -0,0 +1,2 @@
1
+ <% @num_from_outer_template = 7 %>
2
+ <%= render_file "definitions/rendered_function.sql" %>
@@ -0,0 +1,7 @@
1
+ CREATE FUNCTION rendered_function() RETURNS integer AS $$
2
+ SELECT <%= @num_from_outer_template %>
3
+ $$ LANGUAGE SQL;
4
+
5
+ ---- CREATE above / DROP below ----
6
+
7
+ DROP FUNCTION rendered_function();
@@ -0,0 +1,20 @@
1
+ # Put the relative path to your definition files in the sequence they should run.
2
+ #
3
+ # Most definitions should go in the default list. These will be dropped and
4
+ # recreated every migration.
5
+ #
6
+ # Definitions that should not automatically be dropped and created can be put in
7
+ # different lists. This is useful for definitions that make take prohibitively
8
+ # to drop and create every migration such as check constraints.
9
+ #
10
+ # Run alternative sequences by specifying sequences in order to migrate command.
11
+ # Example: rake migrate sequences=expensive,default
12
+ #
13
+ # default:
14
+ # - ultimate_answer.sql
15
+ # expensive:
16
+ # - super_slow_check_constraint.sql
17
+
18
+ default:
19
+ - ultimate_answer.sql
20
+ - render_file.sql
@@ -0,0 +1,7 @@
1
+ CREATE FUNCTION ultimate_answer() RETURNS integer AS $$
2
+ SELECT <%= 7*6 %>
3
+ $$ LANGUAGE SQL;
4
+
5
+ ---- CREATE above / DROP below ----
6
+
7
+ DROP FUNCTION ultimate_answer();
data/spec/tern_spec.rb CHANGED
@@ -181,6 +181,16 @@ describe "tern" do
181
181
  tern_migrate "spec/projects/definitions"
182
182
  @dev_db.get{ultimate_answer{}}.should == 42
183
183
  end
184
+
185
+ it "runs definitions through erb" do
186
+ tern_migrate "spec/projects/erb"
187
+ @dev_db.get{ultimate_answer{}}.should == 42
188
+ end
189
+
190
+ it "runs definitions through erb - render_file" do
191
+ tern_migrate "spec/projects/erb"
192
+ @dev_db.get{rendered_function{}}.should == 7
193
+ end
184
194
 
185
195
  it "drops definitions" do
186
196
  tern_migrate "spec/projects/definitions"
data/tern.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "tern"
6
- s.version = '0.6.1'
6
+ s.version = '0.7.0'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Jack Christensen"]
9
9
  s.email = ["jack@jackchristensen.com"]
metadata CHANGED
@@ -1,8 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tern
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.6.1
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 7
8
+ - 0
9
+ version: 0.7.0
6
10
  platform: ruby
7
11
  authors:
8
12
  - Jack Christensen
@@ -10,7 +14,7 @@ autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
16
 
13
- date: 2011-02-22 00:00:00 -06:00
17
+ date: 2011-04-06 00:00:00 -05:00
14
18
  default_executable:
15
19
  dependencies:
16
20
  - !ruby/object:Gem::Dependency
@@ -21,6 +25,10 @@ dependencies:
21
25
  requirements:
22
26
  - - ">="
23
27
  - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 19
31
+ - 0
24
32
  version: 3.19.0
25
33
  type: :runtime
26
34
  version_requirements: *id001
@@ -32,6 +40,10 @@ dependencies:
32
40
  requirements:
33
41
  - - ">="
34
42
  - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ - 14
46
+ - 6
35
47
  version: 0.14.6
36
48
  type: :runtime
37
49
  version_requirements: *id002
@@ -43,6 +55,10 @@ dependencies:
43
55
  requirements:
44
56
  - - ">="
45
57
  - !ruby/object:Gem::Version
58
+ segments:
59
+ - 2
60
+ - 4
61
+ - 0
46
62
  version: 2.4.0
47
63
  type: :development
48
64
  version_requirements: *id003
@@ -94,6 +110,12 @@ files:
94
110
  - spec/projects/duplicated_alteration/config.yml
95
111
  - spec/projects/duplicated_alteration/definitions/sequence.yml
96
112
  - spec/projects/duplicated_alteration/definitions/ultimate_answer.sql.example
113
+ - spec/projects/erb/alterations/001_create_people.sql.example
114
+ - spec/projects/erb/config.yml
115
+ - spec/projects/erb/definitions/render_file.sql
116
+ - spec/projects/erb/definitions/rendered_function.sql
117
+ - spec/projects/erb/definitions/sequence.yml
118
+ - spec/projects/erb/definitions/ultimate_answer.sql
97
119
  - spec/projects/irreversible_alterations/alterations/001_create_people.sql
98
120
  - spec/projects/irreversible_alterations/alterations/002_create_animals.sql
99
121
  - spec/projects/irreversible_alterations/alterations/003_create_plants.sql
@@ -136,17 +158,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
136
158
  requirements:
137
159
  - - ">="
138
160
  - !ruby/object:Gem::Version
161
+ segments:
162
+ - 0
139
163
  version: "0"
140
164
  required_rubygems_version: !ruby/object:Gem::Requirement
141
165
  none: false
142
166
  requirements:
143
167
  - - ">="
144
168
  - !ruby/object:Gem::Version
169
+ segments:
170
+ - 0
145
171
  version: "0"
146
172
  requirements: []
147
173
 
148
174
  rubyforge_project:
149
- rubygems_version: 1.5.2
175
+ rubygems_version: 1.3.7
150
176
  signing_key:
151
177
  specification_version: 3
152
178
  summary: The SQL Fan's Migrator