| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,8 +13,12 @@ | |||
| 13 | 13 | # See the License for the specific language governing permissions and | |
| 14 | 14 | # limitations under the License. | |
| 15 | 15 | """Handles execution of migrations.""" | |
| 16 | + | ||
| 17 | + from __future__ import annotations | ||
| 18 | + | ||
| 16 | 19 | import datetime | |
| 17 | 20 | import logging | |
| 21 | + from typing import Any, Dict, Optional | ||
| 18 | 22 | ||
| 19 | 23 | from spanner_orm import api | |
| 20 | 24 | from spanner_orm import error | |
@@ -24,34 +28,37 @@ | |||
| 24 | 28 | from spanner_orm.admin import migration_status | |
| 25 | 29 | from spanner_orm.admin import update | |
| 26 | 30 | ||
| 31 | + from google.auth import credentials as auth_credentials | ||
| 32 | + | ||
| 27 | 33 | _logger = logging.getLogger(__name__) | |
| 28 | 34 | ||
| 29 | 35 | ||
| 30 | - class MigrationExecutor(object): | ||
| 36 | + class MigrationExecutor: | ||
| 31 | 37 | """Handles execution of migrations.""" | |
| 32 | 38 | ||
| 33 | 39 | def __init__(self, | |
| 34 | - instance, | ||
| 35 | - database, | ||
| 36 | - project=None, | ||
| 37 | - credentials=None, | ||
| 38 | - basedir=None): | ||
| 40 | + instance: str, | ||
| 41 | + database: str, | ||
| 42 | + project: Optional[str] = None, | ||
| 43 | + credentials: Optional[auth_credentials.Credentials] = None, | ||
| 44 | + basedir: Optional[str] = None): | ||
| 39 | 45 | self._manager = migration_manager.MigrationManager(basedir) | |
| 40 | 46 | self._migration_status_map = None | |
| 41 | 47 | self._instance = instance | |
| 42 | 48 | self._database = database | |
| 43 | 49 | self._project = project | |
| 44 | 50 | self._credentials = credentials | |
| 45 | 51 | ||
| 46 | - def migrated(self, migration_id): | ||
| 52 | + def migrated(self, migration_id: str) -> bool: | ||
| 47 | 53 | if migration_id is None: | |
| 48 | 54 | return True | |
| 49 | 55 | return self._migration_status().get(migration_id, False) | |
| 50 | 56 | ||
| 51 | - def migrations(self): | ||
| 57 | + # TODO(dbrandao): make a Migration object so this is no longer Any | ||
| 58 | + def migrations(self) -> Any: | ||
| 52 | 59 | return self._manager.migrations | |
| 53 | 60 | ||
| 54 | - def migrate(self, target_migration=None): | ||
| 61 | + def migrate(self, target_migration: Optional[str] = None) -> None: | ||
| 55 | 62 | """Executes unmigrated migrations on the curent database. | |
| 56 | 63 | ||
| 57 | 64 | Note: SpannerApi and SpannerAdminApi connections are modified as a result | |
@@ -79,7 +86,7 @@ def migrate(self, target_migration=None): | |||
| 79 | 86 | self._update_status(migration.migration_id, True) | |
| 80 | 87 | self._hangup() | |
| 81 | 88 | ||
| 82 | - def rollback(self, target_migration): | ||
| 89 | + def rollback(self, target_migration: str) -> None: | ||
| 83 | 90 | """Rolls back migrated migrations on the curent database. | |
| 84 | 91 | ||
| 85 | 92 | Note: SpannerApi and SpannerAdminApi connections are modified as a result | |
@@ -110,7 +117,7 @@ def rollback(self, target_migration): | |||
| 110 | 117 | self._update_status(migration.migration_id, False) | |
| 111 | 118 | self._hangup() | |
| 112 | 119 | ||
| 113 | - def _connect(self): | ||
| 120 | + def _connect(self) -> None: | ||
| 114 | 121 | admin_api.SpannerAdminApi.connect( | |
| 115 | 122 | self._instance, | |
| 116 | 123 | self._database, | |
@@ -122,11 +129,12 @@ def _connect(self): | |||
| 122 | 129 | project=self._project, | |
| 123 | 130 | credentials=self._credentials) | |
| 124 | 131 | ||
| 125 | - def _hangup(self): | ||
| 132 | + def _hangup(self) -> None: | ||
| 126 | 133 | admin_api.SpannerAdminApi.hangup() | |
| 127 | 134 | api.SpannerApi.hangup() | |
| 128 | 135 | ||
| 129 | - def _filter_migrations(self, migrations, migrated, last_migration): | ||
| 136 | + def _filter_migrations(self, migrations: Any, migrated: bool, | ||
| 137 | + last_migration: Optional[str]) -> Any: | ||
| 130 | 138 | """Filters the list of migrations according to the desired conditions. | |
| 131 | 139 | ||
| 132 | 140 | Args: | |
@@ -153,7 +161,7 @@ def _filter_migrations(self, migrations, migrated, last_migration): | |||
| 153 | 161 | last_migration)) | |
| 154 | 162 | return filtered | |
| 155 | 163 | ||
| 156 | - def _migration_status(self): | ||
| 164 | + def _migration_status(self) -> Dict[str, bool]: | ||
| 157 | 165 | """Gathers from Spanner which migrations have been executed.""" | |
| 158 | 166 | if self._migration_status_map is None: | |
| 159 | 167 | model_from_db = metadata.SpannerMetadata.model( | |
@@ -167,7 +175,7 @@ def _migration_status(self): | |||
| 167 | 175 | ||
| 168 | 176 | return self._migration_status_map | |
| 169 | 177 | ||
| 170 | - def _update_status(self, migration_id, new_status): | ||
| 178 | + def _update_status(self, migration_id: str, new_status: bool) -> None: | ||
| 171 | 179 | """Updates migration status in the database for the given migration.""" | |
| 172 | 180 | new_model = migration_status.MigrationStatus({ | |
| 173 | 181 | 'id': migration_id, | |
@@ -178,7 +186,7 @@ def _update_status(self, migration_id, new_status): | |||
| 178 | 186 | None, [new_model], force_write=True) | |
| 179 | 187 | self._migration_status()[migration_id] = new_status | |
| 180 | 188 | ||
| 181 | - def _validate_migrations(self): | ||
| 189 | + def _validate_migrations(self) -> None: | ||
| 182 | 190 | """Validates the migration status of all migrations makes sense.""" | |
| 183 | 191 | migrations = self.migrations() | |
| 184 | 192 | if not migrations: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,28 +13,33 @@ | |||
| 13 | 13 | # See the License for the specific language governing permissions and | |
| 14 | 14 | # limitations under the License. | |
| 15 | 15 | """Handles reading and writing of migration files.""" | |
| 16 | + | ||
| 17 | + from __future__ import annotations | ||
| 18 | + | ||
| 19 | + | ||
| 16 | 20 | import datetime | |
| 17 | 21 | import importlib | |
| 18 | 22 | import os | |
| 19 | 23 | import re | |
| 20 | 24 | import string | |
| 25 | + from typing import Any, Iterable, List, Optional | ||
| 21 | 26 | import uuid | |
| 22 | 27 | ||
| 23 | 28 | from spanner_orm import error | |
| 24 | 29 | ||
| 25 | 30 | ||
| 26 | - class MigrationManager(object): | ||
| 31 | + class MigrationManager: | ||
| 27 | 32 | """Handles reading and writing of migration files.""" | |
| 28 | 33 | DEFAULT_DIRECTORY = 'migrations' | |
| 29 | 34 | ||
| 30 | - def __init__(self, basedir=None): | ||
| 35 | + def __init__(self, basedir: Optional[str] = None): | ||
| 31 | 36 | self.basedir = basedir or self.DEFAULT_DIRECTORY | |
| 32 | 37 | self._migrations = None | |
| 33 | 38 | ||
| 34 | 39 | if not os.path.exists(self.basedir): | |
| 35 | 40 | os.makedirs(self.basedir) | |
| 36 | 41 | ||
| 37 | - def generate(self, migration_name): | ||
| 42 | + def generate(self, migration_name: str) -> str: | ||
| 38 | 43 | """Creates a new migration that is the last migration to be executed.""" | |
| 39 | 44 | migration_id = uuid.uuid4().hex[-12:] | |
| 40 | 45 | prev_id = self.migrations[-1].migration_id if self.migrations else None | |
@@ -58,14 +63,14 @@ def generate(self, migration_name): | |||
| 58 | 63 | return filepath | |
| 59 | 64 | ||
| 60 | 65 | @property | |
| 61 | - def migrations(self): | ||
| 66 | + def migrations(self) -> Any: | ||
| 62 | 67 | """Loads and orders all migrations in the base dir.""" | |
| 63 | 68 | if self._migrations is None: | |
| 64 | 69 | unordered_migrations = self._all_migrations() | |
| 65 | 70 | self._migrations = self._order_migrations(unordered_migrations) | |
| 66 | 71 | return self._migrations | |
| 67 | 72 | ||
| 68 | - def _migration_from_file(self, filename): | ||
| 73 | + def _migration_from_file(self, filename: str) -> Any: | ||
| 69 | 74 | """Loads a single migration from the given filename in the base dir.""" | |
| 70 | 75 | module_name = re.sub(r'\W', '_', filename) | |
| 71 | 76 | path = os.path.join(self.basedir, filename) | |
@@ -76,7 +81,7 @@ def _migration_from_file(self, filename): | |||
| 76 | 81 | raise error.SpannerError('{} has no migration id'.format(path)) | |
| 77 | 82 | return module | |
| 78 | 83 | ||
| 79 | - def _all_migrations(self): | ||
| 84 | + def _all_migrations(self) -> List[Any]: | ||
| 80 | 85 | """Loads all migrations from the base dir.""" | |
| 81 | 86 | migrations = [] | |
| 82 | 87 | for filename in os.listdir(self.basedir): | |
@@ -85,7 +90,7 @@ def _all_migrations(self): | |||
| 85 | 90 | migrations.append(self._migration_from_file(filename)) | |
| 86 | 91 | return migrations | |
| 87 | 92 | ||
| 88 | - def _order_migrations(self, migrations): | ||
| 93 | + def _order_migrations(self, migrations: Iterable[Any]) -> List[Any]: | ||
| 89 | 94 | """Returns list of migrations in the order they have to be applied.""" | |
| 90 | 95 | if not migrations: | |
| 91 | 96 | return [] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,16 +13,21 @@ | |||
| 13 | 13 | # See the License for the specific language governing permissions and | |
| 14 | 14 | # limitations under the License. | |
| 15 | 15 | """Entry point for spanner_orm scripts.""" | |
| 16 | + | ||
| 17 | + from __future__ import annotations | ||
| 18 | + | ||
| 16 | 19 | import argparse | |
| 20 | + from typing import Any | ||
| 21 | + | ||
| 17 | 22 | from spanner_orm.admin import migration_manager | |
| 18 | 23 | ||
| 19 | 24 | ||
| 20 | - def generate(args): | ||
| 25 | + def generate(args: Any) -> None: | ||
| 21 | 26 | manager = migration_manager.MigrationManager(args.directory) | |
| 22 | 27 | manager.generate(args.name) | |
| 23 | 28 | ||
| 24 | 29 | ||
| 25 | - def main(as_module=False): | ||
| 30 | + def main(as_module: bool = False) -> None: | ||
| 26 | 31 | prog = 'spanner-orm' if as_module else None | |
| 27 | 32 | parser = argparse.ArgumentParser(prog=prog) | |
| 28 | 33 | subparsers = parser.add_subparsers( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments