hecks-adapters-sql-database 0.1.16.rc → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b31e87dcb2a1765ae18855a18547b9d15ffcbd3a
4
- data.tar.gz: dd98c21eebf3af7b87bf688db29c8d182a426bfc
3
+ metadata.gz: 718a990a3d06f30ec1b5e073041e5a85b315625a
4
+ data.tar.gz: 1cb6ca574d8580e04eba20f16670218459d2efd1
5
5
  SHA512:
6
- metadata.gz: a43324c284dddb638c8a8d7fa2737760aa01e675029ec66818e0c95ffdc8a584a52d79ed4952bef3ee5e69b6f1ea1f707369fb00e2acf5efc3465ce580aa3c07
7
- data.tar.gz: 0305d01d1d136b6887248978b435202cbbffbc9b7b58407e5ecdc43577c4e9ca9c897155b09e55b9d78b4224694dd4822410729765b691a641ce2147de783ff3
6
+ metadata.gz: b12bbe762373e33e9c619b47292932ee3bb8f9e03f76076d139ff2c19fe6911eded3a4e0ff2aec3c79937f5cc979719554ea2e8ab1f2e8d4295d01f386df04ae
7
+ data.tar.gz: 58293406eb18f8bdc30feecf8b5207461bbe3b9bcf73c7b71918bf11d594d6f3bbc7a9662b0b7c967fbd1de06b0f5aa18157ce9f1c84c1a7b5b4e1d4168a35dc
data/lib/cli/cli.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative 'generate_domain_migrations'
2
+ require_relative 'generate_sql_database'
3
+ require_relative 'migration_builder'
@@ -0,0 +1,29 @@
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ module CLI
4
+ class GenerateDomainMigrations < Thor::Group
5
+ include Thor::Actions
6
+ attr_writer :migration_builder
7
+
8
+ def load_domain_spec
9
+ load('Domain')
10
+ end
11
+
12
+ def self.source_root
13
+ File.dirname(__FILE__) + '/templates/'
14
+ end
15
+
16
+ def create_migration_file
17
+ @migration_builder = MigrationBuilder.new(self, DOMAIN)
18
+ @migration_builder.call
19
+ end
20
+
21
+ private
22
+
23
+ def migration_builder
24
+ @migration_builder
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ module CLI
4
+ class GenerateSQLDatabase < Thor::Group
5
+ include Thor::Actions
6
+
7
+ def self.source_root
8
+ File.dirname(__FILE__) + '/templates/'
9
+ end
10
+
11
+ def create_sql_database_folder
12
+ directory('sql_database', './adapters/sql_database')
13
+ end
14
+
15
+ def bundle
16
+ run 'bundle'
17
+ end
18
+
19
+ private
20
+
21
+ def class_name
22
+ @class_name
23
+ end
24
+
25
+ def domain_module_name
26
+ DOMAIN.name.camelize
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,47 @@
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ module CLI
4
+ class MigrationBuilder
5
+ def initialize(generator, specification)
6
+ @generator = generator
7
+ @schema = HecksAdapters::SQLDatabase::Schema.factory(specification)
8
+ end
9
+
10
+ def call
11
+ generate_migrations
12
+ self
13
+ end
14
+
15
+ def table_name
16
+ @table.name
17
+ end
18
+
19
+ def columns
20
+ @table.columns
21
+ end
22
+
23
+ def tables
24
+ @schema.tables
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :generator
30
+
31
+ def file_name(index, object)
32
+ "#{index}_create_#{table_name}.rb"
33
+ end
34
+
35
+ def generate_migrations
36
+ tables.each.with_index(1) do |table, index|
37
+ @table = table
38
+ @generator.template(
39
+ "migration.rb.tt",
40
+ "db/migrate/" + file_name(index, table)
41
+ )
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,14 @@
1
+ Sequel.migration do
2
+ up do
3
+ create_table(:<%= migration_builder.table_name %>) do
4
+ String :id, primary_key: true
5
+ <%- migration_builder.columns.each do |column| -%>
6
+ <%= column.type.camelcase %> :<%= column.name.to_sym %>, :null=>false
7
+ <%- end -%>
8
+ end
9
+ end
10
+
11
+ down do
12
+ drop_table(:<%= migration_builder.table_name %>)
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ module <%= domain_module_name %>
2
+ module Adapters
3
+ class SQLDatabase
4
+ module Repositories
5
+ class <%= class_name %>
6
+ def self.create attributes = {}
7
+ end
8
+
9
+ def self.update id, attributes
10
+ end
11
+
12
+ def self.read id
13
+ end
14
+
15
+ def self.delete id
16
+ end
17
+
18
+ def self.delete_all
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ namespace :db do
2
+ desc "Run migrations"
3
+ task :migrate, [:version] do |t, args|
4
+ require "sequel"
5
+ Sequel.extension :migration
6
+ db ||= Sequel.connect(ENV.fetch("DATABASE_URL"))
7
+ if args[:version]
8
+ puts "Migrating to version #{args[:version]}"
9
+ Sequel::Migrator.run(db, "db/migrate", target: args[:version].to_i)
10
+ else
11
+ puts "Migrating to latest"
12
+ Sequel::Migrator.run(db, "db/migrate")
13
+ end
14
+ end
15
+ end
data/lib/column.rb CHANGED
@@ -1,77 +1,76 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- class Column
5
- attr_reader :referenced_object
6
- TYPE_MAP = {
7
- 'Currency' => "BigDecimal",
8
- 'String' => 'String',
9
- 'Integer' => 'Integer'
10
- }
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ # Represents a SQL Column
4
+ class Column
5
+ attr_reader :referenced_object
6
+ TYPE_MAP ||= {
7
+ 'Currency' => "BigDecimal",
8
+ 'String' => 'String',
9
+ 'Integer' => 'Integer'
10
+ }
11
11
 
12
- def initialize(name:, referenced_object: nil, table_name: nil, type:, is_list: false)
13
- @name = name
14
- @referenced_object = referenced_object
15
- @type = type
16
- @table_name = table_name
17
- @is_list = is_list
18
- end
12
+ def initialize(name:, referenced_object: nil, table_name: nil, type:, is_list: false)
13
+ @name = name
14
+ @referenced_object = referenced_object
15
+ @type = type
16
+ @table_name = table_name
17
+ @is_list = is_list
18
+ end
19
19
 
20
- def self.factory(attribute)
21
- new(
22
- name: attribute.name,
23
- referenced_object: attribute.referenced_object,
24
- type: attribute.type,
25
- table_name: attribute.object_name,
26
- is_list: attribute.list?
27
- )
28
- end
20
+ def self.factory(attribute)
21
+ new(
22
+ name: attribute.name,
23
+ referenced_object: attribute.referenced_object,
24
+ type: attribute.type,
25
+ table_name: attribute.object_name,
26
+ is_list: attribute.list?
27
+ )
28
+ end
29
29
 
30
- def to_foreign_key
31
- (type.downcase + '_id').to_sym
32
- end
30
+ def to_foreign_key
31
+ (type.downcase + '_id').to_sym
32
+ end
33
33
 
34
- def to_table_name
35
- name.downcase.pluralize.to_sym
36
- end
34
+ def to_table_name
35
+ name.downcase.pluralize.to_sym
36
+ end
37
37
 
38
- def list?
39
- @is_list
40
- end
38
+ def list?
39
+ @is_list
40
+ end
41
41
 
42
- def reference?
43
- @referenced_object
44
- end
42
+ def reference?
43
+ @referenced_object
44
+ end
45
45
 
46
- def referenced_table
47
- return unless @referenced_object
48
- @referenced_object.pluralize.underscore
49
- end
46
+ def referenced_table
47
+ return unless @referenced_object
48
+ @referenced_object.pluralize.underscore
49
+ end
50
50
 
51
- def name
52
- @name
53
- end
51
+ def name
52
+ @name
53
+ end
54
54
 
55
- def copy(new_attributes={})
56
- self.class.new(
57
- {
58
- name: self.name,
59
- referenced_object: self.referenced_object,
60
- type: self.type,
61
- is_list: self.list?
62
- }.merge(new_attributes)
63
- )
64
- end
55
+ def copy(new_attributes={})
56
+ self.class.new(
57
+ {
58
+ name: self.name,
59
+ referenced_object: self.referenced_object,
60
+ type: self.type,
61
+ is_list: self.list?
62
+ }.merge(new_attributes)
63
+ )
64
+ end
65
65
 
66
- def ==(other)
67
- return false if name != other.name
68
- return false if referenced_table != other.referenced_table
69
- true
70
- end
66
+ def ==(other)
67
+ return false if name != other.name
68
+ return false if referenced_table != other.referenced_table
69
+ true
70
+ end
71
71
 
72
- def type
73
- TYPE_MAP[@type] || @type
74
- end
72
+ def type
73
+ TYPE_MAP[@type] || @type
75
74
  end
76
75
  end
77
76
  end
@@ -1,57 +1,59 @@
1
1
  require_relative 'create/find_or_create_references'
2
2
  require_relative 'create/add_to_join_tables'
3
3
 
4
- module Hecks
5
- module Adapters
6
- class SQLDatabase
7
- module Commands
8
- class Create
9
- attr_reader :id
10
-
11
- def initialize(attributes:, head:)
12
- @attributes = attributes.clone
13
- @reference_ids = {}
14
- @head = head
15
- @references = @head.references
16
- @table = Table.factory([@head]).first
17
- end
4
+ module HecksAdapters
5
+ class SQLDatabase
6
+ module Commands
7
+ # Create a resource
8
+ class Create
9
+ attr_reader :id
10
+
11
+ def initialize(attributes:, head:)
12
+ @attributes = attributes.clone
13
+ @reference_ids = {}
14
+ @head = head
15
+ @references = @head.references
16
+ @table = Table.factory([@head]).first
17
+ end
18
18
 
19
- def call
20
- DB.transaction do
21
- find_or_create_references
22
- create
23
- add_to_join_tables
24
- end
25
- self
19
+ def call
20
+ DB.transaction do
21
+ find_or_create_references
22
+ create
23
+ add_to_join_tables
26
24
  end
25
+ self
26
+ end
27
27
 
28
- private
28
+ private
29
29
 
30
- def find_or_create_references
31
- @reference_ids =
32
- FindOrCreateReferences.new(
33
- head: @head,
34
- attributes: @attributes
35
- ).call.reference_ids
36
- end
30
+ def find_or_create_references
37
31
 
38
- def create
39
- graph = @references.map do |reference|
40
- next if reference.list?
41
- column = Column.factory(reference)
42
- [column.to_foreign_key, @reference_ids[reference.name]]
43
- end.compact.to_h
32
+ @reference_ids =
33
+ FindOrCreateReferences.new(
34
+ head: @head,
35
+ attributes: @attributes
36
+ ).call.reference_ids
44
37
 
45
- @id = DB[@table.name.to_sym].insert(@attributes.merge(graph))
46
- end
38
+ end
47
39
 
48
- def add_to_join_tables
49
- AddToJoinTables.new(
50
- head: @head,
51
- id: @id,
52
- reference_ids: @reference_ids
53
- ).call
54
- end
40
+ def create
41
+ graph = @references.map do |reference|
42
+ next if reference.list?
43
+ column = Column.factory(reference)
44
+ [column.to_foreign_key, @reference_ids[reference.name]]
45
+ end.compact.to_h
46
+
47
+ @id = @attributes[:id]
48
+ DB[@table.name.to_sym].insert(@attributes.merge(graph))
49
+ end
50
+
51
+ def add_to_join_tables
52
+ AddToJoinTables.new(
53
+ head: @head,
54
+ id: @id,
55
+ reference_ids: @reference_ids
56
+ ).call
55
57
  end
56
58
  end
57
59
  end
@@ -1,36 +1,36 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- module Commands
5
- class Create
6
- class AddToJoinTables
7
- attr_reader :reference_ids
8
- def initialize(head:, reference_ids:, id:)
9
- @head = head
10
- @table = Table.factory([@head]).first
11
- @reference_ids = reference_ids
12
- @id = id
13
- end
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ module Commands
4
+ class Create
5
+ # Update data in joining tables
6
+ class AddToJoinTables
7
+ attr_reader :reference_ids
8
+ def initialize(head:, reference_ids:, id:)
9
+ @head = head
10
+ @table = Table.factory([@head]).first
11
+ @reference_ids = reference_ids
12
+ @id = id
13
+ end
14
14
 
15
- def call
16
- @head.references.each do |reference|
17
- column = Column.factory(reference)
18
- join_table = JoinTable.new(@table, column)
15
+ def call
16
+ @head.references.each do |reference|
17
+ column = Column.factory(reference)
18
+ join_table = JoinTable.new(@table, column)
19
19
 
20
- next unless reference.list?
20
+ next unless reference.list?
21
21
 
22
- @reference_ids[reference.name.downcase].each do |id|
23
- DB[join_table.name.to_sym].insert(record(column, id))
24
- end
22
+ @reference_ids[reference.name.downcase].each do |id|
23
+ DB[join_table.name.to_sym].insert(record(column, id).merge(id: SecureRandom.uuid))
25
24
  end
26
- self
25
+
27
26
  end
27
+ self
28
+ end
28
29
 
29
- private
30
+ private
30
31
 
31
- def record(column, id)
32
- [[@table.to_foreign_key, @id], [(column.to_foreign_key).to_sym, id]].to_h
33
- end
32
+ def record(column, id)
33
+ [[@table.to_foreign_key, @id], [(column.to_foreign_key).to_sym, id]].to_h
34
34
  end
35
35
  end
36
36
  end