def init_storage(self, force_migrate=False, recreate_materialized_views: Union[bool, list] = False): """Check if the necessary tables (for events, and for the entities in gobmodel) are present If not, they are required :param recreate_materialized_views: List of mv's to recreate, True for all, False for none """ MIGRATION_LOCK = 19935910 # Just some random number if not force_migrate: # Don't force # Nicely wait for any migrations to finish before continuing self.engine.execute(f"SELECT pg_advisory_lock({MIGRATION_LOCK})") try: # Check if storage is up-to-date alembic_cfg = alembic.config.Config('alembic.ini') script = alembic.script.ScriptDirectory.from_config(alembic_cfg) with self.engine.begin() as conn: context = migration.MigrationContext.configure(conn) up_to_date = context.get_current_revision( ) == script.get_current_head() if not up_to_date: print('Migrating storage') alembicArgs = [ '--raiseerr', 'upgrade', 'head', ] alembic.config.main(argv=alembicArgs) # refresh reflected base self._set_base(update=True) # Create necessary indexes self._init_indexes() # Initialise materialized views for relations self._init_relation_materialized_views(recreate_materialized_views) # Create model views. # Should happen after initialisation of materialized views because views may depend on mvs self._init_views() except Exception as e: print(f'Storage migration failed: {str(e)}') else: # No exception print('Storage is up-to-date') finally: # Always unlock self.engine.execute(f"SELECT pg_advisory_unlock({MIGRATION_LOCK})") self._check_configuration()
def migrate_storage(force_migrate): """ Migrate storage to latest version. In order to prevent that multiple instances will migrate at the same time and to prevent migration locks, access to this method is normally acquired by a lock. This method will always unlock the lock, even if a lock has not been set. When using the force_migrate option the lock is passed and any open lock will be released The reason for setting the lock is to prevent multiple migrations that might lock each other :return: """ MIGRATION_LOCK = 2480442490 # Just some random number if not force_migrate: # Don't force # Nicely wait for any migrations to finish before continuing engine.execute(f"SELECT pg_advisory_lock({MIGRATION_LOCK})") try: # Check if storage is up-to-date alembic_cfg = alembic.config.Config('alembic.ini') script = alembic.script.ScriptDirectory.from_config(alembic_cfg) with engine.begin() as conn: context = migration.MigrationContext.configure(conn) up_to_date = context.get_current_revision( ) == script.get_current_head() if not up_to_date: print('Migrating storage') alembicArgs = [ '--raiseerr', 'upgrade', 'head', ] alembic.config.main(argv=alembicArgs) except Exception as e: print(f'Storage migration failed: {str(e)}') else: # No exception print('Storage is up-to-date') # Always unlock engine.execute(f"SELECT pg_advisory_unlock({MIGRATION_LOCK})")
def get_head_version(self) -> str: script = self._create_alembic_script() return script.get_current_head()