Skip to content

Commit

Permalink
Adds a connection_string method to the adapters. It is used to retrieve
Browse files Browse the repository at this point in the history
a command to start a db console/REPL
  • Loading branch information
Linuus committed Jan 27, 2015
1 parent 02c3c29 commit cff0027
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/lotus/model/adapters/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ def query(collection, &blk)
def transaction(options = {})
raise NotImplementedError
end

# Returns a string which can be executed to start a console suitable
# for the configured database.
#
# @return [String]
#
# @since x.x.x
def connection_string
raise NotImplementedError
end
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions lib/lotus/model/adapters/sql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ def transaction(options = {})
end
end

# Returns a string which can be executed to start a console suitable
# for the configured database.
#
# @return [String]
#
# @since x.x.x
def connection_string
parsed_uri = URI.parse(@uri)
case parsed_uri.scheme
when 'sqlite'
"sqlite3 #{parsed_uri.host}#{parsed_uri.path}"
when 'postgres'
"psql -h #{parsed_uri.host} -d #{parsed_uri.path.sub(/^\//, '')}"
when 'mysql'

This comment has been minimized.

Copy link
@runlevel5

runlevel5 Jan 31, 2015

looks good, wondering if we should also support mysql2 adapter too?

This comment has been minimized.

Copy link
@jodosha

jodosha Feb 2, 2015

👍

"mysql -h #{parsed_uri.host} -d #{parsed_uri.path.sub(/^\//, '')}"

This comment has been minimized.

Copy link
@jodosha

jodosha Feb 2, 2015

This is -D or --database 😉

This comment has been minimized.

Copy link
@Linuus

Linuus Feb 2, 2015

Author Owner

Thanks :) I haven't tested this with mysql yet.

end
end

private

# Returns a collection from the given name.
Expand Down

4 comments on commit cff0027

@jodosha
Copy link

@jodosha jodosha commented on cff0027 Feb 2, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Linuus Thank you for this proposal, I think this is the right direction. Alongside with tests, this should be expanded with the following CLI options:

% psql --help
# ...
Connection options:
  -h, --host=HOSTNAME      database server host or socket directory (default: "local socket") # MISSING SOCKET SUPPORT
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name (default: "luca")
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)
mysql --help
# ...
  -D, --database=name Database to use.
  -p, --password[=name]
                      Password to use when connecting to server. If password is
                      not given it's asked from the tty.
  -P, --port=#        Port number to use for connection or 0 for default to, in
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
                      /etc/services, built-in default (3306).
  -S, --socket=name   The socket file to use for connection.
  -u, --user=name     User for login if not current user.

@Linuus
Copy link
Owner Author

@Linuus Linuus commented on cff0027 Feb 2, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jodosha Yes I was thinking about that but if we specify all those options for the dbconsole command it gets tied to the actual adapter, right? Different adapters can take different options. So I guess we must collect all options sent to the dbconsole command and just pass it along to the connection_string method and let it handle it?

EDIT: Hmm but that does not seem to be possible with Thor.

@jodosha
Copy link

@jodosha jodosha commented on cff0027 Feb 9, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Linuus Apart from Thor, I don't think it's a good idea and I'll tell you why in a second. 😄

First of all, it doesn't create a solid CLI, as it doesn't protect from databases changes. For instance, if a newer version of MySQL changes from --user to --username developers shouldn't notice it.

Secondly, it looses the point of this command at all. If they have to do bundle exec lotus dbconsole --port=1234 --username=luca --password=secret, and figure out which args are part of Lotus, and which are part of the database CLI. I think they will go for the latter: psql ... it will became easier to deal with.

@Linuus
Copy link
Owner Author

@Linuus Linuus commented on cff0027 Feb 9, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jodosha Yes, I agree :) I have tried to create a unified interface.

The only thing which is a bit weird is if I use mysql I can write
lotus dbconsole --password=foobar
and it will do it.
If I use the same command for psql it will discard the actual password and just bring up the password prompt.

lotus dbconsole --password=foobar -> psql --password

But maybe that's not an issue. It may just be a bit hard to know what lotus options I can use for my DB.

If I do lotus dbconsole --help and I see:
-S, --socket=name The socket file to use for connection.

I don't know if it applies to mysql, psql or anything else.

Please sign in to comment.