Exemple #1
0
def apply_sdl(
    documents: Iterable[Tuple[str, Sequence[qlast.DDL]]],
    *,
    target_schema: s_schema.Schema,
    current_schema: s_schema.Schema,
    stdmode: bool = False,
    testmode: bool = False,
) -> s_schema.Schema:
    ddl_stmts = s_decl.sdl_to_ddl(current_schema, documents)
    context = sd.CommandContext(
        modaliases={},
        schema=target_schema,
        stdmode=stdmode,
        testmode=testmode,
        declarative=True,
    )

    for ddl_stmt in ddl_stmts:
        delta = sd.DeltaRoot()
        with context(sd.DeltaRootContext(schema=target_schema, op=delta)):
            cmd = cmd_from_ddl(ddl_stmt,
                               schema=target_schema,
                               modaliases={},
                               context=context,
                               testmode=testmode)

            delta.add(cmd)
            target_schema = delta.apply(target_schema, context)
            context.schema = target_schema

    return target_schema
Exemple #2
0
def apply_sdl(
    sdl_document: qlast.Schema,
    *,
    base_schema: s_schema.Schema,
    current_schema: s_schema.Schema,
    stdmode: bool = False,
    testmode: bool = False,
    allow_dml_in_functions: bool = False,
) -> s_schema.Schema:
    # group declarations by module
    documents: Dict[str, List[qlast.DDL]] = defaultdict(list)
    # initialize the "default" module
    documents[defines.DEFAULT_MODULE_ALIAS] = []
    extensions = {}
    for decl in sdl_document.declarations:
        # declarations are either in a module block or fully-qualified
        if isinstance(decl, qlast.ModuleDeclaration):
            documents[decl.name.name].extend(decl.declarations)
        elif isinstance(decl, qlast.CreateExtension):
            extensions[decl.name.name] = decl
        else:
            assert decl.name.module is not None
            documents[decl.name.module].append(decl)

    ddl_stmts = s_decl.sdl_to_ddl(current_schema, documents)
    context = sd.CommandContext(
        modaliases={},
        schema=base_schema,
        stdmode=stdmode,
        testmode=testmode,
        declarative=True,
        allow_dml_in_functions=allow_dml_in_functions,
    )

    target_schema = base_schema
    for ddl_stmt in itertools.chain(extensions.values(), ddl_stmts):
        delta = sd.DeltaRoot()
        with context(sd.DeltaRootContext(schema=target_schema, op=delta)):
            cmd = cmd_from_ddl(ddl_stmt,
                               schema=target_schema,
                               modaliases={},
                               context=context,
                               testmode=testmode)

            delta.add(cmd)
            target_schema = delta.apply(target_schema, context)
            context.schema = target_schema

    return target_schema
Exemple #3
0
def apply_sdl(
    sdl_document: qlast.Schema,
    *,
    base_schema: s_schema.Schema,
    current_schema: s_schema.Schema,
    stdmode: bool = False,
    testmode: bool = False,
) -> s_schema.Schema:
    # group declarations by module
    documents: Dict[str, List[qlast.DDL]] = defaultdict(list)
    # initialize the "default" module
    documents['default'] = []
    for decl in sdl_document.declarations:
        # declarations are either in a module block or fully-qualified
        if isinstance(decl, qlast.ModuleDeclaration):
            documents[decl.name.name].extend(decl.declarations)
        else:
            documents[decl.name.module].append(decl)

    ddl_stmts = s_decl.sdl_to_ddl(current_schema, documents)
    context = sd.CommandContext(
        modaliases={},
        schema=base_schema,
        stdmode=stdmode,
        testmode=testmode,
        declarative=True,
    )

    target_schema = base_schema
    for ddl_stmt in ddl_stmts:
        delta = sd.DeltaRoot()
        with context(sd.DeltaRootContext(schema=target_schema, op=delta)):
            cmd = cmd_from_ddl(ddl_stmt,
                               schema=target_schema,
                               modaliases={},
                               context=context,
                               testmode=testmode)

            delta.add(cmd)
            target_schema = delta.apply(target_schema, context)
            context.schema = target_schema

    return target_schema