Пример #1
0
def init(db: str = None, schema: str = "schema.sql", overwrite: bool = False):
    """
    Create an initial schema SQL file, optionally from an existing database.
    :param db: An optional database to create the schema from.
    :param schema: An optional file to write schema to. Default: schema.sql
    :param overwrite: Overwrite existing file.
    """
    if os.path.exists(schema) and not overwrite:
        print(cf.bold_red("Error:"),
              f'File "{schema}" already exists.',
              file=sys.stderr)
        print("Run again with", cf.bold("--overwrite"), "to replace.")
        sys.exit(os.EX_OSFILE)

    if not db:
        with open(schema, "w") as f:
            f.write("")

        print(cf.bold("All done! ✨"))
        print(f'Created blank file "{schema}"')
        sys.exit()

    base_uri = copy_url(db)
    target_exists = database_exists(base_uri, test_can_select=True)

    if not target_exists:
        print(
            cf.bold_red("Error:"),
            f'Database "{base_uri.database}" does not exist.',
        )
        sys.exit(os.EX_NOHOST)

    sql = ""

    patch = create_admin_patch(base_uri)
    patch.start()

    with temporary_database(base_uri) as sTemp, S(db) as sFrom:
        # Compare
        m = Migration(sTemp, sFrom)
        m.add_all_changes()
        m.set_safety(False)

        # Get SQL
        sql = m.sql

    with open(schema, "wb") as f:
        f.write(pg_format(sql.encode(), unquote=False))

    print(cf.bold("All done! ✨"))
    print(f'Created file "{schema}" with schema from "{base_uri.database}"')
    sys.exit()
Пример #2
0
def mock_retrieve_company_non_find_a_supplier(retrieve_profile_data):
    retrieve_profile_data['is_published_find_a_supplier'] = (False)
    patch = mock.patch.object(api_client.company,
                              'retrieve_public_profile',
                              return_value=create_response(
                                  200, retrieve_profile_data))
    yield patch.start()
    patch.stop()
Пример #3
0
def mock_retrieve_supplier():
    data = {'company': None}
    response = create_response(data)
    patch = mock.patch.object(api_client.supplier,
                              'retrieve_profile',
                              return_value=response)
    yield patch.start()
    patch.stop()
    def setup(self):
        self._setting_override = override_settings(
            TESTING=True,
            DEBUG=True,
            CACHES={
                "default": {
                    "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
                }
            },
            CELERY_TASK_EAGER_PROPAGATES=True,
            CELERY_TASK_ALWAYS_EAGER=True,
            CELERY_BROKER_BACKEND="memory",
        )
        self._setting_override.__enter__()

        self._patches = self.patches()
        for patch in self._patches:
            patch.start()
Пример #5
0
 def on_train_start(self):
     if kwargs.get("amp_backend") != "apex":
         return
     # extremely ugly. APEX patches all the native torch optimizers on `_initialize` which we call on
     # `ApexMixedPrecisionPlugin.dispatch`. Additionally, their replacement `new_step` functions are locally
     # defined so can't even patch those, thus we need to create the mock after APEX has been initialized
     nonlocal apex_optimizer_patches, apex_optimizer_steps
     for opt in self.trainer.optimizers:
         # `amp.scale_loss` will also patch the step to avoid it when gradient overflow happens. avoid it
         opt._amp_stash.already_patched = True
         patch = mock.patch.object(opt, "step")
         apex_optimizer_patches.append(patch)
         apex_optimizer_steps.append(patch.start())
Пример #6
0
def mock_retrieve_company():
    data = {
        'name': 'Cool Company',
        'is_publishable': True,
        'expertise_products_services': {},
        'is_identity_check_message_sent': False,
    }
    response = create_response(data)
    patch = mock.patch.object(api_client.company,
                              'profile_retrieve',
                              return_value=response)
    yield patch.start()
    patch.stop()
Пример #7
0
 def _start_patches(self, *patches):
     self._current_patches.append(patches)
     for patch in patches:
         patch.start()
Пример #8
0
def diff(
    sql_statements: str,
    db: str,
    unsafe: bool = False,
    apply: bool = False,
    chatty: bool = False,
):
    base_uri = copy_url(db)
    patch = create_admin_patch(base_uri)
    patch.start()

    roles, statements = extract_roles(sql_statements)

    with temporary_database(base_uri) as sTemp:
        roles_m = Migration(sTemp, NullInspector())
        from_roles = roles_m.changes.i_from.roles

        # Exclude all unspecified roles
        for k in set(from_roles.keys()) - set(roles.keys()):
            del from_roles[k]

        # Compare roles
        roles_m.add(statements_for_changes(from_roles, roles))

        if roles_m.statements:
            roles_m.set_safety(True)
            roles_sql = roles_m.sql

            if chatty:
                print(pg_format(roles_sql.encode(), unquote=False).decode())
                print(cf.bold("Applying roles..."))

            roles_m.apply()
            sTemp.commit()

            if chatty:
                print(cf.bold("Done."))

        target_exists = database_exists(base_uri, test_can_select=True)

        with S(db) if target_exists else temporary_database(base_uri) as sFrom:
            # Run schema in temporary database
            try:
                raw_execute(sTemp, statements)
            except Exception as e:
                raise SQLSyntaxError(e, statements)

            # Compare
            m = Migration(sFrom, sTemp)
            m.set_safety(not unsafe)
            m.add_all_changes(privileges=True)

            if not m.statements:
                if chatty:
                    print(cf.bold("All done! ✨"))
                    print(f'Database "{base_uri.database}" is up to date.')

                return None, False

            sql = ""

            # Get SQL
            try:
                sql = m.sql
            except UnsafeMigrationException:
                m.set_safety(False)
                sql = m.sql
                formatted = pg_format(sql.encode(), unquote=False).decode()
                return formatted, True

            formatted = pg_format(sql.encode(), unquote=False).decode()

            if chatty:
                print(formatted)

            if apply:
                if chatty:
                    print(cf.bold("Applying..."))

                m.apply()

                if chatty:
                    print(cf.bold("All done! ✨"))
                    print(f'Database "{base_uri.database}" has been updated.')

            return formatted, False
Пример #9
0
 def _start_patches(self, *patches):
     self._current_patches.append(patches)
     for patch in patches:
         patch.start()