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.
Instance Method Summary collapse
-
#auth? ⇒ Boolean
True if auth data should be loaded.
- #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, seeds: true, auth: true, step: false) ⇒ Builder
constructor
A new instance of Builder.
- #load_pool ⇒ Object
-
#seeds? ⇒ Boolean
True if seed data should be loaded.
-
#step? ⇒ Boolean
True if SQL entries should be single-stepped.
Constructor Details
#initialize(conn, path, clean = true, single: false, touched: false, load_pool: true, seeds: true, auth: true, step: false) ⇒ Builder
Returns a new instance of Builder.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/prick/builder/builder.rb', line 71 def initialize(conn, path, clean = true, single: false, touched: false, load_pool: true, seeds: true, auth: true, step: false) File.exist?(path) or raise Error, "Can't find #{path}" ! single || File.file?(path) or raise Error, "Not a file - #{path}" @conn = conn @path = path @reflections_file = Prick.state.reflections_file @clean = clean @single = single @seeds = seeds @auth = auth @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
59 60 61 |
# File 'lib/prick/builder/builder.rb', line 59 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. Default false
46 47 48 |
# File 'lib/prick/builder/builder.rb', line 46 def single @single end |
Instance Method Details
#auth? ⇒ Boolean
True if auth data should be loaded. Default true
52 |
# File 'lib/prick/builder/builder.rb', line 52 def auth?() @auth end |
#batches ⇒ Object
69 |
# File 'lib/prick/builder/builder.rb', line 69 def batches() @batches || create_batches end |
#create_batches ⇒ Object
Group sources into batches
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/prick/builder/builder.rb', line 96 def create_batches @batches = [] kind = nil batch = nil nodes = ( [init_nodes, decl_nodes] + (seeds? ? [fox_seed_nodes, sql_seed_nodes] : []) + (auth? ? auth_nodes : []) + [term_nodes.reverse] ).flatten # TODO: Refactor for node in nodes # 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
186 187 188 189 |
# File 'lib/prick/builder/builder.rb', line 186 def dump load_pool if pool.nil? batches ? batches.each(&:dump) : pool.dump end |
#execute(conn, create_schemas: schemas) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/prick/builder/builder.rb', line 159 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 |
#load_pool ⇒ Object
90 91 92 93 |
# File 'lib/prick/builder/builder.rb', line 90 def load_pool @pool = NodePool.new load_pool_impl(@root) end |
#seeds? ⇒ Boolean
True if seed data should be loaded. Default true
49 |
# File 'lib/prick/builder/builder.rb', line 49 def seeds?() @seeds end |
#step? ⇒ Boolean
True if SQL entries should be single-stepped. Default false
55 56 |
# File 'lib/prick/builder/builder.rb', line 55 def step?() = @step # def step?() @step end |