Ejemplo n.º 1
0
    def load_schemas(cls):
        script = cls.get_schema_script()
        statements = edgeql.parse_block(script)

        schema = s_std.load_std_schema()
        schema = s_std.load_graphql_schema(schema)

        for stmt in statements:
            if isinstance(stmt, qlast.Delta):
                # CREATE/APPLY MIGRATION
                ddl_plan = s_ddl.cmd_from_ddl(stmt,
                                              schema=schema,
                                              modaliases={None: 'default'})

            elif isinstance(stmt, qlast.DDL):
                # CREATE/DELETE/ALTER (FUNCTION, TYPE, etc)
                ddl_plan = s_ddl.delta_from_ddl(stmt,
                                                schema=schema,
                                                modaliases={None: 'default'})

            else:
                raise ValueError(
                    f'unexpected {stmt!r} in compiler setup script')

            context = sd.CommandContext()
            ddl_plan.apply(schema, context)

        return schema
Ejemplo n.º 2
0
async def plan_statement(stmt, backend, flags={}, *, timer):
    schema = backend.schema
    modaliases = backend.modaliases
    testmode = backend.testmode

    if isinstance(stmt, qlast.Database):
        # CREATE/ALTER/DROP DATABASE
        return s_ddl.cmd_from_ddl(stmt,
                                  schema=schema,
                                  modaliases=modaliases,
                                  testmode=testmode)

    elif isinstance(stmt, qlast.CreateDelta):
        # CREATE MIGRATION
        cmd = s_ddl.cmd_from_ddl(stmt,
                                 schema=schema,
                                 modaliases=modaliases,
                                 testmode=testmode)

        cmd = await backend.compile_migration(cmd)
        return cmd

    elif isinstance(stmt, qlast.Delta):
        # Other MIGRATION commands.
        return s_ddl.cmd_from_ddl(stmt,
                                  schema=schema,
                                  modaliases=modaliases,
                                  testmode=testmode)

    elif isinstance(stmt, qlast.DDL):
        # CREATE/DELETE/ALTER (FUNCTION, TYPE, etc)
        return s_ddl.delta_from_ddl(stmt,
                                    schema=schema,
                                    modaliases=modaliases,
                                    testmode=testmode)

    elif isinstance(stmt, qlast.Transaction):
        # BEGIN/COMMIT
        return TransactionStatement(stmt)

    elif isinstance(stmt, qlast.SetSessionState):
        # SET ...
        with timer.timeit('compile_eql_to_ir'):
            ir = ql_compiler.compile_ast_to_ir(stmt,
                                               schema=schema,
                                               modaliases=modaliases)

        return ir

    else:
        # Queries
        with timer.timeit('compile_eql_to_ir'):
            ir = ql_compiler.compile_ast_to_ir(stmt,
                                               schema=schema,
                                               modaliases=modaliases)

        return backend.compile(ir,
                               output_format=compiler.OutputFormat.JSON,
                               timer=timer)
Ejemplo n.º 3
0
    async def _load_std(self):
        schema = s_schema.Schema()

        current_block = None

        std_texts = []
        for modname in s_schema.STD_LIB + ['stdgraphql']:
            std_texts.append(s_std.get_std_module_text(modname))

        ddl_text = '\n'.join(std_texts)

        for ddl_cmd in edgeql.parse_block(ddl_text):
            delta_command = s_ddl.delta_from_ddl(
                ddl_cmd, schema=schema, modaliases={None: 'std'}, stdmode=True)

            if debug.flags.delta_plan_input:
                debug.header('Delta Plan Input')
                debug.dump(delta_command)

            # Do a dry-run on test_schema to canonicalize
            # the schema delta-commands.
            test_schema = schema
            context = self.create_context(stdmode=True)
            canonical_delta = delta_command.copy()
            canonical_delta.apply(test_schema, context=context)

            # Apply and adapt delta, build native delta plan, which
            # will also update the schema.
            schema, plan = self.process_delta(canonical_delta, schema,
                                              stdmode=True)

            if isinstance(plan, (s_db.CreateDatabase, s_db.DropDatabase)):
                if (current_block is not None and
                        not isinstance(current_block, dbops.SQLBlock)):
                    raise errors.QueryError(
                        'cannot mix DATABASE commands with regular DDL '
                        'commands in a single block')
                if current_block is None:
                    current_block = dbops.SQLBlock()

            else:
                if (current_block is not None and
                        not isinstance(current_block, dbops.PLTopBlock)):
                    raise errors.QueryError(
                        'cannot mix DATABASE commands with regular DDL '
                        'commands in a single block')
                if current_block is None:
                    current_block = dbops.PLTopBlock()

            plan.generate(current_block)

        sql_text = current_block.to_string()

        return schema, sql_text
Ejemplo n.º 4
0
    def _compile_ql_ddl(self, ctx: CompileContext, ql: qlast.DDL):
        current_tx = ctx.state.current_tx()
        schema = current_tx.get_schema()

        cmd = s_ddl.delta_from_ddl(
            ql,
            schema=schema,
            modaliases=current_tx.get_modaliases(),
            testmode=bool(current_tx.get_config().get('__internal_testmode')))

        return self._compile_command(ctx, cmd)
Ejemplo n.º 5
0
async def _init_std_schema(conn):
    logger.info('Bootstrapping std module...')

    stdschema = os.path.join(os.path.dirname(edgedb_schema.__file__),
                             '_std.eql')
    with open(stdschema, 'r') as f:
        stdschema_script = f.read()

    statements = edgeql.parse_block(stdschema_script)

    bk = await backend.open_database(conn)

    for statement in statements:
        cmd = s_ddl.delta_from_ddl(statement,
                                   schema=bk.schema,
                                   modaliases={None: 'std'})
        await bk.run_ddl_command(cmd)

    await metaschema.generate_views(conn, bk.schema)
Ejemplo n.º 6
0
def plan_statement(stmt, backend, flags={}, *, timer):
    schema = backend.schema
    modaliases = backend.modaliases

    if isinstance(stmt, qlast.Database):
        # CREATE/ALTER/DROP DATABASE
        return s_ddl.cmd_from_ddl(stmt, schema=schema, modaliases=modaliases)

    elif isinstance(stmt, qlast.Delta):
        # CREATE/APPLY MIGRATION
        return s_ddl.cmd_from_ddl(stmt, schema=schema, modaliases=modaliases)

    elif isinstance(stmt, qlast.DDL):
        # CREATE/DELETE/ALTER (FUNCTION, TYPE, etc)
        return s_ddl.delta_from_ddl(stmt, schema=schema, modaliases=modaliases)

    elif isinstance(stmt, qlast.Transaction):
        # BEGIN/COMMIT
        return TransactionStatement(stmt)

    elif isinstance(stmt, qlast.SessionStateDecl):
        # SET ...
        with timer.timeit('compile_eql_to_ir'):
            ir = ql_compiler.compile_ast_to_ir(stmt,
                                               schema=schema,
                                               modaliases=modaliases)

        return ir

    else:
        # Queries
        with timer.timeit('compile_eql_to_ir'):
            ir = ql_compiler.compile_ast_to_ir(stmt,
                                               schema=schema,
                                               modaliases=modaliases,
                                               implicit_id_in_shapes=False)

        return backend.compile(ir,
                               output_format=compiler.OutputFormat.JSON,
                               timer=timer)
Ejemplo n.º 7
0
    def run_ddl(cls, schema, ddl):
        statements = edgeql.parse_block(ddl)

        current_schema = schema
        target_schema = None

        for stmt in statements:
            if isinstance(stmt, qlast.CreateDelta):
                # CREATE MIGRATION
                if target_schema is None:
                    target_schema = _load_std_schema()

                ddl_plan = s_ddl.cmd_from_ddl(stmt,
                                              schema=current_schema,
                                              modaliases={None: 'default'})

                ddl_plan = s_ddl.compile_migration(ddl_plan, target_schema,
                                                   current_schema)

            elif isinstance(stmt, qlast.Delta):
                # APPLY MIGRATION
                ddl_plan = s_ddl.cmd_from_ddl(stmt,
                                              schema=current_schema,
                                              modaliases={None: 'default'})

            elif isinstance(stmt, qlast.DDL):
                # CREATE/DELETE/ALTER (FUNCTION, TYPE, etc)
                ddl_plan = s_ddl.delta_from_ddl(stmt,
                                                schema=current_schema,
                                                modaliases={None: 'default'})

            else:
                raise ValueError(
                    f'unexpected {stmt!r} in compiler setup script')

            context = sd.CommandContext()
            current_schema, _ = ddl_plan.apply(current_schema, context)

        return current_schema