Skip to content

feat(sqlite): Collect and report migration errors before starting the migration#6759

Merged
kodiakhq[bot] merged 4 commits intocloudquery:mainfrom
erezrokah:feat/sqlite_report_migration_errors_before_migration_starts
Jan 19, 2023
Merged

feat(sqlite): Collect and report migration errors before starting the migration#6759
kodiakhq[bot] merged 4 commits intocloudquery:mainfrom
erezrokah:feat/sqlite_report_migration_errors_before_migration_starts

Conversation

@erezrokah
Copy link
Copy Markdown
Member

@erezrokah erezrokah commented Jan 12, 2023

Summary

Related to #6600. Still in draft as I need to test it 😄
At the moment we migrate the tables and columns one by one and stop if we can't migrate due to potential data loss.
This has a few issues

  • We can leave the DB in partial migration state
  • We only report one error, so if there are multiple errors the user has to run the migration multiple times to know what to fix

Notes

  • Didn't add the force flag to keep this PR small as possible. Once we add --force the change will be simply to ignore the errors and keep on migrating (e.g. drop/add columns/tables)
  • SQLite is missing the logic Postgres has to recreate primary keys on existing tables. This is unrelated to this PR so didn't add it, but would be good to align that in a future PR
  • Might be worth moving some of the code to the SDK, but won't do that yet until I see how it looks in other plugins

@cq-bot cq-bot added the sqlite label Jan 12, 2023
switch {
case sqliteColumn == nil:
c.logger.Debug().Str("table", table.Name).Str("column", col.Name).Msg("Column doesn't exist, creating")
sql := "alter table \"" + table.Name + "\" add column \"" + columnName + "\" \"" + columnType + `"`
Copy link
Copy Markdown
Member Author

@erezrokah erezrokah Jan 12, 2023

Choose a reason for hiding this comment

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

We only modify table columns when we add a new one and it's not a PK. I kept that logic

if info, err = c.getTableInfo(table.Name); err != nil {
return fmt.Errorf("failed to get table %s columns types: %w", table.Name, err)
migrationErrors := getMigrationErrors(schemaChanges)
if len(migrationErrors) > 0 {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

When we add the --force flag, we'll log the errors and continue (the code to handle the errors, e.g. dropping a table is not here yet, I'll add that when I add the --force flag)

@erezrokah erezrokah force-pushed the feat/sqlite_report_migration_errors_before_migration_starts branch 5 times, most recently from fa4d5b5 to 0f3ab0a Compare January 12, 2023 13:43
@erezrokah erezrokah marked this pull request as ready for review January 12, 2023 14:01
@erezrokah erezrokah requested review from a team and amanenk and removed request for a team January 12, 2023 14:01
@erezrokah erezrokah requested review from yevgenypats and removed request for amanenk January 12, 2023 15:30
@erezrokah erezrokah force-pushed the feat/sqlite_report_migration_errors_before_migration_starts branch from f8b8186 to 222f60a Compare January 15, 2023 16:28
@erezrokah erezrokah force-pushed the feat/sqlite_report_migration_errors_before_migration_starts branch from 67e1cb4 to e1119f4 Compare January 17, 2023 11:32
@erezrokah
Copy link
Copy Markdown
Member Author

Thanks for the review @hermanschaaf. I'll merge this as it's blocking another PR cc @yevgenypats if you have concerns I can follow up

@erezrokah erezrokah added the automerge Automatically merge once required checks pass label Jan 19, 2023
@kodiakhq kodiakhq bot merged commit a80e9d9 into cloudquery:main Jan 19, 2023
@erezrokah erezrokah deleted the feat/sqlite_report_migration_errors_before_migration_starts branch January 19, 2023 13:00
kodiakhq bot pushed a commit that referenced this pull request Jan 19, 2023

#### Summary

To be merged after #6759, and needs cloudquery/plugin-sdk#604

<!--
kodiakhq bot pushed a commit that referenced this pull request Jan 24, 2023

#### Summary

Related to #6600, #6763 and #6759

This PR adds support for PK changes migration. Before we would just ignore them, so the following use cases were ignored:
1. Adding a new column as a primary key -> The column was added, but the table PKs were left unchanged
2. Removing a PK from an existing column -> Was ignored
3. Adding a PK to an existing column -> Was ignored
4. Changing the type of an existing PK column -> Was handled by a regular non PK column type change. We dropped the column, but we need to drop the table.

SQLite is quite limited in what can be done with PK changes. On any PK change we need to recreate the table, so either `drop + create` or `create new + copy old to new + drop old + rename new`.
I went with the former for simplicity.

<!--
kodiakhq bot pushed a commit that referenced this pull request Jan 24, 2023
🤖 I have created a release *beep* *boop*
---


## [1.2.0](plugins-destination-sqlite-v1.1.6...plugins-destination-sqlite-v1.2.0) (2023-01-24)


### Features

* **sqlite-migrate:** Support PK changes to schema ([#7006](#7006)) ([dddd852](dddd852))
* **sqlite:** Collect and report migration errors before starting the migration ([#6759](#6759)) ([a80e9d9](a80e9d9))
* **sqlite:** Support force migration ([#6763](#6763)) ([19bba77](19bba77))


### Bug Fixes

* **deps:** Update module github.com/cloudquery/plugin-sdk to v1.28.0 ([#7009](#7009)) ([12ac005](12ac005))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Automatically merge once required checks pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants