Skip to content

Commit

Permalink
'dba db:add' now infers and installs database schemas, if db pings.
Browse files Browse the repository at this point in the history
  • Loading branch information
blambeau committed Aug 1, 2010
1 parent 52bb28d commit 04bec06
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
10 changes: 10 additions & 0 deletions lib/dbagile/command/db/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def execute_command
name, uri = self.db_name, self.uri
db = DbAgile::Core::Database.new(name, uri)
repository << db
infer_schemas(db) if db.ping?

# Makes it the current one if requested
if self.current
Expand All @@ -77,6 +78,15 @@ def execute_command
# Returns created database
db
end

# Infers database schemas
def infer_schemas(db)
schema = db.physical_schema
db.set_announced_schema(schema)
db.set_effective_schema(schema)
rescue => ex
say("An error occured when infering schema: #{ex.message}\nYou'll have to install them manually. Sorry", :magenta)
end

end # class Add
end # module Db
Expand Down
32 changes: 27 additions & 5 deletions lib/dbagile/core/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ def announced_schema(unstage = false)
end
end

# Overrides announced schema with a given schema
def set_announced_schema(schema)
# Set announced files
self.announced_files ||= []
case announced_files.size
when 0
FileUtils.mkdir_p(file_resolver.call(name))
self.announced_files = [ "#{name}/announced.yaml" ]
when 1
else
raise "Unable to set announced schema with multiple effective files"
end
::File.open(file_resolver.call(announced_files[0]), 'w') do |io|
io << schema.to_yaml
end
end

# Returns the effective schema. If no effective files are installed and
# unstage is true, returns the physical schema. Returns nil otherwise.
def effective_schema(unstage = false)
Expand All @@ -154,12 +171,17 @@ def effective_schema(unstage = false)

# Overrides effective schema with a given schema
def set_effective_schema(schema)
return unless has_effective_schema?
if effective_files.size > 1
raise "Unable to set effective schema with multiple effective files"
# Set effective files
self.effective_files ||= []
case effective_files.size
when 0
FileUtils.mkdir_p(file_resolver.call(name))
self.effective_files = [ "#{name}/effective.yaml" ]
when 1
else
raise "Unable to set effective schema with multiple effective files"
end
file = file_resolver.call(effective_files[0])
::File.open(file, 'w') do |io|
::File.open(file_resolver.call(effective_files[0]), 'w') do |io|
io << schema.to_yaml
end
end
Expand Down

0 comments on commit 04bec06

Please sign in to comment.