示例#1
0
 def service_runner():
     run_args = [
         script_path,
         *command,
     ]
     process.call_process(run_args=run_args)
     should_resurrect = eternal
     try:
         should_resurrect = input_queue.get_nowait()
     except Empty:
         pass
     if should_resurrect:
         raise MicraResurrect
示例#2
0
def restore(data_dragon: DataDragon, data_context: DataContext):
  database_name = data_dragon.environment['databases']['default']['database']
  if not data_dragon.user.present_confirmation(f'Drop the {database_name} database and replace it with dumped data at\n{data_context.data_path}', default_response=data_dragon.use_the_force):
    return
  drop_run_args = [
    'mongo',
    database_name,
    '--eval',
    'db.dropDatabase()',
  ]
  drop_return_code = process.call_process(run_args=drop_run_args)
  assert drop_return_code == 0
  run_args = [
    'mongorestore',
    '--dir', str(data_context.data_path / database_name),
    '-d', database_name,
    '--nsInclude', f'{database_name}.*',
  ]
  return_code = process.call_process(run_args=run_args)
  assert return_code == 0
示例#3
0
def dump(ctx: any, data_dragon: DataDragon, data_context: DataContext):
  ctx.invoke(
    purge
  )
  data_context.data_path.mkdir()
  run_args=[
    'mongodump',
    '-o', str(data_context.data_path),
    '-d', data_dragon.environment['databases']['default']['database'],
  ]
  return_code = process.call_process(run_args=run_args)
  assert return_code == 0
示例#4
0
  def run_local_process(self, run_args: List[str], confirm: bool=True, capture_output: bool=False, check_completed: bool=True, shell: bool=False) -> Optional[Tuple[int, Optional[bytes], Optional[bytes]]]:
    process_description = " ".join(process.escape_run_args(run_args))
    if confirm and not self.user.present_confirmation(
      f'Run {process_description}',
      default_response=self.use_the_force
    ):
      return None

    if capture_output:
      result = process.run_process_output(run_args=run_args, shell=shell)
    else:
      result = (process.call_process(run_args=run_args, shell=shell), None, None)

    if check_completed and result[0] != 0:
      raise click.ClickException(f'Process returned a non-zero exit code {result[0]}:\n{process_description}')
    return result
示例#5
0
def migrate(data_dragon: DataDragon, data_context: DataContext, from_version: str, to_version: str):
  script_name = f'{from_version}__to__{to_version}'
  query_path = Path(__file__).parent.parent / 'migrate' / 'queries' / f'{script_name}.json'
  script_path = Path(__file__).parent.parent / 'migrate' / 'scripts' / f'{script_name}.py'
  javascript_path = Path(__file__).parent.parent / 'migrate' / 'scripts' / f'{script_name}.js'
  assert query_path.exists() or script_path.exists() or javascript_path.exists()

  queries = []
  if query_path.exists():
    query_url = f'{query_path}?locator=1&encoding=utf-8'
    query_locator = locator_factory(url=query_url)
    query_text = query_locator.get()
    queries.append(SQL.Query(query=query_text))

  layer = SQL.Layer()
  layer.connect()

  def run_query(query: SQL.Query, confirmation: Optional[str]='Run this migration query?') -> Optional[any]:
    log.log(query.substituted_query)
    if confirmation is not None and not data_dragon.user.present_confirmation(
      confirmation.format(query=query),
      default_response=True
    ):
      return None
    return query.run(sql_layer=layer)

  if script_path.exists():
    data_dragon.user.locals = {
      **data_dragon.user.python_locals,
      'log': log,
      'user': data_dragon.user,
      'SQL': SQL,
      'layer': layer,
      'queries': queries,
      'run_query': run_query,
      'db': layer.get_database(),
    }
    data_dragon.user.script_directory_components = [
      'migrate',
      'scripts',
    ]
    data_dragon.user.run_script(script_name=script_name)
  
  if javascript_path.exists():
    if data_dragon.user.present_confirmation(
      f'{javascript_path.read_text()}\n\nRun this script from {javascript_path}',
      default_response=True
    ):
      process.call_process(run_args=[
        'node',
        str(javascript_path),
      ])

  for index, query in enumerate(queries):
    run_query(
      query=query,
      confirmation=f'Run migration query {index + 1} of {len(queries)}?'
    )

  layer.commit()
  layer.disconnect()