fx 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +15 -13
- data/gemfiles/rails40.gemfile +1 -1
- data/gemfiles/rails40.gemfile.lock +2 -2
- data/gemfiles/rails41.gemfile +1 -1
- data/gemfiles/rails41.gemfile.lock +2 -2
- data/gemfiles/rails42.gemfile +1 -1
- data/gemfiles/rails42.gemfile.lock +2 -2
- data/gemfiles/rails50.gemfile +1 -1
- data/gemfiles/rails50.gemfile.lock +2 -2
- data/gemfiles/rails_edge.gemfile +3 -3
- data/gemfiles/rails_edge.gemfile.lock +2 -2
- data/lib/fx/definition.rb +13 -3
- data/lib/fx/version.rb +1 -1
- data/spec/fx/adapters/postgres/triggers_spec.rb +4 -3
- data/spec/fx/definition_spec.rb +23 -0
- metadata +3 -5
- data/.ruby-version +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4b2ed07756db1d26e35994559994635b2e5bc1e807eda6eb4b132442a3b561ea
|
4
|
+
data.tar.gz: c5d60d5200347b5fc5cdf5b18d232b0662b2f92926cfb816f4bb4a36206c9bb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac363c781155605fc98eac9649d5e49fc3e5a0412ef50f4195e8fa37dcf7bf58d9ac89625e7dd8e482e4ebafc0fbcdc447f7a3d5c97cb2813c6d1a60727e2f1a
|
7
|
+
data.tar.gz: db5c222c9de3b758dbdd21f725cd5740b8ecfe1571ee761de7385cb4e73ec77f8f02284c95b2b115dbf62906d2fa2d6613c6f39535cdef3e62cf1d3419c36765
|
data/README.md
CHANGED
@@ -21,18 +21,29 @@ F(x) ships with support for PostgreSQL. The adapter is configurable (see
|
|
21
21
|
|
22
22
|
## Great, how do I create a trigger and a function?
|
23
23
|
|
24
|
-
You've got this great idea for a
|
24
|
+
You've got this great idea for a function you'd like to call
|
25
25
|
`uppercase_users_name`. You can create the migration and the corresponding
|
26
26
|
definition file with the following command:
|
27
27
|
|
28
28
|
```sh
|
29
|
-
% rails generate fx:
|
29
|
+
% rails generate fx:function uppercase_users_name
|
30
|
+
create db/functions/uppercase_users_name_v01.sql
|
31
|
+
create db/migrate/[TIMESTAMP]_create_function_uppercase_users_name.rb
|
32
|
+
```
|
33
|
+
|
34
|
+
Edit the `db/functions/uppercase_users_name_v01.sql` file with the SQL statement
|
35
|
+
that defines your function.
|
36
|
+
|
37
|
+
Next, let's add a trigger called `uppercase_users_name` to call our new
|
38
|
+
function each time we `INSERT` on the `users` table.
|
39
|
+
|
40
|
+
```sh
|
41
|
+
% rails generate fx:trigger uppercase_users_name table_name:users
|
30
42
|
create db/triggers/uppercase_users_name_v01.sql
|
31
43
|
create db/migrate/[TIMESTAMP]_create_trigger_uppercase_users_name.rb
|
32
44
|
```
|
33
45
|
|
34
|
-
|
35
|
-
that defines your trigger. In our example, this might look something like this:
|
46
|
+
In our example, this might look something like this:
|
36
47
|
|
37
48
|
```sql
|
38
49
|
CREATE TRIGGER uppercase_users_name
|
@@ -41,15 +52,6 @@ CREATE TRIGGER uppercase_users_name
|
|
41
52
|
EXECUTE PROCEDURE uppercase_users_name();
|
42
53
|
```
|
43
54
|
|
44
|
-
As you see, we execute a function called `uppercase_users_name` before each
|
45
|
-
`INSERT` on the `users` table, which is a function we don't have yet.
|
46
|
-
|
47
|
-
```sh
|
48
|
-
% rails generate fx:function uppercase_users_name
|
49
|
-
create db/functions/uppercase_users_name_v01.sql
|
50
|
-
create db/migrate/[TIMESTAMP]_create_function_uppercase_users_name.rb
|
51
|
-
```
|
52
|
-
|
53
55
|
The generated migrations contains `create_function` and `create_trigger`
|
54
56
|
statements. The migration is reversible and the schema will be dumped into your
|
55
57
|
`schema.rb` file.
|
data/gemfiles/rails40.gemfile
CHANGED
data/gemfiles/rails41.gemfile
CHANGED
data/gemfiles/rails42.gemfile
CHANGED
data/gemfiles/rails50.gemfile
CHANGED
data/gemfiles/rails_edge.gemfile
CHANGED
data/lib/fx/definition.rb
CHANGED
@@ -8,7 +8,7 @@ module Fx
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def to_sql
|
11
|
-
File.read(full_path).tap do |content|
|
11
|
+
File.read(find_file || full_path).tap do |content|
|
12
12
|
if content.empty?
|
13
13
|
raise "Define #{@type} in #{path} before migrating."
|
14
14
|
end
|
@@ -20,7 +20,7 @@ module Fx
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def path
|
23
|
-
File.join("db", @type.pluralize, filename)
|
23
|
+
@_path ||= File.join("db", @type.pluralize, filename)
|
24
24
|
end
|
25
25
|
|
26
26
|
def version
|
@@ -30,7 +30,17 @@ module Fx
|
|
30
30
|
private
|
31
31
|
|
32
32
|
def filename
|
33
|
-
"#{@name}_v#{version}.sql"
|
33
|
+
@_filename ||= "#{@name}_v#{version}.sql"
|
34
|
+
end
|
35
|
+
|
36
|
+
def find_file
|
37
|
+
migration_paths.lazy
|
38
|
+
.map { |migration_path| File.expand_path(File.join("..", "..", path), migration_path) }
|
39
|
+
.find { |definition_path| File.exist?(definition_path) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def migration_paths
|
43
|
+
Rails.application.config.paths["db/migrate"].expanded
|
34
44
|
end
|
35
45
|
end
|
36
46
|
end
|
data/lib/fx/version.rb
CHANGED
@@ -34,9 +34,10 @@ module Fx
|
|
34
34
|
first = triggers.first
|
35
35
|
expect(triggers.size).to eq 1
|
36
36
|
expect(first.name).to eq "uppercase_users_name"
|
37
|
-
expect(first.definition).to
|
38
|
-
|
39
|
-
|
37
|
+
expect(first.definition).to include("BEFORE INSERT")
|
38
|
+
expect(first.definition).to match(/ON [public\.users|users]/)
|
39
|
+
expect(first.definition).to include("FOR EACH ROW")
|
40
|
+
expect(first.definition).to include("EXECUTE PROCEDURE uppercase_users_name()")
|
40
41
|
end
|
41
42
|
end
|
42
43
|
end
|
data/spec/fx/definition_spec.rb
CHANGED
@@ -28,6 +28,29 @@ describe Fx::Definition do
|
|
28
28
|
%r(Define function in db/functions/test_v01.sql before migrating),
|
29
29
|
)
|
30
30
|
end
|
31
|
+
|
32
|
+
context "when definition is at Rails engine" do
|
33
|
+
it "returns the content of a function definition" do
|
34
|
+
sql_definition = <<-EOS
|
35
|
+
CREATE OR REPLACE FUNCTION test()
|
36
|
+
RETURNS text AS $$
|
37
|
+
BEGIN
|
38
|
+
RETURN 'test';
|
39
|
+
END;
|
40
|
+
$$ LANGUAGE plpgsql;
|
41
|
+
EOS
|
42
|
+
engine_path = Rails.root.join("tmp", "engine")
|
43
|
+
FileUtils.mkdir_p(engine_path.join("db", "functions"))
|
44
|
+
File.write(engine_path.join("db", "functions", "custom_test_v01.sql"), sql_definition)
|
45
|
+
Rails.application.config.paths["db/migrate"].push(engine_path.join("db", "migrate"))
|
46
|
+
|
47
|
+
definition = Fx::Definition.new(name: "custom_test", version: 1)
|
48
|
+
|
49
|
+
expect(definition.to_sql).to eq sql_definition
|
50
|
+
|
51
|
+
FileUtils.rm_rf(engine_path)
|
52
|
+
end
|
53
|
+
end
|
31
54
|
end
|
32
55
|
|
33
56
|
context "representing a trigger definition" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Teo Ljungberg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: appraisal
|
@@ -191,7 +191,6 @@ files:
|
|
191
191
|
- ".hound.yml"
|
192
192
|
- ".rspec"
|
193
193
|
- ".rubocop.yml"
|
194
|
-
- ".ruby-version"
|
195
194
|
- ".travis.yml"
|
196
195
|
- ".yardopts"
|
197
196
|
- Appraisals
|
@@ -301,8 +300,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
301
300
|
- !ruby/object:Gem::Version
|
302
301
|
version: '0'
|
303
302
|
requirements: []
|
304
|
-
|
305
|
-
rubygems_version: 2.5.2.1
|
303
|
+
rubygems_version: 3.0.3
|
306
304
|
signing_key:
|
307
305
|
specification_version: 4
|
308
306
|
summary: Support for database functions and triggers in Rails migrations
|
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
2.3
|