-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add date_trunc function to mysql #17
base: master
Are you sure you want to change the base?
Add date_trunc function to mysql #17
Conversation
442a3c6
to
c0ec8d5
Compare
c797a4c
to
c513381
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if you were finished with this work, but wanted to point out the spelling issue in the readme.
README.md
Outdated
@@ -142,6 +142,8 @@ ActiveReporting::Configuration.setting = value | |||
|
|||
`metric_lookup_class` - The name of a constant used to lookup prebuilt `Reporting::Metric` objects by name. The constant should define a class method called `#lookup` which can take a string or symbol of the metric name. (Default: `::Metric`) | |||
|
|||
`db_adapter` - Each database has a diferent way of doing the same thing on SQL. This is an abstraction layer on each database. For example `PostGreSQL` have a native `date_trunc` while `MySQL` doesn't so we do the same in another way. Make sure to chosse your database's adapter (Default: `:sqlite3`). If you want MySQL put `:mysql2` and if you want PostGreSQL put `:postgresql`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misspelled "choose" here. Also, I've only known the database to be spelled "PostgreSQL". I'm not sure why the "G" is capitalized
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the corrections, I'm not an English speaker at all :)
c513381
to
609de71
Compare
Yes, it's finish :). Do you see it ok? |
37716cd
to
d43f34f
Compare
…menting the same feature
d43f34f
to
4553066
Compare
@@ -235,7 +237,6 @@ end | |||
|
|||
When creating a metric, ActiveReporting will recognize implicit hierarchies for this dimension. The hierarchies correspond to the [values](https://www.postgresql.org/docs/8.1/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC) supported by PostgreSQL. (See example under the metric section, below.) | |||
|
|||
*NOTE*: PRs welcomed to support this functionality in other databases. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess PRs are still welcome to support other databases 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOL, true but the rest of databases are SQLitle which I guess nobody use in production. I doubt someone will do that
# @return [String] | ||
def date_trunc(interval, field) | ||
clean_sql( | ||
<<-SQL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "squiggly heredoc" has been in Ruby since 2.3, I recommend using that instead.
https://ruby-doc.org/core-2.3.0/doc/syntax/literals_rdoc.html#label-Here+Documents
Also, I would say I personally am not concerned by multi-line SQL being generated, so I don't think #clean_sql
is really necessary.
class << self | ||
ADAPTERS = { | ||
sqlite3: SqliteAdapter, | ||
mysql2: MysqlAdapter, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go with just sqlite
and mysql
here. The use of "3" and "2" in various Rails projects references the adaptor and version of the gem they use. In the case of this gem, we shouldn't care.
@@ -142,6 +142,8 @@ ActiveReporting::Configuration.setting = value | |||
|
|||
`metric_lookup_class` - The name of a constant used to lookup prebuilt `Reporting::Metric` objects by name. The constant should define a class method called `#lookup` which can take a string or symbol of the metric name. (Default: `::Metric`) | |||
|
|||
`db_adapter` - Each database has a diferent way of doing the same thing on SQL. This is an abstraction layer on each database. For example `PostgreSQL` have a native `date_trunc` while `MySQL` doesn't so we do the same in another way. Make sure to choose your database's adapter (Default: `:sqlite3`). If you want MySQL put `:mysql2` and if you want PostGreSQL put `:postgresql`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a note here explaining that while it is encouraged to set this value, it is not required if the user does not with to implement database-specific features such as the date functions.
) | ||
SQL | ||
) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been playing around with different variants of replicating date_trunc
, including this one. This approach is about 2x faster according to my primitive metrics, but it falls short in truly replicating date_trunc
, mostly patently shown by the limit on supported datetime_hierarchies.
Take a look at this code and consider if it would be useful. It is ugly but may provide a better parallel in functionality.
report = ActiveReporting::Report.new(metric) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would propose that you might want to add some more comprehensive tests for the MySQL date_trunc adaptation. You can run into a lot of edge cases. See here for some examples you could include.
What is this?
Do a
db_adapter
to abstractdate_trunc
. This way I can implement in Ruby all the methods that generate the necessary methods to do date tuncation on SQL.TODO
DB='mysql' rake test
date_trunc