#!/usr/bin/env ruby require_relative '../builder/builder.rb' module Prick::SubCommand def self.run(database, username, path, step: false, timer: nil, dump: nil, schema: nil) Timer.on! if timer Timer.time "Prick::Command#build" do begin super_conn = State.connection # Used to create new databases (doesn't make a # difference right now as the database user is # a superuser anyway conn = nil builder = nil constrain super_conn.rdbms.exist?(database), true # FIXME Same problem as below. Also in other commands Timer.time "Load build object" do if super_conn.rdbms.exist? database # FIXME Why create database? Setup should have done this exist = true else super_conn.rdbms.create database, owner: username exist = false end conn = Prick.state.connection # Parse builder = Prick::Build::Builder.new(conn, path, single: true, load_pool: false, step: step) # Register if a build file is referenced and normalize path to # include build.yml of directories if File.directory?(path) path = File.join(path, "build.yml") is_build_file = true elsif File.basename(path) == "build.yml" is_build_file = true else is_build_file = false end # Read schema from build file if possible and decide if schema should # be dropped beforehand if is_build_file if builder.root.has_schema !schema or Prick.error "Can't use --schema when doing a schema build" is_schema_rebuild = true schema = build_node.schema else is_schema_rebuild = false end else is_schema_rebuild = false end # Infer schema from path if !schema abspath = File.realpath(path) schemapath = File.realpath(Prick.state.schema_dir) if abspath =~ /^#{schemapath}\/([^\/]+)(?:\/.*)$/ schema = $1 else Prick.error "Can't find schema" # No default schema to avoid unintended runs end end # Write-back schema to builder builder.root.schema = schema # Drop schema if needed if is_schema_rebuild conn.schema.drop schema, cascade: true end # Create schema if absent if !conn.schema.exist?(schema) conn.schema.create(schema) end end if dump builder.load_pool case dump when :nodes; builder.nodes.reject { |node| node.is_a?(Build::BuildNode) }.map &:dump when :allnodes; builder.nodes.map &:dump when :batches; builder.dump else Prick.error "Illegal dump type: #{dump.inspect}" end else Timer.time "Execute build object" do builder.execute conn end end # FIXME # rescue Prick::Error => ex # $stderr.puts ex.message # exit 1 # # rescue ::Command::Error => ex # $stderr.puts ex.message # exit 1 ensure super_conn&.terminate conn&.terminate end end end end