Skip to content

Commit

Permalink
cli: implement db create/drop for sqlite3 (#351)
Browse files Browse the repository at this point in the history
When running `amber db create` for an sqlite3 database, amber displays
the error:

  could not determine database name

This can be confusing to the user who doesn't know that `amber db
create` does not need to be run for sqlite3 databases. Display a
friendlier message.

Also implement `amber db drop` to delete the database.
  • Loading branch information
nathanj authored and eliasjpr committed Nov 8, 2017
1 parent 8ba8c03 commit 8d9a461
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
28 changes: 28 additions & 0 deletions spec/amber/cli/commands/generator_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ include CLIHelper
include CLIFixtures

module Amber::CLI
context "sqlite" do
cleanup
MainCommand.run ["new", TESTING_APP, "-d", "sqlite"]

describe "database" do
db_filename = db_yml["sqlite"]["database"].to_s.sub("sqlite3:", "")

Dir.cd(TESTING_APP)

it "does not create the database when `db create`" do
MainCommand.run ["db", "create"]
File.exists?(db_filename).should be_false
end

it "does create the database when `db migrate`" do
MainCommand.run ["generate", "model", "Post"]
MainCommand.run ["db", "migrate"]
File.exists?(db_filename).should be_true
File.stat(db_filename).size.should_not eq 0
end

it "deletes the database when `db drop`" do
MainCommand.run ["db", "drop"]
File.exists?(db_filename).should be_false
end
end
end

describe "amber generate" do
ENV["AMBER_ENV"] = "test"
camel_case = "PostComment"
Expand Down
31 changes: 21 additions & 10 deletions src/amber/cli/commands/database.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,34 @@ module Amber::CLI
end

def drop_database
name = set_database_to_schema
Micrate::DB.connect do |db|
db.exec "DROP DATABASE #{name};"
url = Micrate::DB.connection_url.to_s
if url.starts_with? "sqlite3:"
path = url.gsub("sqlite3:", "")
File.delete(path)
puts "Deleted file #{path}"
else
name = set_database_to_schema url
Micrate::DB.connect do |db|
db.exec "DROP DATABASE #{name};"
end
puts "Dropped database #{name}"
end
puts "Dropped database #{name}"
end

def create_database
name = set_database_to_schema
Micrate::DB.connect do |db|
db.exec "CREATE DATABASE #{name};"
url = Micrate::DB.connection_url.to_s
if url.starts_with? "sqlite3:"
puts "For sqlite3, the database will be created during the first migration."
else
name = set_database_to_schema url
Micrate::DB.connect do |db|
db.exec "CREATE DATABASE #{name};"
end
puts "Created database #{name}"
end
puts "Created database #{name}"
end

def set_database_to_schema
url = Micrate::DB.connection_url.to_s
def set_database_to_schema(url)
uri = URI.parse(url)
if path = uri.path
Micrate::DB.connection_url = url.gsub(path, "/#{uri.scheme}")
Expand Down

0 comments on commit 8d9a461

Please sign in to comment.