1414# License for the specific language governing permissions and limitations
1515# under the License.
1616
17+ import logging
18+ import os
19+
20+ from gcl_sdk import migrations as sdk_migrations
21+
1722from restalchemy .storage .sql import migrations
1823
1924
25+ SDK_MIGRATION_FILE_NAME = "0001-universal-agent-391c09.py"
26+
27+
28+ LOG = logging .getLogger (__name__ )
29+
30+
31+ class MigrationEngine (migrations .MigrationEngine ):
32+
33+ def apply_migration (self , migration_name , session ):
34+ filename = self .get_file_name (migration_name )
35+ self ._init_migration_table (session )
36+ migrations = self ._load_migration_controllers (session )
37+
38+ migration = migrations [filename ]
39+ if migration .is_applied ():
40+ LOG .warning ("Migration '%s' is already applied" , migration .name )
41+ else :
42+ LOG .info ("Applying migration '%s'" , migration .name )
43+ migrations [filename ].apply (session , migrations )
44+
45+ def rollback_migration (self , migration_name , session ):
46+ filename = self .get_file_name (migration_name )
47+ self ._init_migration_table (session )
48+ migrations = self ._load_migration_controllers (session )
49+ migration = migrations [filename ]
50+ if not migration .is_applied ():
51+ LOG .warning ("Migration '%s' is not applied" , migration .name )
52+ else :
53+ LOG .info ("Rolling back migration '%s'" , migration .name )
54+ migrations [filename ].rollback (session , migrations )
55+
56+
2057class MigrationStep (migrations .AbstarctMigrationStep ):
2158
2259 def __init__ (self ):
23- self ._depends = ["0018 -init-configs-c3bbc6.py" ]
60+ self ._depends = ["0017 -init-configs-c3bbc6.py" ]
2461
2562 @property
2663 def migration_id (self ):
@@ -30,6 +67,10 @@ def migration_id(self):
3067 def is_manual (self ):
3168 return False
3269
70+ def _get_migration_engine (self ):
71+ sdk_migration_path = os .path .dirname (sdk_migrations .__file__ )
72+ return MigrationEngine (migrations_path = sdk_migration_path )
73+
3374 def upgrade (self , session ):
3475 expressions = [
3576 """
@@ -164,6 +205,9 @@ def upgrade(self, session):
164205 """ ,
165206 ]
166207
208+ migration_engine = self ._get_migration_engine ()
209+ migration_engine .apply_migration (SDK_MIGRATION_FILE_NAME , session )
210+
167211 for expression in expressions :
168212 session .execute (expression )
169213
@@ -185,5 +229,8 @@ def downgrade(self, session):
185229 for table in reversed (tables ):
186230 self ._delete_table_if_exists (session , table )
187231
232+ migration_engine = self ._get_migration_engine ()
233+ migration_engine .rollback_migration (SDK_MIGRATION_FILE_NAME , session )
234+
188235
189236migration_step = MigrationStep ()
0 commit comments