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

Autogenerated migration issue with Rails 7, MySQL and CURRENT_TIMESTAMP #159

Closed
adamlofting opened this issue Sep 19, 2022 · 11 comments
Closed

Comments

@adamlofting
Copy link

Thanks for all the great work on rodauth-rails.

I've just tried an install on a fresh Rails 7 app using MySQL and caught one small issue, which I've solved but thought it might be helpful to share here.

In the autogenerated migrations for the accounts table (db/migrate/xxx_create_rodauth.rb), there are 3 fields that use the CURRENT_TIMESTAMP as the default value for datetime fields.

When running rails db:migrate in Rails 7, this generates an error :

ActiveRecord::StatementInvalid: Mysql2::Error: Invalid default value for xxxxxxxxx

I found the solution discussed here on the rails repo: rails/rails#43292

I was able to make the rodauth-rails migrations work by changing the examples like this:

t.datetime :email_last_sent, null: false, default: -> { "CURRENT_TIMESTAMP" }

to:

t.datetime :email_last_sent, null: false, precision: nil, default: -> { "CURRENT_TIMESTAMP()" }
@janko
Copy link
Owner

janko commented Sep 19, 2022

Thanks for reporting the issue. For some reason I wasn't able to reproduce the issue on Rails 7.0.4 and MySQL 8.0.30, for me the migration succeeds, and I can create records that rely on this default value. What versions are you using?

@adamlofting
Copy link
Author

No worries, and that's interesting.

I'm running:

  • Rails 7.0.4
  • MySQL 8.0.29
  • mysql2 0.5.4

I don't think this is important, but kust in case it helps, I was running rails generate rodauth:install --jwt

@janko
Copy link
Owner

janko commented Sep 19, 2022

OK, so the only difference is the minor version of MySQL, I don't know if the recent version happens to fix this issue 🤷🏻‍♂️. I'm on MacBook M1 (Apple Silicon), I don't know if that makes any difference. You're able to reproduce this in a fresh Rails app?

@adamlofting
Copy link
Author

Yes, it's very strange 🧐

For the sake of ruling things out, I've just created a completely fresh Rails App and was able to repro this again.

I then updated MySQL to 8.0.30 and was still able to repro this (notes below on the commands I ran).

It looks like the only other difference is that I'm running an Intel MacBook.

Screenshot 2022-09-19 at 14 51 27

$ rails new roaduth-rails-repro --database=mysql

Add to gemfile:

gem 'rodauth-rails', '~> 1.6'
$ bundle install
$ rails generate rodauth:install --jwt
$ bundle add jwt
$ bin/rails db:create
$ rails db:migrate
== 20220919133647 CreateRodauth: migrating ====================================
-- create_table(:accounts)
   -> 0.0132s
-- create_table(:account_password_reset_keys, {:id=>false})
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Invalid default value for 'email_last_sent'
/Users/adamlofting/code/roaduth-rails-repro/db/migrate/20220919133647_create_rodauth.rb:11:in `change'

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Invalid default value for 'email_last_sent'
/Users/adamlofting/code/roaduth-rails-repro/db/migrate/20220919133647_create_rodauth.rb:11:in `change'

Caused by:
Mysql2::Error: Invalid default value for 'email_last_sent'
/Users/adamlofting/code/roaduth-rails-repro/db/migrate/20220919133647_create_rodauth.rb:11:in `change'
$ brew update
$ brew install mysql
$ mysql -V
mysql  Ver 8.0.30 for macos12.4 on x86_64 (Homebrew)

Then repeat the steps above with the same results.

@janko
Copy link
Owner

janko commented Sep 19, 2022

Thanks for trying it out. It seems there might be differences in MySQL installations, but the documentation clearly states that if a precision is specified for the column type, then the same precision needs to be used for the default value. Instead of adding precision: nil, and that way overriding Active Record's default, I think it would make more sense to use CURRENT_TIMESTAMP(6) for the default value. Could you test whether this works for you?

@janko
Copy link
Owner

janko commented Sep 19, 2022

I suspect my MySQL installation was reusing the database created for MariaDB, so I just re-created it locally, and also updated to latest macOS version, and I was able to finally reproduce the issue 🤘🏻

@janko janko closed this as completed in 7db38af Sep 19, 2022
@adamlofting
Copy link
Author

Excellent, thanks for responding so quickly.

I can also confirm that CURRENT_TIMESTAMP(6) works here too.

@zavan
Copy link
Contributor

zavan commented Mar 27, 2023

FYI I'm using the latest MySQL 5.7 and Rails 7 and had to use CURRENT_TIMESTAMP(6) for it to work.

zavan added a commit to zavan/rodauth-oauth that referenced this issue Mar 27, 2023
3 changes:

1. Changed ids and foreign key columns to use `bigint` instead of `int`. Rodauth (and Rails) now uses `bigint` columns for the ids, so using `int` for the foreign keys doesn't work.

2. Added missing `index` keys for some unique fields to fix `ArgumentError: Unknown key: :unique.` error.

3. Rails now uses precision 6 for `datetime` fields by default, so the default values must also specify that. See [this](janko/rodauth-rails#159).
@janko
Copy link
Owner

janko commented Mar 27, 2023

@zavan I see from your other activity that you're using the Trilogy adapter, which isn't currently being handled here (only mysql2). I can add it to the conditional.

@zavan
Copy link
Contributor

zavan commented Mar 27, 2023

@janko I had to go back to mysql2 since Sequel doesn't support trilogy, so no worries. Thanks!

@janko
Copy link
Owner

janko commented May 4, 2023

For anyone interested, the Trilogy adapter has landed in Sequel master, so it will be part of the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants