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 +4 -4
- data/lib/cli/cli.rb +3 -0
- data/lib/cli/generate_domain_migrations.rb +29 -0
- data/lib/cli/generate_sql_database.rb +31 -0
- data/lib/cli/migration_builder.rb +47 -0
- data/lib/cli/templates/migration.rb.tt +14 -0
- data/lib/cli/templates/repository.rb.tt +24 -0
- data/lib/cli/templates/sql_database/Rakefile +15 -0
- data/lib/column.rb +62 -63
- data/lib/commands/create.rb +46 -44
- data/lib/commands/create/add_to_join_tables.rb +26 -26
- data/lib/commands/create/find_or_create_references.rb +46 -39
- data/lib/commands/delete.rb +13 -14
- data/lib/commands/read.rb +22 -23
- data/lib/commands/read/fetch_references.rb +40 -40
- data/lib/commands/update.rb +28 -29
- data/lib/commands/update/create_new_value.rb +33 -30
- data/lib/commands/update/delete_references.rb +30 -31
- data/lib/commands/update/link_to_references.rb +33 -33
- data/lib/commands/update/update_values.rb +42 -43
- data/lib/hecks-adapters-sql-database.rb +11 -10
- data/lib/join_table.rb +14 -15
- data/lib/repository.rb +23 -28
- data/lib/schema.rb +13 -14
- data/lib/schema_factory.rb +39 -40
- data/lib/table.rb +29 -30
- metadata +18 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 718a990a3d06f30ec1b5e073041e5a85b315625a
|
4
|
+
data.tar.gz: 1cb6ca574d8580e04eba20f16670218459d2efd1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b12bbe762373e33e9c619b47292932ee3bb8f9e03f76076d139ff2c19fe6911eded3a4e0ff2aec3c79937f5cc979719554ea2e8ab1f2e8d4295d01f386df04ae
|
7
|
+
data.tar.gz: 58293406eb18f8bdc30feecf8b5207461bbe3b9bcf73c7b71918bf11d594d6f3bbc7a9662b0b7c967fbd1de06b0f5aa18157ce9f1c84c1a7b5b4e1d4168a35dc
|
data/lib/cli/cli.rb
ADDED
@@ -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
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
30
|
+
def to_foreign_key
|
31
|
+
(type.downcase + '_id').to_sym
|
32
|
+
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
def to_table_name
|
35
|
+
name.downcase.pluralize.to_sym
|
36
|
+
end
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
def list?
|
39
|
+
@is_list
|
40
|
+
end
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
def reference?
|
43
|
+
@referenced_object
|
44
|
+
end
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
def referenced_table
|
47
|
+
return unless @referenced_object
|
48
|
+
@referenced_object.pluralize.underscore
|
49
|
+
end
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
def name
|
52
|
+
@name
|
53
|
+
end
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
73
|
-
|
74
|
-
end
|
72
|
+
def type
|
73
|
+
TYPE_MAP[@type] || @type
|
75
74
|
end
|
76
75
|
end
|
77
76
|
end
|
data/lib/commands/create.rb
CHANGED
@@ -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
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
28
|
+
private
|
29
29
|
|
30
|
-
|
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
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end.compact.to_h
|
32
|
+
@reference_ids =
|
33
|
+
FindOrCreateReferences.new(
|
34
|
+
head: @head,
|
35
|
+
attributes: @attributes
|
36
|
+
).call.reference_ids
|
44
37
|
|
45
|
-
|
46
|
-
end
|
38
|
+
end
|
47
39
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def call
|
16
|
+
@head.references.each do |reference|
|
17
|
+
column = Column.factory(reference)
|
18
|
+
join_table = JoinTable.new(@table, column)
|
19
19
|
|
20
|
-
|
20
|
+
next unless reference.list?
|
21
21
|
|
22
|
-
|
23
|
-
|
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
|
-
|
25
|
+
|
27
26
|
end
|
27
|
+
self
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
+
private
|
30
31
|
|
31
|
-
|
32
|
-
|
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
|