diff --git a/news/+migration-unrenderable-default.bugfix.md b/news/+migration-unrenderable-default.bugfix.md new file mode 100644 index 00000000000..8fa83876260 --- /dev/null +++ b/news/+migration-unrenderable-default.bugfix.md @@ -0,0 +1 @@ +`reflex db makemigrations` no longer crashes with a `CompileError` when an added column defaults to an Enum member or a JSON list or dict. An Enum default becomes the server default; existing rows get NULL in a nullable JSON column, and a NOT NULL one is refused until it has a `server_default`. diff --git a/reflex/.templates/alembic/env.py b/reflex/.templates/alembic/env.py index ec724b24437..4ea7becf30e 100644 --- a/reflex/.templates/alembic/env.py +++ b/reflex/.templates/alembic/env.py @@ -104,6 +104,26 @@ def has_default_factory(op: ops.AddColumnOp) -> bool: return False +def missing_value_error( + op: ops.AddColumnOp, cause: str, remedy: str +) -> util.CommandError: + """The error for a NOT NULL column that the migration cannot fill in existing rows. + + Args: + op: The add-column operation. + cause: Why the column has no value, after the column's name. + remedy: The sentence that says how to give it one. + + Returns: + The error to raise. + """ + name = ".".join(filter(None, (op.schema, op.table_name, op.column.name))) + return util.CommandError( + f"Cannot add the NOT NULL column {name!r} {cause}: the migration has no " + f"value for rows that a database already holds. {remedy}" + ) + + @writer.rewrites(ops.AddColumnOp) def add_column_with_server_default( context: MigrationContext, revision: str | Iterable[str | None], op: ops.AddColumnOp @@ -112,9 +132,10 @@ def add_column_with_server_default( Existing rows then get that value when the column is added, so a NOT NULL column can be added to a table that already holds data. A callable default - is evaluated once, when the migration script is generated. A dataclass - ``default_factory`` is not on the column, so a NOT NULL column with one and - no other default is refused rather than added without a value. + is evaluated once, when the migration script is generated. A NOT NULL + column is refused rather than added without a value when its default has no + SQL literal form, like a JSON list, or when its only default is a dataclass + ``default_factory``, which is not on the column. Args: context: The migration context. @@ -122,11 +143,12 @@ def add_column_with_server_default( op: The add-column operation to rewrite. Returns: - The same operation, with ``server_default`` filled in. + The same operation, with ``server_default`` filled in when the default + has a SQL literal form. Raises: - CommandError: If the column is NOT NULL and its only default is a - dataclass ``default_factory``. + CommandError: If the column is NOT NULL and its default has no SQL + literal form, or its only default is a dataclass ``default_factory``. """ column = op.column if column.server_default is not None: @@ -134,16 +156,33 @@ def add_column_with_server_default( default = column.default if default is not None: value = default.arg(None) if default.is_callable else default.arg - column.server_default = sqlalchemy.DefaultClause(sqlalchemy.literal(value)) + # The column's type renders values like an Enum member as the column + # stores them; the untyped literal keeps working where the type rejects one. + for literal in ( + sqlalchemy.literal(value, type_=column.type), + sqlalchemy.literal(value), + ): + try: + literal.compile( + dialect=context.dialect, compile_kwargs={"literal_binds": True} + ) + except sqlalchemy.exc.CompileError: + continue + column.server_default = sqlalchemy.DefaultClause(literal) + return op + if not column.nullable: + raise missing_value_error( + op, + f"with the default {value!r}, which has no SQL literal form", + "Set server_default=... or make the column nullable.", + ) elif not column.nullable and has_default_factory(op): - name = ".".join(filter(None, (op.schema, op.table_name, column.name))) - msg = ( - f"Cannot add the NOT NULL column {name!r} with only a default_factory: " - "the migration has no value for rows that a database already holds. " + raise missing_value_error( + op, + "with only a default_factory", "Set server_default=..., a literal default= or insert_default=, or " - "make the column nullable." + "make the column nullable.", ) - raise util.CommandError(msg) return op diff --git a/tests/units/utils/test_migrations.py b/tests/units/utils/test_migrations.py index 6fb951e35b0..16fe286a4ce 100644 --- a/tests/units/utils/test_migrations.py +++ b/tests/units/utils/test_migrations.py @@ -549,6 +549,61 @@ def test_callable_default_is_evaluated_for_existing_rows(project: Path): assert created +ENUM_APP = SQLMODEL_APP.replace( + "import datetime\n", "import datetime\nimport enum\n" +).replace( + "class Thing(", + 'class Level(str, enum.Enum):\n LOW = "low"\n HIGH = "high"\n\n\nclass Thing(', +) + + +def test_defaults_without_an_untyped_sql_literal( + project: Path, caplog: pytest.LogCaptureFixture +): + """An Enum default is written through its column type; a JSON default has no SQL form. + + A nullable JSON column is added without a server default and a NOT NULL one + is refused, since the existing rows would get no value. A value the column + type rejects, like a naive datetime in a UTC column, keeps its untyped literal. + """ + _db("init") + with get_engine().begin() as connection: + connection.execute(sa.text("INSERT INTO thing (t1) VALUES ('existing')")) + columns = ( + " t1: str\n" + " level: Level = Level.HIGH\n" + " tags: list | None = sqlmodel.Field(default=[], sa_type=sa.JSON)\n" + " created: datetime.datetime = sqlmodel.Field(\n" + " default_factory=datetime.datetime.now\n" + " )" + ) + _write_source(project, ENUM_APP.format(columns=columns)) + _db("makemigrations", "--message", "defaults") + _db("migrate") + ((_, t1, level, tags, created),) = _rows() + assert (t1, level, tags) == ("existing", "HIGH", None) + assert created + caplog.clear() + + _write_source( + project, + ENUM_APP.format( + columns=f"{columns}\n" + " labels: list = sqlmodel.Field(default=[], sa_type=sa.JSON)" + ), + ) + _db("makemigrations", "--message", "json", exit_code=1) + assert _errors(caplog) == [ + ( + "Cannot add the NOT NULL column 'thing.labels' with the default [], which " + "has no SQL literal form: the migration has no value for rows that a " + "database already holds. Set server_default=... or make the column " + "nullable." + ) + ] + assert len(_versions(project)) == 2 + + DATACLASS_APP = SQLALCHEMY_APP.replace( "from sqlalchemy.orm import DeclarativeBase,", "from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass,",