Skip to content

Commit

Permalink
Update dbview with user schema version
Browse files Browse the repository at this point in the history
  • Loading branch information
fantix committed Dec 11, 2023
1 parent 21ae092 commit b445ae2
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 26 deletions.
24 changes: 20 additions & 4 deletions edb/server/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
from edb.schema import roles as s_role
from edb.schema import schema as s_schema
from edb.schema import types as s_types
from edb.schema import version as s_ver

from edb.pgsql import ast as pgast
from edb.pgsql import compiler as pg_compiler
Expand Down Expand Up @@ -1002,6 +1003,7 @@ def parse_user_schema_db_config(
)
return dbstate.ParsedDatabase(
user_schema_pickle=pickle.dumps(user_schema, -1),
schema_version=_get_schema_version(user_schema),
database_config=db_config,
ext_config_settings=ext_config_settings,
protocol_version=defines.CURRENT_PROTOCOL,
Expand Down Expand Up @@ -1498,6 +1500,11 @@ def _compile_schema_storage_stmt(
ctx.state.current_tx().update_schema(schema)


def _get_schema_version(user_schema: s_schema.Schema) -> uuid.UUID:
ver = user_schema.get_global(s_ver.SchemaVersion, "__schema_version__")
return ver.get_version(user_schema)


def _compile_ql_script(
ctx: CompileContext,
eql: str,
Expand Down Expand Up @@ -2443,7 +2450,10 @@ def _try_compile(
if not ctx.dump_restore_mode:
if comp.user_schema is not None:
final_user_schema = comp.user_schema
unit.user_schema = pickle.dumps(comp.user_schema, -1)
unit.user_schema_pv = (
pickle.dumps(comp.user_schema, -1),
_get_schema_version(comp.user_schema),
)
unit.extensions, unit.ext_config_settings = (
_extract_extensions(ctx, comp.user_schema)
)
Expand All @@ -2468,7 +2478,10 @@ def _try_compile(
if not ctx.dump_restore_mode:
if comp.user_schema is not None:
final_user_schema = comp.user_schema
unit.user_schema = pickle.dumps(comp.user_schema, -1)
unit.user_schema_pv = (
pickle.dumps(comp.user_schema, -1),
_get_schema_version(comp.user_schema),
)
unit.extensions, unit.ext_config_settings = (
_extract_extensions(ctx, comp.user_schema)
)
Expand Down Expand Up @@ -2506,7 +2519,10 @@ def _try_compile(
if not ctx.dump_restore_mode:
if comp.user_schema is not None:
final_user_schema = comp.user_schema
unit.user_schema = pickle.dumps(comp.user_schema, -1)
unit.user_schema_pv = (
pickle.dumps(comp.user_schema, -1),
_get_schema_version(comp.user_schema),
)
unit.extensions, unit.ext_config_settings = (
_extract_extensions(ctx, comp.user_schema)
)
Expand Down Expand Up @@ -2626,7 +2642,7 @@ def _try_compile(
unit.cardinality is enums.Cardinality.NO_RESULT
)
if unit.cacheable and (
unit.config_ops or unit.modaliases or unit.user_schema or
unit.config_ops or unit.modaliases or unit.user_schema_pv or
unit.cached_reflection
):
raise errors.InternalServerError(
Expand Down
7 changes: 4 additions & 3 deletions edb/server/compiler/dbstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ class QueryUnit:
dataclasses.field(default_factory=list))
modaliases: Optional[immutables.Map[Optional[str], str]] = None

# If present, represents the future schema state after
# the command is run. The schema is pickled.
user_schema: Optional[bytes] = None
# If present, represents the future schema state after the command is run.
# The schema is pickled, followed by its version (_pv).
user_schema_pv: Optional[Tuple[bytes, uuid.UUID]] = None
cached_reflection: Optional[bytes] = None
extensions: Optional[set[str]] = None
ext_config_settings: Optional[list[config.Setting]] = None
Expand Down Expand Up @@ -473,6 +473,7 @@ class SQLQueryUnit:
@dataclasses.dataclass
class ParsedDatabase:
user_schema_pickle: bytes
schema_version: uuid.UUID
database_config: immutables.Map[str, config.SettingValue]
ext_config_settings: list[config.Setting]

Expand Down
6 changes: 4 additions & 2 deletions edb/server/dbview/dbview.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ cdef class Database:
readonly object user_config_spec

readonly str name
readonly object schema_version
readonly object dbver
readonly object db_config
readonly bytes user_schema_pickle
Expand All @@ -101,6 +102,7 @@ cdef class Database:
cdef _set_and_signal_new_user_schema(
self,
new_schema_pickle,
schema_version,
extensions,
ext_config_settings,
reflection_cache=?,
Expand Down Expand Up @@ -141,7 +143,7 @@ cdef class DatabaseConnectionView:
object _txid
object _in_tx_db_config
object _in_tx_savepoints
object _in_tx_user_schema_pickle
object _in_tx_user_schema_pv
object _in_tx_user_config_spec
object _in_tx_global_schema_pickle
object _in_tx_new_types
Expand Down Expand Up @@ -186,7 +188,7 @@ cdef class DatabaseConnectionView:
cdef on_success(self, query_unit, new_types)
cdef commit_implicit_tx(
self,
user_schema,
user_schema_pv,
extensions,
ext_config_settings,
global_schema,
Expand Down
36 changes: 24 additions & 12 deletions edb/server/dbview/dbview.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ cdef class Database:
str name,
*,
bytes user_schema_pickle,
object schema_version,
object db_config,
object reflection_cache,
object backend_ids,
Expand All @@ -164,6 +165,7 @@ cdef class Database:
):
self.name = name

self.schema_version = schema_version
self.dbver = next_dbver()

self._index = index
Expand Down Expand Up @@ -199,6 +201,7 @@ cdef class Database:
cdef _set_and_signal_new_user_schema(
self,
new_schema_pickle,
schema_version,
extensions,
ext_config_settings,
reflection_cache=None,
Expand All @@ -208,6 +211,7 @@ cdef class Database:
if new_schema_pickle is None:
raise AssertionError('new_schema is not supposed to be None')

self.schema_version = schema_version
self.dbver = next_dbver()

self.user_schema_pickle = new_schema_pickle
Expand Down Expand Up @@ -339,7 +343,7 @@ cdef class DatabaseConnectionView:
self._in_tx_with_sysconfig = False
self._in_tx_with_dbconfig = False
self._in_tx_with_set = False
self._in_tx_user_schema_pickle = None
self._in_tx_user_schema_pv = None
self._in_tx_global_schema_pickle = None
self._in_tx_new_types = {}
self._in_tx_user_config_spec = None
Expand Down Expand Up @@ -510,7 +514,7 @@ cdef class DatabaseConnectionView:

def get_user_schema_pickle(self):
if self._in_tx:
return self._in_tx_user_schema_pickle
return self._in_tx_user_schema_pv[0]
else:
return self._db.user_schema_pickle

Expand Down Expand Up @@ -764,7 +768,9 @@ cdef class DatabaseConnectionView:
self._in_tx_globals = self._globals
self._in_tx_db_config = self._db.db_config
self._in_tx_modaliases = self._modaliases
self._in_tx_user_schema_pickle = self._db.user_schema_pickle
self._in_tx_user_schema_pv = (
self._db.user_schema_pickle, self._db.schema_version
)
self._in_tx_global_schema_pickle = \
self._db._index._global_schema_pickle
self._in_tx_user_config_spec = self._db.user_config_spec
Expand All @@ -779,8 +785,8 @@ cdef class DatabaseConnectionView:
self._in_tx_with_dbconfig = True
if query_unit.has_set:
self._in_tx_with_set = True
if query_unit.user_schema is not None:
self._in_tx_user_schema_pickle = query_unit.user_schema
if query_unit.user_schema_pv is not None:
self._in_tx_user_schema_pv = query_unit.user_schema_pv
self._in_tx_user_config_spec = config.FlatSpec(
*query_unit.ext_config_settings
)
Expand All @@ -805,10 +811,11 @@ cdef class DatabaseConnectionView:
if not self._in_tx:
if new_types:
self._db._update_backend_ids(new_types)
if query_unit.user_schema is not None:
if query_unit.user_schema_pv is not None:
self._in_tx_dbver = next_dbver()
self._db._set_and_signal_new_user_schema(
query_unit.user_schema,
query_unit.user_schema_pv[0],
query_unit.user_schema_pv[1],
query_unit.extensions,
query_unit.ext_config_settings,
pickle.loads(query_unit.cached_reflection)
Expand Down Expand Up @@ -848,9 +855,10 @@ cdef class DatabaseConnectionView:

if self._in_tx_new_types:
self._db._update_backend_ids(self._in_tx_new_types)
if query_unit.user_schema is not None:
if query_unit.user_schema_pv is not None:
self._db._set_and_signal_new_user_schema(
query_unit.user_schema,
query_unit.user_schema_pv[0],
query_unit.user_schema_pv[1],
query_unit.extensions,
query_unit.ext_config_settings,
pickle.loads(query_unit.cached_reflection)
Expand Down Expand Up @@ -884,7 +892,7 @@ cdef class DatabaseConnectionView:

cdef commit_implicit_tx(
self,
user_schema,
user_schema_pv,
extensions,
ext_config_settings,
global_schema,
Expand All @@ -900,9 +908,10 @@ cdef class DatabaseConnectionView:

if self._in_tx_new_types:
self._db._update_backend_ids(self._in_tx_new_types)
if user_schema is not None:
if user_schema_pv is not None:
self._db._set_and_signal_new_user_schema(
user_schema,
user_schema_pv[0],
user_schema_pv[1],
extensions,
ext_config_settings,
pickle.loads(cached_reflection)
Expand Down Expand Up @@ -1212,6 +1221,7 @@ cdef class DatabaseIndex:
dbname,
*,
user_schema_pickle,
schema_version,
db_config,
reflection_cache,
backend_ids,
Expand All @@ -1223,6 +1233,7 @@ cdef class DatabaseIndex:
if db is not None:
db._set_and_signal_new_user_schema(
user_schema_pickle,
schema_version,
extensions,
ext_config_settings,
reflection_cache,
Expand All @@ -1234,6 +1245,7 @@ cdef class DatabaseIndex:
self,
dbname,
user_schema_pickle=user_schema_pickle,
schema_version=schema_version,
db_config=db_config,
reflection_cache=reflection_cache,
backend_ids=backend_ids,
Expand Down
12 changes: 7 additions & 5 deletions edb/server/protocol/execute.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,15 @@ async def execute_script(
bytes state = None, orig_state = None
ssize_t sent = 0
bint in_tx, sync, no_sync
object user_schema, extensions, ext_config_settings, cached_reflection
object user_schema_pv
object extensions, ext_config_settings, cached_reflection
object global_schema, roles
WriteBuffer bind_data
int dbver = dbv.dbver
bint parse

user_schema = extensions = ext_config_settings = cached_reflection = None
user_schema_pv = None
extensions = ext_config_settings = cached_reflection = None
global_schema = roles = None
unit_group = compiled.query_unit_group

Expand Down Expand Up @@ -295,8 +297,8 @@ async def execute_script(
dbv.start_implicit(query_unit)
config_ops = query_unit.config_ops

if query_unit.user_schema:
user_schema = query_unit.user_schema
if query_unit.user_schema_pv:
user_schema_pv = query_unit.user_schema_pv
extensions = query_unit.extensions
ext_config_settings = query_unit.ext_config_settings
cached_reflection = query_unit.cached_reflection
Expand Down Expand Up @@ -363,7 +365,7 @@ async def execute_script(
else:
if not in_tx:
side_effects = dbv.commit_implicit_tx(
user_schema,
user_schema_pv,
extensions,
ext_config_settings,
global_schema,
Expand Down
2 changes: 2 additions & 0 deletions edb/server/tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ async def introspect_db(self, dbname: str) -> None:
db = self._dbindex.register_db(
dbname,
user_schema_pickle=parsed_db.user_schema_pickle,
schema_version=parsed_db.schema_version,
db_config=parsed_db.database_config,
reflection_cache=reflection_cache,
backend_ids=backend_ids,
Expand Down Expand Up @@ -918,6 +919,7 @@ async def _early_introspect_db(self, dbname: str) -> None:
self._dbindex.register_db(
dbname,
user_schema_pickle=None,
schema_version=None,
db_config=None,
reflection_cache=None,
backend_ids=None,
Expand Down

0 comments on commit b445ae2

Please sign in to comment.