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

22827 - Add sql-versioning library and versioning switching logic #3024

Merged
merged 13 commits into from
Oct 21, 2024

Conversation

chenhongjing
Copy link
Collaborator

@chenhongjing chenhongjing commented Oct 10, 2024

Issue #: /bcgov/entity#22827

Description of changes:

  • Add sql-versioning library as new db versioning, including docstring, doc, tests etc.
  • Import sql-versioning library to legal-api
  • Add db versioning switching logic with feature flag to legal-api
  • Update db models
  • Tweak current db versioning to use custom versioning_manager and Transaction model
  • Update versioning_manager uses across components

Note that

  1. All the coloured debugging messages are kept in the code for further development
  2. Currently versioning switching logic is disabled (setup function is commented out), and only old db versioning is enabled

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the lear license (Apache 2.0).

@chenhongjing chenhongjing self-assigned this Oct 10, 2024
Copy link

gitguardian bot commented Oct 10, 2024

⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

Since your pull request originates from a forked repository, GitGuardian is not able to associate the secrets uncovered with secret incidents on your GitGuardian dashboard.
Skipping this check run and merging your pull request will create secret incidents on your GitGuardian dashboard.

🔎 Detected hardcoded secret in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
9442085 Triggered Generic Password 685ee7a python/common/sql-versioning/tests/docker-compose.yml View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secret safely. Learn here the best practices.
  3. Revoke and rotate this secret.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

@@ -20,7 +20,6 @@
class CorpType(db.Model): # pylint: disable=too-many-instance-attributes
"""This class manages the corp type."""

__versioned__ = {}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Does corp_type have version table? I don't find it so I remove its old versioning.

Copy link
Collaborator

Choose a reason for hiding this comment

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

couldn't find it in DEV db on my side
image

# make_versioned(user_cls=None, plugins=[FlaskPlugin()])
make_versioned(user_cls=None)

class Transaction(db.Model):
Copy link
Collaborator Author

@chenhongjing chenhongjing Oct 11, 2024

Choose a reason for hiding this comment

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

Introduce custom Transaction model to solve FK related issues between filings and transaction tables when we completely remove old versioning and only new versioning exists. Put it in db.py file for now to avoid the import issue when starting up the flask app.

I make old and new versionings use this custom model to make sure there's no conflicts when both of them exist in the code, though they have their own transaction model. And the removal of old versioning will be clean in the future and the core logic of new versioning won't be touched then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let me know if I need to split this into multiple files.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should be fine as one file I think

Comment on lines 244 to 246
if not _is_session_modified(session):
print('\033[31mThere is no modified object in this session.\033[0m')
return
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The logic is updated after the experimental code to make sure that no new transaction entry will be created if the versioned object is not "actually" modified..

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A test postgres db container will be started automatically during testing.

Comment on lines +13 to 25
"db-versioning": {
"legal-api": false,
"emailer": false,
"filer": false,
"entity-bn": false,
"digital-credentials": false,
"dissolutions-job": false,
"furnishings-job": false,
"emailer-reminder-job": false,
"future-effective-job": false,
"update-colin-filings-job": false,
"update-legal-filings-job": false
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we have this flag setup for each environment in LD already?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not yet, and the related setup_versioning() function is commented out so there's no impact. But I'll set them up now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. The flag is off for all env.

Current on value:

 {
    "legal-api": false,
    "emailer": false,
    "filer": false,
    "entity-bn": false,
    "digital-credentials": false,
    "dissolutions-job": false,
    "furnishings-job": false,
    "emailer-reminder-job": false,
    "future-effective-job": false,
    "update-colin-filings-job": false,
    "update-legal-filings-job": false
}

Current off value:

{}

Comment on lines +289 to +291
# TODO: enable versioning switching
# it should be called before data model initialzed, otherwise, old versioning doesn't work properly
# setup_versioning()
Copy link
Collaborator

Choose a reason for hiding this comment

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

so this will be uncommented as a part of the work in #22942 right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes

Copy link
Collaborator

@JazzarKarim JazzarKarim left a comment

Choose a reason for hiding this comment

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

From what I can see, looks great!

I may have missed something as the work here is kind of brand new to myself. I'm up to speed now though after going through this PR.

Comment on lines +170 to +172
# the following operations should not result in new versioned records for native sqlalchemy session
session.add(user)
session.commit()
Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks, very thorough to test this scenario too!

Copy link
Collaborator

Choose a reason for hiding this comment

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

These tests are great!

Will be a great starting point to verify the versioning library and also if we ever need to add other tests.

I don't believe we have any CI for this and I think that's fine for now.

But can you provide a screenshot of the test results when you run things locally?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FYI the test results at local env, and REAME.md has the setup instructions for this work so that anyone can play around it and test it easily. (think I had better update the application name to align with the one in sbc-connect-common repo later).
image

Comment on lines 25 to 27
# git+https://github.com/bcgov/lear.git#egg=legal_api&subdirectory=legal-api
git+https://github.com/chenhongjing/lear.git@22827-sql-versioning#egg=legal_api&subdirectory=legal-api
git+https://github.com/chenhongjing/lear.git@22827-sql-versioning#egg=sql-versioning&subdirectory=python/common/sql-versioning
Copy link
Collaborator

@argush3 argush3 Oct 17, 2024

Choose a reason for hiding this comment

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

So when we are ready to merge, what do all the queue service requirements files look like?

Will it just be a reference to git+https://github.com/bcgov/lear.git#egg=legal_api&subdirectory=legal-api?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We need to add a new dependency - sql-versioning (reference: failed CI), so it will be

git+https://github.com/bcgov/lear.git#egg=legal_api&subdirectory=legal-api
git+https://github.com/bcgov/lear.git#egg=sql-versioning&subdirectory=python/common/sql-versioning

@argush3
Copy link
Collaborator

argush3 commented Oct 17, 2024

Great work!

Will approve once some of the comments I've added have been addressed.

Also, think you'll need to rebase as there are some conflicts with main

Copy link
Collaborator

@argush3 argush3 left a comment

Choose a reason for hiding this comment

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

Approving with assumption following will be addressed:

  • conflict with main
  • dependencies in requirements.txt files will be updated as appropriate

chenhongjing and others added 2 commits October 17, 2024 08:36
Signed-off-by: chenhongjing <[email protected]>
Signed-off-by: Hongjing Chen <[email protected]>
Signed-off-by: Hongjing Chen <[email protected]>
Signed-off-by: Hongjing Chen <[email protected]>
Signed-off-by: Hongjing Chen <[email protected]>
Signed-off-by: Hongjing Chen <[email protected]>
Signed-off-by: Hongjing Chen <[email protected]>
Signed-off-by: Hongjing Chen <[email protected]>
Signed-off-by: Hongjing Chen <[email protected]>
Copy link
Collaborator

@leodube-aot leodube-aot 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! Lots of learning for me going through this

Signed-off-by: Hongjing Chen <[email protected]>
Copy link

sonarcloud bot commented Oct 21, 2024

@chenhongjing
Copy link
Collaborator Author

Just add a test of rollback scenario for sql-versioning, and no significant changes. Going to merge this pr after basic checks are done.

image

@chenhongjing chenhongjing merged commit 4b41a96 into bcgov:main Oct 21, 2024
6 of 23 checks passed
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.

5 participants