Class: Tarantool::Schema::Definition

Inherits:
Object
  • Object
show all
Defined in:
tarantool-schema/lib/tarantool/schema/definition.rb

Overview

Spaces, indexes and grants an application needs. Built once from a block and frozen, so a definition kept in a constant cannot grow when its file is loaded again. The block runs with the definition as self.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Definition

Returns a new instance of Definition.



8
9
10
11
12
13
14
15
16
17
# File 'tarantool-schema/lib/tarantool/schema/definition.rb', line 8

def initialize(&block)
  @spaces = {}
  @external = {}
  @grants = {}
  instance_exec(&block) if block
  @spaces.transform_values! { |space| SpaceRules.check(space.resolve) }.freeze
  @external.each_value(&:freeze).freeze
  @grants.each_value(&:freeze).freeze
  freeze
end

Class Method Details

.from_live(client, only: nil) ⇒ Object

Every user space as the server has it, with the grants on it, or only the named ones. A space the DSL cannot express raises.



21
22
23
24
25
26
27
28
29
30
# File 'tarantool-schema/lib/tarantool/schema/definition.rb', line 21

def self.from_live(client, only: nil)
  arguments = SpaceLister.call(client, only:).to_h { |name| [name, live_arguments(client, name)] }

  new do
    arguments.each do |name, (space, grants)|
      ensure_space(name, **space)
      grants.each { |user, privileges| grant(user, privileges, on: name) }
    end
  end
end

Instance Method Details

#apply(client, alter: []) ⇒ Object

The second plan re-reads everything, so a space another deploy created differently in the meantime fails here instead of being reported as created.



86
87
88
89
90
91
92
93
# File 'tarantool-schema/lib/tarantool/schema/definition.rb', line 86

def apply(client, alter: [])
  alter = Alter.check(alter)
  creations = build_plan(client, alter).verify.creations
  creations.each { |creation| creation.apply(client) }
  build_plan(client, alter).verify(complete: true)

  creations.map(&:to_s)
end

#ensure_index(space, name, **options) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'tarantool-schema/lib/tarantool/schema/definition.rb', line 50

def ensure_index(space, name, **options)
  raise ArgumentError, 'space without name' if space.nil?

  space = space.to_sym
  index = IndexSpec.build(name, options)
  if @spaces.key?(space)
    @spaces[space] = @spaces[space].add_index(index)
  else
    add_external(space, index)
  end
  nil
end

#ensure_space(name, format:, indexes:, engine: :memtx) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
# File 'tarantool-schema/lib/tarantool/schema/definition.rb', line 39

def ensure_space(name, format:, indexes:, engine: :memtx)
  raise ArgumentError, 'space without name' if name.nil?

  name = name.to_sym
  raise ArgumentError, "space #{name} is already declared" if @spaces.key?(name)
  raise ArgumentError, "space #{name}: declare it before its ensure_index" if @external.key?(name)

  @spaces[name] = SpaceSpec.build(name, engine:, format:, indexes:)
  nil
end

#grant(user, privileges, on:) ⇒ Object

user may be a role too. Privileges already granted are kept, nothing is revoked.



64
65
66
67
68
69
70
71
72
73
# File 'tarantool-schema/lib/tarantool/schema/definition.rb', line 64

def grant(user, privileges, on:)
  spec = GrantSpec.build(user, privileges, on:)
  grants = @grants[spec.space] ||= []
  if grants.any? { |declared| declared.user == spec.user }
    raise ArgumentError, "grant #{spec.user} on #{spec.space} is already declared"
  end

  grants << spec
  nil
end

#grants ⇒ Object



77
# File 'tarantool-schema/lib/tarantool/schema/definition.rb', line 77

def grants = @grants.values.flatten.map(&:to_h)

#plan(client, alter: []) ⇒ Object

alter: lists the changes to live spaces that are allowed besides creating what is missing, see Alter.



82
# File 'tarantool-schema/lib/tarantool/schema/definition.rb', line 82

def plan(client, alter: []) = build_plan(client, Alter.check(alter)).verify.creations.map(&:to_s)

#to_h ⇒ Object



75
# File 'tarantool-schema/lib/tarantool/schema/definition.rb', line 75

def to_h = @spaces.transform_values(&:to_h)

#to_ruby ⇒ Object



79
# File 'tarantool-schema/lib/tarantool/schema/definition.rb', line 79

def to_ruby = Source.render(to_h, @external.transform_values { |indexes| indexes.map(&:to_h) }, grants)