X Tutup
Skip to content

Fix columns with utc_timestamp default#77

Open
c9845 wants to merge 4 commits intomysql2sqlite:masterfrom
c9845:fix-utc-timestamp-columns
Open

Fix columns with utc_timestamp default#77
c9845 wants to merge 4 commits intomysql2sqlite:masterfrom
c9845:fix-utc-timestamp-columns

Conversation

@c9845
Copy link
Contributor

@c9845 c9845 commented Aug 24, 2020

Replace utc_timestamp with CURRENT_TIMESTAMP.

MySQL allows using UTC_TIMESTAMP as the default value for a column (i.e. DatetimeCreated DATETIME DEFAULT UTC_TIMESTAMP). This is useful in MySQL since the timezone of the default value will always be UTC. Compare this against using CURRENT_TIMESTAMP where the timezone of the default value will be the timezone of the system or server.

UTC_TIMESTAMP does not exists in SQLite; SQLite only uses CURRENT_TIMESTAMP and it returns a value in the UTC timezone. So if we want to convert from MySQL to SQLite, we need to convert UTC_TIMESTAMP to CURRENT_TIMESTAMP.

c9845 added 3 commits August 24, 2020 14:25
…ect the "direct pipe" method as noted in the README.

Direct piping to sqlite3 is typically how this script will be used since you get a sqlite database file as a result.
MySQL allows using UTC_TIMESTAMP as the default value for a column (i.e. DatetimeCreated DATETIME DEFAULT UTC_TIMESTAMP).  This is useful in MySQL since the timezone of the default value will always be UTC.  Compare this against using CURRENT_TIMESTAMP where the timezone of the default value will be the timezone of the system or server.

UTC_TIMESTAMP does not exists in SQLite; SQLite only uses CURRENT_TIMESTAMP and it returns a value in the UTC timezone.  So if we want to convert from MySQL to SQLite, we need to convert UTC_TIMESTAMP to CURRENT_TIMESTAMP.
/ (PRIMARY |primary )?(KEY|key)/ { gsub( /\([0-9]+\)/, "" ) }

# Replace utc_timestamp with CURRENT_TIMESTAMP. utc_timestamp does not exist on sqlite but sqlites CURRENT_TIMESTAMP uses utc timezone.
/ (utc_timestamp)/ { gsub( /utc_timestamp/, "current_timestamp" ) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please take a look at the rather bigger PR I referenced? We need thorough testing and there are good examples.

Could you also make both your PRs mutually non-conflicting?

My main concern is though, that this change seems extremely dangerous. The other such generic substitutions are usually based on the fact, that the input string always appears in the input and thus they match once and then not any more. But utc_timestamp is not such a regular case and thus this will much more likely destroy the values in the DB.

But maybe I'm missing something (one thing I know for sure I'm missing is time).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, not sure how I did that with the conflicting commits...brain fart or something. I think I fixed it.

In regards to danger, I agree, but I can't figure out another way to handle this. I tried putting the gsub inside the /^(CREATE... block but it won't run there. It would probably help if I understood awk a bit better. I will try to dive in more.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, I'd try to get rid of gsub in favor of sub. Second, there are few variables (inTrigger inView aInc firstInTable) designating the scope (and state) where the parsing currently takes place. This way you can easily detect whether you're in a CREATE scope or not.

@c9845 c9845 changed the title Fix columsn with utc_timestamp default Fix columns with utc_timestamp default Aug 24, 2020
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

Successfully merging this pull request may close these issues.

2 participants

X Tutup