def load_graphql_schema(schema=None): if schema is None: schema = s_schema.Schema() with open(os.path.join(os.path.dirname(__file__), '_graphql.eschema'), 'r') as f: eschema = f.read() script = f''' CREATE MODULE graphql; CREATE MIGRATION graphql::d0 TO eschema $${eschema}$$; COMMIT MIGRATION graphql::d0; ''' statements = edgeql.parse_block(script) for stmt in statements: if isinstance(stmt, qlast.Delta): # CREATE/APPLY MIGRATION ddl_plan = s_ddl.cmd_from_ddl(stmt, schema=schema, modaliases={}) elif isinstance(stmt, qlast.DDL): # CREATE/DELETE/ALTER (FUNCTION, TYPE, etc) ddl_plan = s_ddl.delta_from_ddl(stmt, schema=schema, modaliases={}) context = sd.CommandContext() ddl_plan.apply(schema, context) return schema
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
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
def load_std_schema(): schema = s_schema.Schema() std_eql_f = os.path.join(os.path.dirname(__file__), '_std.eql') with open(std_eql_f) as f: std_eql = f.read() statements = edgeql.parse_block(std_eql) for statement in statements: cmd = s_ddl.delta_from_ddl( statement, schema=schema, modaliases={None: 'std'}) cmd.apply(schema) return schema
def load_std_schema(): schema = s_schema.Schema() std_eql_f = os.path.join(os.path.dirname(__file__), '_std.eql') with open(std_eql_f) as f: std_eql = f.read() statements = edgeql.parse_block(std_eql) for statement in statements: cmd = s_ddl.delta_from_ddl(statement, schema=schema, modaliases={None: 'std'}) cmd.apply(schema) return schema
def load_delta(self, id, compat_mode=False): d = self.read_delta(id, compat_mode=compat_mode) if d.script: delta_script = edgeql.parse_block(d.script) alter_db = s_db.AlterDatabase() context = sd.CommandContext() with context(s_db.DatabaseCommandContext(alter_db)): for ddl in delta_script: ddl = edgeql.deoptimize(ddl) cmd = sd.Command.from_ast(ddl, context=context) alter_db.add(cmd) d.deltas = [alter_db] return d
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)
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)
def load_default_schema(schema=None): if schema is None: schema = s_schema.Schema() script = f''' CREATE MODULE default; ''' statements = edgeql.parse_block(script) for stmt in statements: if isinstance(stmt, qlast.Delta): # CREATE/APPLY MIGRATION ddl_plan = s_ddl.cmd_from_ddl(stmt, schema=schema, modaliases={}) elif isinstance(stmt, qlast.DDL): # CREATE/DELETE/ALTER (FUNCTION, TYPE, etc) ddl_plan = s_ddl.delta_from_ddl(stmt, schema=schema, modaliases={}) context = sd.CommandContext() ddl_plan.apply(schema, context) return schema
async def _run_script(self, script, *, graphql=False, flags={}): timer = Timer() if graphql: with timer.timeit('graphql_translation'): modules = { m.name for m in self.backend.schema.get_modules() } - {'schema', 'graphql'} script = graphql_compiler.translate( self.backend.schema, script, variables={}, modules=modules) + ';' with timer.timeit('parse_eql'): statements = edgeql.parse_block(script) results = [] for statement in statements: plan = planner.plan_statement( statement, self.backend, flags, timer=timer) with timer.timeit('execution'): result = await executor.execute_plan(plan, self) if result is not None and isinstance(result, list): loaded = [] for row in result: if isinstance(row, str): # JSON result row = json.loads(row) loaded.append(row) result = loaded results.append(result) return results, timer.as_dict()
async def _run_script(self, script, *, graphql=False, flags={}): timer = Timer() if graphql: with timer.timeit('graphql_translation'): modules = {m.name for m in self.backend.schema.get_modules() } - {'schema', 'graphql'} script = graphql_compiler.translate( self.backend.schema, script, variables={}, modules=modules) + ';' with timer.timeit('parse_eql'): statements = edgeql.parse_block(script) results = [] for statement in statements: plan = planner.plan_statement(statement, self.backend, flags, timer=timer) with timer.timeit('execution'): result = await executor.execute_plan(plan, self) if result is not None and isinstance(result, list): loaded = [] for row in result: if isinstance(row, str): # JSON result row = json.loads(row) loaded.append(row) result = loaded results.append(result) return results, timer.as_dict()