activerecord-dbt 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +192 -106
  3. data/lib/active_record/dbt/column/column.rb +4 -13
  4. data/lib/active_record/dbt/column/testable/accepted_values_testable.rb +3 -7
  5. data/lib/active_record/dbt/column/testable/not_null_testable.rb +9 -7
  6. data/lib/active_record/dbt/column/testable/relationships_testable.rb +4 -8
  7. data/lib/active_record/dbt/column/testable/unique_testable.rb +2 -6
  8. data/lib/active_record/dbt/config.rb +3 -30
  9. data/lib/active_record/dbt/configuration/dwh_platform.rb +39 -0
  10. data/lib/active_record/dbt/configuration/i18n_configuration.rb +14 -0
  11. data/lib/active_record/dbt/configuration/source.rb +40 -0
  12. data/lib/active_record/dbt/data_type/mapper.rb +103 -0
  13. data/lib/active_record/dbt/dbt_package/dbt_utils/table/testable/unique_combination_of_columns_testable.rb +18 -11
  14. data/lib/active_record/dbt/dbt_package/dbterd/column/testable/relationships_meta_relationship_type.rb +22 -19
  15. data/lib/active_record/dbt/factory/model/staging_factory.rb +3 -3
  16. data/lib/active_record/dbt/factory/source_factory.rb +1 -2
  17. data/lib/active_record/dbt/i18n_wrapper/translate.rb +34 -0
  18. data/lib/active_record/dbt/model/staging/yml.rb +10 -6
  19. data/lib/active_record/dbt/required_methods.rb +17 -0
  20. data/lib/active_record/dbt/seed/enum/base.rb +48 -0
  21. data/lib/active_record/dbt/seed/enum/csv.rb +85 -0
  22. data/lib/active_record/dbt/seed/enum/yml.rb +128 -0
  23. data/lib/active_record/dbt/source/yml.rb +6 -2
  24. data/lib/active_record/dbt/table/yml.rb +1 -4
  25. data/lib/active_record/dbt/version.rb +1 -1
  26. data/lib/active_record/dbt.rb +0 -2
  27. data/lib/generators/active_record/dbt/config/templates/source_config.yml.tt +3 -0
  28. data/lib/generators/active_record/dbt/enum/USAGE +9 -0
  29. data/lib/generators/active_record/dbt/enum/enum_generator.rb +31 -0
  30. data/lib/generators/active_record/dbt/initializer/templates/dbt.rb +1 -0
  31. data/lib/generators/active_record/dbt/staging_model/staging_model_generator.rb +2 -2
  32. metadata +13 -2
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Dbt
5
+ module Seed
6
+ module Enum
7
+ class Yml
8
+ include ActiveRecord::Dbt::Column::Testable::UniqueTestable
9
+ include ActiveRecord::Dbt::Column::Testable::NotNullTestable
10
+ include ActiveRecord::Dbt::DataType::Mapper
11
+ include ActiveRecord::Dbt::I18nWrapper::Translate
12
+ include ActiveRecord::Dbt::Seed::Enum::Base
13
+
14
+ delegate :source_config, to: :@config
15
+
16
+ alias column_name enum_column_name
17
+
18
+ def export_path
19
+ "#{basename}.yml"
20
+ end
21
+
22
+ def dump
23
+ YAML.dump(seed_config.deep_stringify_keys)
24
+ end
25
+
26
+ private
27
+
28
+ def seed_config
29
+ {
30
+ 'version' => 2,
31
+ 'seeds' => [
32
+ 'name' => seed_name,
33
+ 'description' => seed_description,
34
+ 'config' => {
35
+ 'column_types' => column_types
36
+ }
37
+ ],
38
+ 'columns' => columns
39
+ }
40
+ end
41
+
42
+ def seed_description
43
+ default_seed_description ||
44
+ "#{source_name} #{translated_table_name} #{translated_attribute_name} enum".strip
45
+ end
46
+
47
+ def default_seed_description
48
+ source_config.dig(:defaults, :seed_descriptions, :enum, :description)
49
+ &.gsub(/{{\s*source_name\s*}}/, source_name)
50
+ &.gsub(/{{\s*translated_table_name\s*}}/, translated_table_name)
51
+ &.gsub(/{{\s*translated_attribute_name\s*}}/, translated_attribute_name)
52
+ end
53
+
54
+ def column_types
55
+ {
56
+ "#{enum_column_name}_before_type_of_cast" => data_type(before_type_of_cast_type),
57
+ "#{enum_column_name}_key" => data_type(:string),
58
+ **enum_column_types
59
+ }
60
+ end
61
+
62
+ def before_type_of_cast_type
63
+ application_record_klass.columns_hash[enum_column_name].type
64
+ end
65
+
66
+ def enum_column_types
67
+ locales.each_with_object({}) do |locale, hash|
68
+ hash["#{enum_column_name}_#{locale}"] = data_type(:string)
69
+ end
70
+ end
71
+
72
+ def columns
73
+ [
74
+ before_type_of_cast_column,
75
+ enum_key_column,
76
+ *enum_columns
77
+ ].compact
78
+ end
79
+
80
+ def before_type_of_cast_column
81
+ {
82
+ 'name' => "#{enum_column_name}_before_type_of_cast",
83
+ 'description' => translated_attribute_name,
84
+ 'tests' => tests
85
+ }.compact
86
+ end
87
+
88
+ def enum_key_column
89
+ {
90
+ 'name' => "#{enum_column_name}_key",
91
+ 'description' => "#{translated_attribute_name}(key)",
92
+ 'tests' => tests
93
+ }.compact
94
+ end
95
+
96
+ def enum_columns
97
+ locales.each_with_object([]) do |locale, array|
98
+ array.push(
99
+ {
100
+ 'name' => "#{enum_column_name}_#{locale}",
101
+ 'description' => "#{translated_attribute_name}(#{locale})",
102
+ 'tests' => tests
103
+ }.compact
104
+ )
105
+ end
106
+ end
107
+
108
+ def tests
109
+ [
110
+ unique_test,
111
+ not_null_test
112
+ ].compact.presence
113
+ end
114
+
115
+ # MEMO: I think all enums are unique.
116
+ def unique?
117
+ true
118
+ end
119
+
120
+ # MEMO: I think all enums are null.
121
+ def null?
122
+ false
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -13,6 +13,12 @@ module ActiveRecord
13
13
  @config = ActiveRecord::Dbt::Config.instance
14
14
  end
15
15
 
16
+ def dump
17
+ YAML.dump(config.deep_stringify_keys)
18
+ end
19
+
20
+ private
21
+
16
22
  def config
17
23
  {
18
24
  'version' => 2,
@@ -22,8 +28,6 @@ module ActiveRecord
22
28
  }
23
29
  end
24
30
 
25
- private
26
-
27
31
  def source_properties
28
32
  source_config[:sources]
29
33
  end
@@ -4,6 +4,7 @@ module ActiveRecord
4
4
  module Dbt
5
5
  module Table
6
6
  class Yml
7
+ include ActiveRecord::Dbt::I18nWrapper::Translate
7
8
  include ActiveRecord::Dbt::Table::Base
8
9
 
9
10
  attr_reader :table_test, :columns
@@ -51,10 +52,6 @@ module ActiveRecord
51
52
  "Write a logical_name of the '#{table_name}' table."
52
53
  end
53
54
 
54
- def translated_table_name
55
- I18n.t("activerecord.models.#{table_name.singularize}", default: nil)
56
- end
57
-
58
55
  def default_logical_name
59
56
  source_config.dig(:defaults, :table_descriptions, :logical_name)
60
57
  &.gsub(/{{\s*table_name\s*}}/, table_name)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Dbt
5
- VERSION = '0.1.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
@@ -11,7 +11,5 @@ module ActiveRecord
11
11
 
12
12
  config
13
13
  end
14
-
15
- class Error < StandardError; end
16
14
  end
17
15
  end
@@ -40,6 +40,9 @@ defaults:
40
40
  logical_name: Write a logical_name of the '{{ table_name }}' table.
41
41
  columns:
42
42
  description: Write a description of the '{{ table_name }}.{{ column_name }}' column.
43
+ seed_descriptions:
44
+ enum:
45
+ description: "{{ source_name }} {{ translated_table_name }} {{ translated_attribute_name }} enum"
43
46
 
44
47
  table_descriptions:
45
48
  ar_internal_metadata:
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Generate seed enum files for dbt from the specified TABLE_NAME and ENUM_COLUMN_NAME.
3
+
4
+ Example:
5
+ bin/rails generate active_record:dbt:enum TABLE_NAME ENUM_COLUMN_NAME
6
+
7
+ This will create:
8
+ #{export_directory_path}/seed_#{source_name}__#{table_name_singularize}_enum_#{enum_pluralized}.csv
9
+ #{export_directory_path}/seed_#{source_name}__#{table_name_singularize}_enum_#{enum_pluralized}.yml
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Dbt
5
+ module Generators
6
+ class EnumGenerator < Rails::Generators::NamedBase
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ argument :enum_column_name, type: :string, default: nil
10
+
11
+ def create_enum_seed_csv_file
12
+ create_file csv.export_path, csv.dump
13
+ end
14
+
15
+ def create_enum_seed_yml_file
16
+ create_file yml.export_path, yml.dump
17
+ end
18
+
19
+ private
20
+
21
+ def csv
22
+ @csv ||= ActiveRecord::Dbt::Seed::Enum::Csv.new(name, enum_column_name)
23
+ end
24
+
25
+ def yml
26
+ @yml ||= ActiveRecord::Dbt::Seed::Enum::Yml.new(name, enum_column_name)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -5,6 +5,7 @@ require 'active_record/dbt'
5
5
  ActiveRecord::Dbt.configure do |c|
6
6
  c.config_directory_path = 'lib/dbt'
7
7
  c.export_directory_path = 'doc/dbt'
8
+ c.dwh_platform = 'bigquery'
8
9
  c.data_sync_delayed = false
9
10
  c.used_dbt_package_names = []
10
11
  end
@@ -11,7 +11,7 @@ module ActiveRecord
11
11
  end
12
12
 
13
13
  def create_staging_model_yml_file
14
- create_file yml.export_path, yml.yml_dump
14
+ create_file yml.export_path, yml.dump
15
15
  end
16
16
 
17
17
  private
@@ -21,7 +21,7 @@ module ActiveRecord
21
21
  end
22
22
 
23
23
  def yml
24
- @yml ||= ActiveRecord::Dbt::Factory::Model::StagingFactory.yml_build(name)
24
+ @yml ||= ActiveRecord::Dbt::Factory::Model::StagingFactory.build(name)
25
25
  end
26
26
 
27
27
  def source_paths
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-dbt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yamotech
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-20 00:00:00.000000000 Z
11
+ date: 2024-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -72,9 +72,13 @@ files:
72
72
  - lib/active_record/dbt/column/testable/unique_testable.rb
73
73
  - lib/active_record/dbt/config.rb
74
74
  - lib/active_record/dbt/configuration/data_sync.rb
75
+ - lib/active_record/dbt/configuration/dwh_platform.rb
76
+ - lib/active_record/dbt/configuration/i18n_configuration.rb
75
77
  - lib/active_record/dbt/configuration/logger.rb
76
78
  - lib/active_record/dbt/configuration/parser.rb
79
+ - lib/active_record/dbt/configuration/source.rb
77
80
  - lib/active_record/dbt/configuration/used_dbt_package.rb
81
+ - lib/active_record/dbt/data_type/mapper.rb
78
82
  - lib/active_record/dbt/dbt_package/dbt_utils/table/testable/unique_combination_of_columns_testable.rb
79
83
  - lib/active_record/dbt/dbt_package/dbterd/column/testable/relationships_meta_relationship_type.rb
80
84
  - lib/active_record/dbt/factory/columns_factory.rb
@@ -82,10 +86,15 @@ files:
82
86
  - lib/active_record/dbt/factory/source_factory.rb
83
87
  - lib/active_record/dbt/factory/table_factory.rb
84
88
  - lib/active_record/dbt/factory/tables_factory.rb
89
+ - lib/active_record/dbt/i18n_wrapper/translate.rb
85
90
  - lib/active_record/dbt/model/staging/base.rb
86
91
  - lib/active_record/dbt/model/staging/sql.rb
87
92
  - lib/active_record/dbt/model/staging/yml.rb
88
93
  - lib/active_record/dbt/railtie.rb
94
+ - lib/active_record/dbt/required_methods.rb
95
+ - lib/active_record/dbt/seed/enum/base.rb
96
+ - lib/active_record/dbt/seed/enum/csv.rb
97
+ - lib/active_record/dbt/seed/enum/yml.rb
89
98
  - lib/active_record/dbt/source/yml.rb
90
99
  - lib/active_record/dbt/table/base.rb
91
100
  - lib/active_record/dbt/table/test.rb
@@ -94,6 +103,8 @@ files:
94
103
  - lib/generators/active_record/dbt/config/USAGE
95
104
  - lib/generators/active_record/dbt/config/config_generator.rb
96
105
  - lib/generators/active_record/dbt/config/templates/source_config.yml.tt
106
+ - lib/generators/active_record/dbt/enum/USAGE
107
+ - lib/generators/active_record/dbt/enum/enum_generator.rb
97
108
  - lib/generators/active_record/dbt/initializer/USAGE
98
109
  - lib/generators/active_record/dbt/initializer/initializer_generator.rb
99
110
  - lib/generators/active_record/dbt/initializer/templates/dbt.rb