Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

andresgutgon
Copy link
Contributor

@andresgutgon andresgutgon commented Oct 10, 2018

What is this?

Do a db_adapter to abstract date_trunc. This way I can implement in Ruby all the methods that generate the necessary methods to do date tuncation on SQL.

TODO

  • Test Mysql migration generator works on a Rails app
  • Pass all tests with DB='mysql' rake test
  • MySQL adapter with date_trunc

@andresgutgon andresgutgon force-pushed the feature/trunc-date-mysql-function-implementation branch 2 times, most recently from 442a3c6 to c0ec8d5 Compare October 11, 2018 09:21
@andresgutgon andresgutgon force-pushed the feature/trunc-date-mysql-function-implementation branch 3 times, most recently from c797a4c to c513381 Compare October 13, 2018 09:27
Copy link
Owner

@t27duck t27duck left a 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`.
Copy link
Owner

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

Copy link
Contributor Author

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 :)

@andresgutgon andresgutgon force-pushed the feature/trunc-date-mysql-function-implementation branch from c513381 to 609de71 Compare October 15, 2018 07:53
@andresgutgon
Copy link
Contributor Author

Yes, it's finish :). Do you see it ok?

@andresgutgon andresgutgon force-pushed the feature/trunc-date-mysql-function-implementation branch from 37716cd to d43f34f Compare October 15, 2018 10:25
@andresgutgon andresgutgon force-pushed the feature/trunc-date-mysql-function-implementation branch from d43f34f to 4553066 Compare October 15, 2018 10:54
@@ -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.
Copy link

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 😉

Copy link
Contributor Author

@andresgutgon andresgutgon Oct 15, 2018

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
Copy link
Owner

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,
Copy link
Owner

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`.
Copy link
Owner

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
Copy link
Contributor

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
Copy link
Contributor

@niborg niborg Oct 17, 2018

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.

@t27duck t27duck added the stale label Oct 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants