Class: Prick::Build::Builder
- Inherits:
-
Object
- Object
- Prick::Build::Builder
- Defined in:
- lib/prick/builder/builder.rb
Instance Attribute Summary collapse
-
#clean ⇒ Object
True if database is initially clean - ie.
-
#conn ⇒ Object
readonly
PgConn object.
-
#dir ⇒ Object
readonly
Schema directory.
-
#path ⇒ Object
readonly
Path to file or directory.
-
#pool ⇒ Object
readonly
Pool of nodes.
-
#reflections_file ⇒ Object
readonly
Reflections YAML file.
-
#root ⇒ Object
readonly
Root build node.
-
#single ⇒ Object
readonly
True if this is a single-file build.
-
#step ⇒ Object
readonly
True if SQL entries should be single-stepped.
Instance Method Summary collapse
- #batches ⇒ Object
-
#create_batches ⇒ Object
Group sources into batches.
- #dump ⇒ Object
- #execute(conn, create_schemas: schemas) ⇒ Object
-
#initialize(conn, path, clean = true, single: false, touched: false, load_pool: true, step: false) ⇒ Builder
constructor
A new instance of Builder.
- #load_pool ⇒ Object
Constructor Details
#initialize(conn, path, clean = true, single: false, touched: false, load_pool: true, step: false) ⇒ Builder
Returns a new instance of Builder.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/prick/builder/builder.rb', line 63 def initialize(conn, path, clean = true, single: false, touched: false, load_pool: true, step: false) File.exist?(path) or raise Error, "Can't find #{path}" single || File.directory?(path) or raise Error, "Can't find directory #{path}" @conn = conn @path = path @reflections_file = Prick.state.reflections_file @clean = clean @single = single @step = step @root = Parser.parse(conn, path, single: single) @pool = nil # Initialized by #load_pool @batches = nil # Initialized by #create_batches self.load_pool if load_pool end |
Instance Attribute Details
#clean ⇒ Object
True if database is initially clean - ie. all tables are empty
40 41 42 |
# File 'lib/prick/builder/builder.rb', line 40 def clean @clean end |
#conn ⇒ Object (readonly)
PgConn object
28 29 30 |
# File 'lib/prick/builder/builder.rb', line 28 def conn @conn end |
#dir ⇒ Object (readonly)
Schema directory
31 32 33 |
# File 'lib/prick/builder/builder.rb', line 31 def dir @dir end |
#path ⇒ Object (readonly)
Path to file or directory
34 35 36 |
# File 'lib/prick/builder/builder.rb', line 34 def path @path end |
#pool ⇒ Object (readonly)
Pool of nodes. Initialized by #load_pool
52 53 54 |
# File 'lib/prick/builder/builder.rb', line 52 def pool @pool end |
#reflections_file ⇒ Object (readonly)
Reflections YAML file
37 38 39 |
# File 'lib/prick/builder/builder.rb', line 37 def reflections_file @reflections_file end |
#root ⇒ Object (readonly)
Root build node
43 44 45 |
# File 'lib/prick/builder/builder.rb', line 43 def root @root end |
#single ⇒ Object (readonly)
True if this is a single-file build
46 47 48 |
# File 'lib/prick/builder/builder.rb', line 46 def single @single end |
#step ⇒ Object (readonly)
True if SQL entries should be single-stepped. Default false
49 50 51 |
# File 'lib/prick/builder/builder.rb', line 49 def step @step end |
Instance Method Details
#batches ⇒ Object
61 |
# File 'lib/prick/builder/builder.rb', line 61 def batches() @batches || create_batches end |
#create_batches ⇒ Object
Group sources into batches
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/prick/builder/builder.rb', line 85 def create_batches @batches = [] kind = nil batch = nil # TODO: Refactor for node in [init_nodes, decl_nodes, fox_seed_nodes, sql_seed_nodes, term_nodes.reverse].flatten # Create new batch if required. Order of when-clauses is important case node.kind when :module if batch&.kind != :module @batches << batch if batch batch = ModuleBatch.new(self) end when :exe # Exe sources always create a new batch @batches << batch if batch batch = SqlBatch.new(self) when batch&.kind # Same kind as current batch. After this when-clause # we know that the node kind has changed if node.kind == :sql && step @batches << batch if batch batch = SqlBatch.new(self) elsif node.kind == :psql && step @batches << batch if batch batch = SqlBatch.new(self) end when :sql || node.kind == :inline if batch&.kind != :exe || step @batches << batch if batch batch = SqlBatch.new(self) end when :psql @batches << batch if batch batch = PSqlBatch.new(self) when :inline @batches << batch if batch batch = SqlBatch.new(self) when :fox @batches << batch if batch batch = FoxBatch.new(self) when :yml # build.yml files next else raise Error, "Unexpected node kind: #{node.kind}" end # Add node to current batch batch.nodes << node end # Add last batch @batches << batch if batch @batches end |
#dump ⇒ Object
168 169 170 171 |
# File 'lib/prick/builder/builder.rb', line 168 def dump load_pool if pool.nil? batches ? batches.each(&:dump) : pool.dump end |
#execute(conn, create_schemas: schemas) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/prick/builder/builder.rb', line 141 def execute(conn, create_schemas: schemas) # Load pool load_pool if pool.nil? # Group batches create_batches if batches.nil? # Register build in database Prick.state.save_build_begin # Create schemas conn.exec create_schemas.map { |schema| "create schema #{schema}" } if !single # Execute batch groups t0 = Time.now # for batch in batches # puts "Executing #{batch.class} batch" # batch.execute # end batches.each(&:execute) t1 = Time.now # Register build result in database dt = t1 - t0 Prick.state.save_build_end(true, dt) end |