Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Feat/backward migrations #1072

Merged
merged 4 commits into from
Jan 30, 2019
Merged

Feat/backward migrations #1072

merged 4 commits into from
Jan 30, 2019

Conversation

patriotyk
Copy link
Contributor

No description provided.

@codecov-io
Copy link

codecov-io commented Jan 29, 2019

Codecov Report

Merging #1072 into master will decrease coverage by <.01%.
The diff coverage is 68.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1072      +/-   ##
==========================================
- Coverage   75.29%   75.28%   -0.01%     
==========================================
  Files         157      157              
  Lines        9036     9065      +29     
==========================================
+ Hits         6804     6825      +21     
- Misses       2232     2240       +8
Impacted Files Coverage Δ
src/libaktualizr/storage/sqlstorage.h 50% <ø> (ø) ⬆️
src/libaktualizr/storage/sqlstorage.cc 74.26% <ø> (-0.04%) ⬇️
src/libaktualizr/storage/sqlstorage_base.h 100% <ø> (ø) ⬆️
src/libaktualizr/storage/sqlstorage_base.cc 79.38% <68.42%> (-2.71%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fcc9105...c0145d4. Read the comment docs.

Signed-off-by: Serhiy Stetskovych <patriotyk@gmail.com>
Signed-off-by: Serhiy Stetskovych <patriotyk@gmail.com>
bool SQLStorageBase::dbMigrateForward(int version_from) {
SQLite3Guard db = dbConnection();
for (int32_t k = version_from + 1; k <= current_schema_version_; k++) {
if (db.exec(schema_migrations_.at(static_cast<size_t>(k)), nullptr, nullptr) != SQLITE_OK) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thoughts:

  • for safety, first insert the rollback migrations, then execute the forward migration. This way, there is no way to lose the backward migrations if the migration process was interrupted. Even cleaner would be to use sqlite save points (https://www.sqlite.org/lang_savepoint.html) to nest all this migration process in a big transaction. Not sure if we can still do that and keep compatibility. It might be fine though, need to think a bit more about it
  • use INSERT OR REPLACE, as we'll probably want to have the more recent versions of these

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to swap, but it doesn't work. Doesn't work because first we need to migrate to create backward_migrations table, then we can insert data in to it. I also tried to add save point but PrepareStatement always fails when I add it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point for swapping, I missed that detail!

I took the liberty to push a version which should work with save points on top of your PR. Please check if it doesn't break your work.

LOG_ERROR << "Can't migrate db from version " << (ver) << " to version " << ver - 1 << ": " << db.errmsg();
return false;
}
if (db.exec(std::string("DELETE FROM migrations WHERE version_from=") + std::to_string(ver) + ";", nullptr,
Copy link
Contributor

Choose a reason for hiding this comment

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

Even though it shouldn't hurt, do you think it's actually needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why not? We don't need this anymore so this data is garbage.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok fair enough, I won't fight on this.

@lbonn
Copy link
Contributor

lbonn commented Jan 29, 2019

Thanks for the update, this is looking good. Sorry for all the nitpick comments, but they should be easy fixes.

Signed-off-by: Serhiy Stetskovych <patriotyk@gmail.com>
With save points inside a transaction

Signed-off-by: Laurent Bonnans <laurent.bonnans@here.com>
@lbonn
Copy link
Contributor

lbonn commented Jan 30, 2019

I've added a commit to use save points for forward migration. It changes our existing migrations scripts which could be controversial but I don't think it has a negative impact.

@patriotyk
Copy link
Contributor Author

I've added a commit to use save points for forward migration. It changes our existing migrations scripts which could be controversial but I don't think it has a negative impact.

Thank you, I was trying to accomplish this without migration scripts modification.

Copy link
Contributor

@lbonn lbonn left a comment

Choose a reason for hiding this comment

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

Looks good to me now but since I've also pushed code there, would be nice to someone else taking a look.

@pattivacek
Copy link
Collaborator

Looks fine to me after a precursory glance, and after discussion with @lbonn, I think it's fine if both of you are happy. It sounds like Laurent wants to do a manual test first, though.

return false;
}
if (db.exec(std::string("DELETE FROM rollback_migrations WHERE version_from=") + std::to_string(ver) + ";", nullptr,
nullptr) != SQLITE_OK) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

DELETE probably won't hurt, but shouldn't be necessary if we add with INSERT OR REPLACE.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here is the original discussion: #1072 (comment). Now I don't feel too strongly about it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, missed that conversation. And I agree, I don't feel strongly, I just wanted to leave the thought.

@lbonn
Copy link
Contributor

lbonn commented Jan 30, 2019

Ok I've added the test I wanted in #1041 (hard to do it here in a convincing fashion) and it passes.

So let's merge!

@lbonn lbonn merged commit 15e17b9 into master Jan 30, 2019
@lbonn lbonn deleted the feat/backward_migrations branch January 30, 2019 16:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants