Example #1
0
def __init_worker__(init_args_pickled: bytes, ) -> None:
    global INITED
    global DBS
    global BACKEND_RUNTIME_PARAMS
    global COMPILER
    global STD_SCHEMA
    global GLOBAL_SCHEMA
    global INSTANCE_CONFIG

    (
        dbs,
        backend_runtime_params,
        std_schema,
        refl_schema,
        schema_class_layout,
        global_schema,
        system_config,
    ) = pickle.loads(init_args_pickled)

    INITED = True
    DBS = dbs
    BACKEND_RUNTIME_PARAMS = backend_runtime_params
    COMPILER = compiler.Compiler(
        backend_runtime_params=BACKEND_RUNTIME_PARAMS, )
    STD_SCHEMA = std_schema
    GLOBAL_SCHEMA = global_schema
    INSTANCE_CONFIG = system_config

    COMPILER.initialize(
        std_schema,
        refl_schema,
        schema_class_layout,
    )
Example #2
0
async def _init_config(cluster: pgcluster.BaseCluster) -> None:
    conn = await cluster.connect(database=edbdef.EDGEDB_TEMPLATE_DB)
    try:
        await _check_data_dir_compatibility(conn)
        compiler = edbcompiler.Compiler()
        await compiler.initialize_from_pg(conn)
        std_schema = compiler.get_std_schema()
        config_spec = config.load_spec_from_schema(std_schema)

        # Initialize global config
        config.set_settings(config_spec)

    finally:
        await conn.close()
Example #3
0
async def _start(ctx: BootstrapContext) -> None:
    conn = await _check_catalog_compatibility(ctx)

    try:
        compiler = edbcompiler.Compiler()
        await compiler.initialize_from_pg(conn)
        std_schema = compiler.get_std_schema()
        config_spec = config.load_spec_from_schema(std_schema)

        # Initialize global config
        config.set_settings(config_spec)

    finally:
        await conn.close()
Example #4
0
async def _get_dbs_and_roles(
    pgconn: asyncpg.Connection, ) -> Tuple[List[str], List[str]]:
    compiler = edbcompiler.Compiler()
    await compiler.initialize_from_pg(pgconn)
    compilerctx = edbcompiler.new_compiler_context(
        user_schema=s_schema.FlatSchema(),
        global_schema=s_schema.FlatSchema(),
        expected_cardinality_one=False,
        single_statement=True,
        output_format=edbcompiler.IoFormat.JSON,
        bootstrap_mode=True,
    )

    _, get_databases_sql = edbcompiler.compile_edgeql_script(
        compiler,
        compilerctx,
        'SELECT sys::Database.name',
    )

    databases = list(
        sorted(
            json.loads(await pgconn.fetchval(get_databases_sql)),
            key=lambda dname: edbdef.EDGEDB_TEMPLATE_DB in dname,
        ))

    _, get_roles_sql = edbcompiler.compile_edgeql_script(
        compiler,
        compilerctx,
        '''SELECT sys::Role {
            name,
            parents := .member_of.name,
        }''',
    )

    roles = json.loads(await pgconn.fetchval(get_roles_sql))
    sorted_roles = list(
        topological.sort({
            r['name']: topological.DepGraphEntry(
                item=r['name'],
                deps=r['parents'],
                extra=False,
            )
            for r in roles
        }))

    return databases, sorted_roles
Example #5
0
async def _start(ctx: BootstrapContext) -> None:
    conn = await _check_catalog_compatibility(ctx)

    try:
        caps = await conn.fetchval("SELECT edgedb.get_backend_capabilities()")
        ctx.cluster.overwrite_capabilities(caps)
        _check_capabilities(ctx)

        compiler = edbcompiler.Compiler()
        await compiler.initialize_from_pg(conn)
        std_schema = compiler.get_std_schema()
        config_spec = config.load_spec_from_schema(std_schema)

        # Initialize global config
        config.set_settings(config_spec)

    finally:
        await conn.close()
Example #6
0
async def _get_dbs_and_roles(pgconn) -> Tuple[List[str], List[str]]:
    compiler = edbcompiler.Compiler({})
    await compiler.ensure_initialized(pgconn)
    schema = compiler.get_std_schema()
    compilerctx = edbcompiler.new_compiler_context(
        schema,
        expected_cardinality_one=False,
        single_statement=True,
        output_format=edbcompiler.IoFormat.JSON,
    )

    schema, get_databases_sql = edbcompiler.compile_edgeql_script(
        compiler,
        compilerctx,
        'SELECT sys::Database.name',
    )

    databases = list(
        sorted(
            json.loads(await pgconn.fetchval(get_databases_sql)),
            key=lambda dname: dname == edbdef.EDGEDB_TEMPLATE_DB,
        ))

    schema, get_roles_sql = edbcompiler.compile_edgeql_script(
        compiler,
        compilerctx,
        '''SELECT sys::Role {
            name,
            parents := .member_of.name,
        }''',
    )

    roles = json.loads(await pgconn.fetchval(get_roles_sql))
    sorted_roles = list(
        topological.sort({
            r['name']: topological.DepGraphEntry(
                item=r['name'],
                deps=r['parents'],
                extra=False,
            )
            for r in roles
        }))

    return databases, sorted_roles
Example #7
0
def __init_worker__(init_args_pickled: bytes, ) -> None:
    global INITED
    global BACKEND_RUNTIME_PARAMS
    global COMPILER
    global STD_SCHEMA

    (
        backend_runtime_params,
        std_schema,
        refl_schema,
        schema_class_layout,
    ) = pickle.loads(init_args_pickled)

    INITED = True
    BACKEND_RUNTIME_PARAMS = backend_runtime_params
    COMPILER = compiler.Compiler(
        backend_runtime_params=BACKEND_RUNTIME_PARAMS, )
    STD_SCHEMA = std_schema

    COMPILER.initialize(
        std_schema,
        refl_schema,
        schema_class_layout,
    )
Example #8
0
async def bootstrap(cluster, args) -> bool:
    pgconn = await cluster.connect()
    pgconn.add_log_listener(_pg_log_listener)
    std_schema = None

    try:
        membership = set()
        session_user = cluster.get_connection_params().user
        if session_user != edbdef.EDGEDB_SUPERUSER:
            membership.add(session_user)

        superuser_uid = await _ensure_edgedb_role(
            cluster,
            pgconn,
            edbdef.EDGEDB_SUPERUSER,
            membership=membership,
            is_superuser=True,
            builtin=True,
        )

        if session_user != edbdef.EDGEDB_SUPERUSER:
            await _execute(
                pgconn,
                f'SET ROLE {edbdef.EDGEDB_SUPERUSER};',
            )
            cluster.set_default_session_authorization(edbdef.EDGEDB_SUPERUSER)

        new_template_db_id = await _ensure_edgedb_template_database(
            cluster, pgconn)

        if new_template_db_id:
            conn = await cluster.connect(database=edbdef.EDGEDB_TEMPLATE_DB)
            conn.add_log_listener(_pg_log_listener)

            await _execute(
                conn,
                f'ALTER SCHEMA public OWNER TO {edbdef.EDGEDB_SUPERUSER}',
            )

            try:
                conn.add_log_listener(_pg_log_listener)

                instancedata = await _populate_misc_instance_data(
                    cluster,
                    conn,
                )

                std_schema, refl_schema, compiler = await _init_stdlib(
                    cluster,
                    conn,
                    testmode=args['testmode'],
                    global_ids={
                        edbdef.EDGEDB_SUPERUSER: superuser_uid,
                        edbdef.EDGEDB_TEMPLATE_DB: new_template_db_id,
                    })
                await _bootstrap_config_spec(std_schema, cluster)
                await _compile_sys_queries(refl_schema, compiler, cluster)
                schema = await _init_defaults(std_schema, compiler, conn)
                schema = await _populate_data(std_schema, compiler, conn)
                await _configure(schema,
                                 compiler,
                                 conn,
                                 cluster,
                                 insecure=args['insecure'])
            finally:
                await conn.close()

            superuser_db = schema.get_global(s_db.Database,
                                             edbdef.EDGEDB_SUPERUSER_DB)

            await _ensure_edgedb_database(
                pgconn,
                edbdef.EDGEDB_SUPERUSER_DB,
                edbdef.EDGEDB_SUPERUSER,
                cluster=cluster,
                builtin=True,
                objid=superuser_db.id,
            )

        else:
            conn = await cluster.connect(database=edbdef.EDGEDB_SUPERUSER_DB)

            try:
                compiler = edbcompiler.Compiler({})
                await compiler.ensure_initialized(conn)
                std_schema = compiler.get_std_schema()
                config_spec = config.load_spec_from_schema(std_schema)
                config.set_settings(config_spec)
                instancedata = await _get_instance_data(conn)
            finally:
                await conn.close()

        datadir_version = instancedata.get('version')
        if datadir_version:
            datadir_major = datadir_version.get('major')

        expected_ver = buildmeta.get_version()

        if datadir_major != expected_ver.major:
            raise errors.ConfigurationError(
                'database instance incompatible with this version of EdgeDB',
                details=(f'The database instance was initialized with '
                         f'EdgeDB version {datadir_major}, '
                         f'which is incompatible with this version '
                         f'{expected_ver.major}'),
                hint=(f'You need to recreate the instance and upgrade '
                      f'using dump/restore.'))

        datadir_catver = instancedata.get('catver')
        expected_catver = edbdef.EDGEDB_CATALOG_VERSION

        if datadir_catver != expected_catver:
            raise errors.ConfigurationError(
                'database instance incompatible with this version of EdgeDB',
                details=(f'The database instance was initialized with '
                         f'EdgeDB format version {datadir_catver}, '
                         f'but this version of the server expects '
                         f'format version {expected_catver}'),
                hint=(f'You need to recreate the instance and upgrade '
                      f'using dump/restore.'))

        await _ensure_edgedb_template_not_connectable(pgconn)

        await _ensure_edgedb_role(
            cluster,
            pgconn,
            args['default_database_user'],
            membership=membership,
            is_superuser=True,
        )

        await _execute(
            pgconn,
            f"SET ROLE {args['default_database_user']};",
        )

        await _ensure_edgedb_database(
            pgconn,
            args['default_database'],
            args['default_database_user'],
            cluster=cluster,
        )

    finally:
        await pgconn.close()

    return new_template_db_id is not None
Example #9
0
async def _bootstrap(
    cluster: pgcluster.BaseCluster,
    pgconn: asyncpg_con.Connection,
    args: Dict[str, Any],
) -> None:
    membership = set()
    session_user = cluster.get_connection_params().user
    if session_user != edbdef.EDGEDB_SUPERUSER:
        membership.add(session_user)

    superuser_uid = await _ensure_edgedb_role(
        cluster,
        pgconn,
        edbdef.EDGEDB_SUPERUSER,
        membership=membership,
        is_superuser=True,
        builtin=True,
    )

    await _execute(
        pgconn,
        f'SET ROLE {edbdef.EDGEDB_SUPERUSER};',
    )
    cluster.set_default_session_authorization(edbdef.EDGEDB_SUPERUSER)

    new_template_db_id = await _ensure_edgedb_template_database(
        cluster, pgconn)

    conn = await cluster.connect(database=edbdef.EDGEDB_TEMPLATE_DB)
    conn.add_log_listener(_pg_log_listener)

    await _execute(
        conn,
        f'ALTER SCHEMA public OWNER TO {edbdef.EDGEDB_SUPERUSER}',
    )

    try:
        conn.add_log_listener(_pg_log_listener)

        await _populate_misc_instance_data(cluster, conn)

        std_schema, refl_schema, compiler = await _init_stdlib(
            cluster,
            conn,
            testmode=args['testmode'],
            global_ids={
                edbdef.EDGEDB_SUPERUSER: superuser_uid,
                edbdef.EDGEDB_TEMPLATE_DB: new_template_db_id,
            }
        )
        await _bootstrap_config_spec(std_schema, cluster)
        await _compile_sys_queries(refl_schema, compiler, cluster)
        schema = await _init_defaults(std_schema, compiler, conn)
        schema = await _populate_data(std_schema, compiler, conn)
        await _configure(schema, compiler, conn, cluster,
                         insecure=args['insecure'])
    finally:
        await conn.close()

    schema = await _execute_edgeql_ddl(
        schema,
        f'CREATE DATABASE {edbdef.EDGEDB_SUPERUSER_DB}',
        stdmode=False,
    )

    superuser_db = schema.get_global(
        s_db.Database, edbdef.EDGEDB_SUPERUSER_DB)

    await _ensure_edgedb_database(
        pgconn,
        edbdef.EDGEDB_SUPERUSER_DB,
        edbdef.EDGEDB_SUPERUSER,
        cluster=cluster,
        objid=superuser_db.id,
    )

    await _ensure_edgedb_role(
        cluster,
        pgconn,
        args['default_database_user'],
        membership=membership,
        is_superuser=True,
    )

    await _execute(
        pgconn,
        f"SET ROLE {args['default_database_user']};",
    )

    await _ensure_edgedb_database(
        pgconn,
        args['default_database'],
        args['default_database_user'],
        cluster=cluster,
    )

    if args['bootstrap_command'] or args['bootstrap_script']:
        logger.info(f'Executing custom initialization')
        init_conn = await cluster.connect(database=args['default_database'])
        await _execute(
            init_conn,
            f"SET ROLE {args['default_database_user']};",
        )
        compiler = edbcompiler.Compiler({})
        await compiler.ensure_initialized(init_conn)
        schema = await compiler.introspect(init_conn)
        try:
            if args['bootstrap_command']:
                try:
                    schema, sql = compile_bootstrap_script(
                        compiler,
                        schema,
                        args['bootstrap_command'],
                    )
                    await _execute(init_conn, sql)
                except Exception as e:
                    raise RuntimeError(
                        "Error executing --bootstrap-command"
                    ) from e
            if args['bootstrap_script']:
                try:
                    with open(args['bootstrap_script']) as f:
                        queries = f.read()
                    schema, sql = compile_bootstrap_script(
                        compiler,
                        schema,
                        queries,
                    )
                    await _execute(init_conn, sql)
                except Exception as e:
                    raise RuntimeError(
                        "Error executing --bootstrap-script"
                    ) from e
        finally:
            await init_conn.close()
Example #10
0
async def bootstrap(cluster, args) -> bool:
    pgconn = await cluster.connect()
    pgconn.add_log_listener(_pg_log_listener)
    std_schema = None

    try:
        membership = set()
        session_user = cluster.get_connection_params().user
        if session_user != edbdef.EDGEDB_SUPERUSER:
            membership.add(session_user)

        superuser_uid = await _ensure_edgedb_role(
            cluster,
            pgconn,
            edbdef.EDGEDB_SUPERUSER,
            membership=membership,
            is_superuser=True,
            builtin=True,
        )

        if session_user != edbdef.EDGEDB_SUPERUSER:
            await _execute(
                pgconn,
                f'SET ROLE {edbdef.EDGEDB_SUPERUSER};',
            )
            cluster.set_default_session_authorization(edbdef.EDGEDB_SUPERUSER)

        new_template_db_id = await _ensure_edgedb_template_database(
            cluster, pgconn)

        if new_template_db_id:
            conn = await cluster.connect(database=edbdef.EDGEDB_TEMPLATE_DB)
            conn.add_log_listener(_pg_log_listener)

            await _execute(
                conn,
                f'ALTER SCHEMA public OWNER TO {edbdef.EDGEDB_SUPERUSER}',
            )

            try:
                conn.add_log_listener(_pg_log_listener)

                await _populate_misc_instance_data(cluster, conn)

                std_schema, refl_schema, compiler = await _init_stdlib(
                    cluster,
                    conn,
                    testmode=args['testmode'],
                    global_ids={
                        edbdef.EDGEDB_SUPERUSER: superuser_uid,
                        edbdef.EDGEDB_TEMPLATE_DB: new_template_db_id,
                    })
                await _bootstrap_config_spec(std_schema, cluster)
                await _compile_sys_queries(refl_schema, compiler, cluster)
                schema = await _init_defaults(std_schema, compiler, conn)
                schema = await _populate_data(std_schema, compiler, conn)
                await _configure(schema,
                                 compiler,
                                 conn,
                                 cluster,
                                 insecure=args['insecure'])
            finally:
                await conn.close()

            superuser_db = schema.get_global(s_db.Database,
                                             edbdef.EDGEDB_SUPERUSER_DB)

            await _ensure_edgedb_database(
                pgconn,
                edbdef.EDGEDB_SUPERUSER_DB,
                edbdef.EDGEDB_SUPERUSER,
                cluster=cluster,
                builtin=True,
                objid=superuser_db.id,
            )

        else:
            conn = await cluster.connect(database=edbdef.EDGEDB_SUPERUSER_DB)

            try:
                await _check_data_dir_compatibility(conn)
                compiler = edbcompiler.Compiler({})
                await compiler.ensure_initialized(conn)
                std_schema = compiler.get_std_schema()
                config_spec = config.load_spec_from_schema(std_schema)
                config.set_settings(config_spec)
            finally:
                await conn.close()

        await _ensure_edgedb_template_not_connectable(pgconn)

        await _ensure_edgedb_role(
            cluster,
            pgconn,
            args['default_database_user'],
            membership=membership,
            is_superuser=True,
        )

        await _execute(
            pgconn,
            f"SET ROLE {args['default_database_user']};",
        )

        await _ensure_edgedb_database(
            pgconn,
            args['default_database'],
            args['default_database_user'],
            cluster=cluster,
        )

    finally:
        await pgconn.close()

    return new_template_db_id is not None