fx-sequence 0.1.0 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 48e0687b9cfd59028be9a254da0c216d94e9bf56299b3ace7447c43b345598d5
4
- data.tar.gz: 3db24cfcdc2f2ee9b9f119a2a4669d9f1cb2f5778765bf67c542e4148daf6b7e
3
+ metadata.gz: d73385a8621ac337e099f90e7efb1764454f341b123feb1f06890869caeb5072
4
+ data.tar.gz: 2ff5c8fc13c5d292d59045afc7c3efbba8502667134cc52c91e51e11c8eebdd0
5
5
  SHA512:
6
- metadata.gz: ced25a9fd8d9936b34ec9492dccde512776b387c3973aa565ca7964a2d68e3d6e94d83d926980bbe029fbe64c2bfb9fa8c524ad11dccc39538f968b071536ebf
7
- data.tar.gz: ad75dd0639222d689bfcc84fade05181343d4739a4aa03e7918e215dc2d1e66f3b2b6e87e69472f4155ea2989093eeb0e73276561f511ec6b95f063b7097cba9
6
+ metadata.gz: 1b4dcace4770cf2fcc7c6313da8e4368a867f61b0ea884d9b0b0540359e872a8098eb846448fdc4af8de33531fa8c4c3a3467c1e96f859d318eee8d4f10b3cf5
7
+ data.tar.gz: b15a6e8b586296fab214a742b18b9ab6e49181140f9797bc3a2c573c8c0595eba16e0491a0c54bd65c8e95c1a08a39378299ee1f9dfeec1d1376f2a43637d14f
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ ## [0.1.2]
4
+
5
+ [0.1.2]: https://github.com/aevula/fx-sequence/compare/v0.1.1...v0.1.2
6
+
7
+ - Rewrote SQL-query to ignore auto-generated sequences for tables primary keys
8
+
9
+ ## [0.1.1]
10
+
11
+ [0.1.1]: https://github.com/aevula/fx-sequence/compare/v0.1.0...v0.1.1
12
+
13
+ - Added rails generators
14
+
15
+ ## [0.1.0]
16
+
17
+ Extension for [fx](https://github.com/aevula/fx-sequence) gem to support creation, drop and dumping to `schema.rb` PostgreSQL sequences.
@@ -9,11 +9,27 @@ module Fx
9
9
  class Sequences
10
10
  SEQUENCES_WITH_DEFINITIONS_QUERY = <<~SQL
11
11
  SELECT ps.relname AS name,
12
- 'CREATE SEQUENCE IF NOT EXISTS ' || ps.relname AS definition
12
+ 'CREATE SEQUENCE IF NOT EXISTS ' || ps.relname AS definition
13
13
  FROM pg_class ps
14
- JOIN pg_namespace pn ON ps.relnamespace = pn.oid
15
- WHERE pn.nspname = 'public'
16
- AND ps.relkind = 'S';
14
+ JOIN pg_namespace pn ON
15
+ ps.relnamespace = pn.oid
16
+ LEFT JOIN pg_depend pd ON
17
+ pd.objid = ps.oid AND
18
+ pd.classid = 'pg_class'::regclass AND
19
+ pd.deptype = 'a'
20
+ LEFT JOIN pg_class pt ON
21
+ pd.refobjid = pt.oid
22
+ LEFT JOIN pg_attribute pa ON
23
+ pa.attrelid = pt.oid AND
24
+ pa.attnum = pd.refobjsubid
25
+ LEFT JOIN pg_constraint pc ON
26
+ pc.conrelid = pt.oid AND
27
+ pa.attnum = ANY(pc.conkey) AND
28
+ pc.contype = 'p'
29
+ WHERE
30
+ pn.nspname = 'public' AND
31
+ ps.relkind = 'S' AND
32
+ pc.oid IS NULL;
17
33
  SQL
18
34
  private_constant :SEQUENCES_WITH_DEFINITIONS_QUERY
19
35
 
@@ -10,6 +10,10 @@ module Fx
10
10
  def drop_sequence(*args)
11
11
  record(:drop_sequence, args)
12
12
  end
13
+
14
+ def invert_create_function(args)
15
+ [:drop_function, args]
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Fx
4
4
  module Sequence
5
- VERSION = '0.1.0'
5
+ VERSION = '0.1.2'
6
6
  public_constant :VERSION
7
7
  end
8
8
  end
@@ -0,0 +1,17 @@
1
+ Description:
2
+ Create a new database sequence for your application. This will create a new
3
+ sequence definition file and the accompanying migration.
4
+
5
+ When --no-migration is passed, skips generating a migration.
6
+ When --exact-name is passed, uses exact name without "seq" suffix.
7
+
8
+ Examples:
9
+ rails generate fx:sequence numbers
10
+
11
+ create: db/sequences/numbers_seq_v01.sql
12
+ create: db/migrate/[TIMESTAMP]_create_sequence_numbers_seq.rb
13
+
14
+ rails generate fx:sequence numbers --exact-name
15
+
16
+ create: db/sequences/numbers_v01.sql
17
+ create: db/migrate/[TIMESTAMP]_create_sequence_numbers.rb
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require 'rails/generators/active_record'
5
+
6
+ module Fx
7
+ module Generators
8
+ class SequenceGenerator < Rails::Generators::NamedBase
9
+ include Rails::Generators::Migration
10
+
11
+ source_root File.expand_path('templates', __dir__)
12
+
13
+ class_option :migration, type: :boolean
14
+ class_option :exact_name, type: :boolean, default: false, description: 'Use exact name without \'seq\' suffix'
15
+
16
+ def self.next_migration_number(dir)
17
+ ::ActiveRecord::Generators::Base.next_migration_number(dir)
18
+ end
19
+
20
+ def create_sequences_directory
21
+ return if sequence_definition_path.exist?
22
+
23
+ empty_directory(sequence_definition_path)
24
+ end
25
+
26
+ def create_sequence_definition
27
+ if creating_new_sequence?
28
+ create_file(definition.path, default_sql_definition)
29
+ else
30
+ copy_file(previous_definition.full_path, definition.full_path)
31
+ end
32
+ end
33
+
34
+ def create_migration_file
35
+ return if skip_migration_creation?
36
+
37
+ migration_template('db/migrate/create_sequence.erb', "db/migrate/create_sequence_#{sequence_name}.rb")
38
+ end
39
+
40
+ no_tasks do
41
+ def previous_version
42
+ @previous_version ||= Dir.entries(sequence_definition_path)
43
+ .map { |name| extract_version(name) }
44
+ .max
45
+ end
46
+
47
+ def version
48
+ @version ||= previous_version.next
49
+ end
50
+
51
+ def activerecord_migration_class
52
+ if ActiveRecord::Migration.respond_to?(:current_version)
53
+ "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
54
+ else
55
+ 'ActiveRecord::Migration'
56
+ end
57
+ end
58
+
59
+ def formatted_name
60
+ name = singular_name.include?('.') ? "\"#{singular_name}\"" : ":#{singular_name}"
61
+ sequence_name(name)
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ def sequence_definition_path
68
+ @sequence_definition_path ||= Rails.root.join(*%w[db sequences])
69
+ end
70
+
71
+ def version_regex; end
72
+
73
+ def extract_version(name)
74
+ Integer(/\A#{sequence_name}_v(?<version>\d+)\.sql\z/.match(name).try(:[], 'version'), 10)
75
+ rescue ArgumentError
76
+ 0
77
+ end
78
+
79
+ def creating_new_sequence?
80
+ previous_version.zero?
81
+ end
82
+
83
+ def definition
84
+ Fx::Sequence::Definition.sequence(name: sequence_name, version: version)
85
+ end
86
+
87
+ def previous_definition
88
+ Fx::Sequence::Definition.sequence(name: sequence_name, version: previous_version)
89
+ end
90
+
91
+ def skip_migration_creation?
92
+ !migration
93
+ end
94
+
95
+ def migration
96
+ options[:migration] != false
97
+ end
98
+
99
+ def default_sql_definition
100
+ <<~SQL
101
+ CREATE SEQUENCE #{sequence_name};
102
+ SQL
103
+ end
104
+
105
+ def sequence_name(name = file_name)
106
+ options[:exact_name] ? name : "#{name}_seq"
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= migration_class_name %> < <%= activerecord_migration_class %>
4
+ def change
5
+ create_sequence(<%= formatted_name %>)
6
+ end
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fx-sequence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - "@aevula"
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-05 00:00:00.000000000 Z
11
+ date: 2025-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fx
@@ -30,6 +30,7 @@ executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
+ - CHANGELOG.md
33
34
  - README.md
34
35
  - Rakefile
35
36
  - lib/fx/sequence.rb
@@ -43,6 +44,9 @@ files:
43
44
  - lib/fx/sequence/sequence.rb
44
45
  - lib/fx/sequence/statements.rb
45
46
  - lib/fx/sequence/version.rb
47
+ - lib/generators/fx/sequence/USAGE
48
+ - lib/generators/fx/sequence/sequence_generator.rb
49
+ - lib/generators/fx/sequence/templates/db/migrate/create_sequence.erb
46
50
  homepage: https://github.com/aevula/fx-sequence
47
51
  licenses:
48
52
  - MIT