-
Notifications
You must be signed in to change notification settings - Fork 63
Upgrade sqlalchemy
#61
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
Open
hartikainen
wants to merge
5
commits into
google-deepmind:main
Choose a base branch
from
hartikainen:upgrade-sqlalchemy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b3bef48
Fix `job_data` typing in `insert_vertex_job`
hartikainen 5bfdec2
Upgrade `alembic==1.16.5` and `sqlalchemy==1.4.54`
hartikainen e7d9652
Add simple test for database
hartikainen 45b0e8b
Fix code to work with `sqlalchemy==1.4.54`
hartikainen 09291e8
Upgrade `sqlalchemy==2.0.43`
hartikainen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import unittest | ||
| import tempfile | ||
| import os | ||
| import sqlalchemy | ||
| from sqlalchemy import inspect | ||
| from alembic.config import Config | ||
| from alembic import command | ||
| from alembic import script as alembic_script | ||
|
|
||
| class AlembicMigrationTest(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.temp_db_file = tempfile.NamedTemporaryFile(suffix=".db", delete=False) | ||
| self.temp_db_file.close() | ||
| self.db_url = f"sqlite:///{self.temp_db_file.name}" | ||
|
|
||
| alembic_dir = os.path.join( | ||
| os.path.dirname(__file__), 'alembic' | ||
| ) | ||
| self.alembic_cfg = Config(os.path.join(alembic_dir, 'alembic.ini')) | ||
| self.alembic_cfg.set_main_option("script_location", alembic_dir) | ||
| self.alembic_cfg.set_main_option("sqlalchemy.url", self.db_url) | ||
|
|
||
| self.engine = sqlalchemy.create_engine(self.db_url) | ||
|
|
||
| def tearDown(self): | ||
| os.unlink(self.temp_db_file.name) | ||
|
|
||
| def test_migrations_upgrade_and_downgrade(self): | ||
| with self.engine.connect() as connection: | ||
| inspector = inspect(connection) | ||
| self.assertEqual(inspector.get_table_names(), []) | ||
|
|
||
| print(f"Upgrading database to head: {self.db_url}") | ||
| command.upgrade(self.alembic_cfg, "head") | ||
|
|
||
| # Verify schema after upgrade | ||
| with self.engine.connect() as connection: | ||
| inspector = inspect(connection) | ||
| expected_tables = ['experiment', 'work_unit', 'job'] # Example tables from your existing migration | ||
| actual_tables = inspector.get_table_names() | ||
|
|
||
| self.assertGreaterEqual(len(actual_tables), len(expected_tables), | ||
| "Not all expected tables were created during upgrade.") | ||
| for table in expected_tables: | ||
| self.assertIn(table, actual_tables, f"Table '{table}' not found after upgrade.") | ||
|
|
||
| experiment_columns = [c['name'] for c in inspector.get_columns('experiment')] | ||
| self.assertIn('experiment_id', experiment_columns) | ||
| self.assertIn('experiment_title', experiment_columns) | ||
|
|
||
| print(f"Attempting to downgrade database to base: {self.db_url}") | ||
| with self.assertRaises(RuntimeError): | ||
| command.downgrade(self.alembic_cfg, "base") | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| """Tests for xmanager.xm_local.storage.database.""" | ||
|
|
||
| import unittest | ||
| from unittest import mock | ||
| import os | ||
| import tempfile | ||
| from xmanager.xm_local.storage import database as db_module | ||
| from xmanager.xm_local import experiment as local_experiment | ||
|
|
||
| class DatabaseTest(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.temp_dir = tempfile.TemporaryDirectory() | ||
| self.db_path = os.path.join(self.temp_dir.name, 'db.sqlite') | ||
|
|
||
| settings = db_module.SqlConnectionSettings(backend='sqlite', db_name=self.db_path) | ||
|
|
||
| self.database = db_module.Database(db_module.SqliteConnector, settings) | ||
|
|
||
| self.patcher = mock.patch('xmanager.xm_local.experiment.database.database', return_value=self.database) | ||
| self.mock_db = self.patcher.start() | ||
|
|
||
| def tearDown(self): | ||
| self.patcher.stop() | ||
| self.temp_dir.cleanup() | ||
|
|
||
| def test_create_experiment(self): | ||
| with local_experiment.create_experiment(experiment_title='test_experiment_1') as experiment: | ||
| self.assertIsNotNone(experiment.experiment_id) | ||
|
|
||
| with self.database.engine.connect() as connection: | ||
| result = connection.execute(db_module.text("SELECT * FROM experiment")) | ||
| rows = result.all() | ||
| self.assertEqual(len(rows), 1) | ||
| self.assertEqual(rows[0].experiment_title, 'test_experiment_1') | ||
|
|
||
| with local_experiment.create_experiment(experiment_title='test_experiment_2') as experiment: | ||
| self.assertIsNotNone(experiment.experiment_id) | ||
|
|
||
| with self.database.engine.connect() as connection: | ||
| result = connection.execute(db_module.text("SELECT * FROM experiment")) | ||
| rows = result.all() | ||
| self.assertEqual(len(rows), 2) | ||
| self.assertEqual(rows[1].experiment_title, 'test_experiment_2') | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seemed like a bug to me. The database column for
job_datais of typesa.String(255), so I think the data should be formatted as string and not bytes.MessageToStringis also in line with whatinsert_kubernetes_jobbelow does already.