emmett-framework / emmett

The web framework for inventors
BSD 3-Clause "New" or "Revised" License
1.06k stars 71 forks source link

Migrations engine should build changes for columns length #270

Closed josejachuf closed 4 years ago

josejachuf commented 4 years ago

Hi @gi0baro I am using Postgresql

in a change of a field from string (length = 3) to string (length = 20) the migration tool should detect it? If so, it is not working for me. It is only to detect if it is a bug or a limitation.

gi0baro commented 4 years ago

@josejachuf I think it should but I inspected the code and actually it's not implemented. I'll keep this opened as a feature request

josejachuf commented 4 years ago

Thanks @gi0baro Please apply it to weppy too

gi0baro commented 4 years ago

@josejachuf actual master should implement this. Please double-check before migrating your data and let me know if you encounter any issues.

josejachuf commented 4 years ago

@gi0baro

Starts with an empty database

(venv38) jose@lenovo:~/emmett/r1app/api$ emmett -a aplicacion migrations generate -m 'PM'

Generated migration for revision b1b86b19b417 (venv38) jose@lenovo:~/emmett/r1app/api$ emmett -a aplicacion migrations up Performing upgrades against postgres://user:password$@localhost/db_appconsultas2 Performing upgrade: -> b1b86b19b417 (head), PM Adding revision b1b86b19b417 to schema Succesfully upgraded to revision b1b86b19b417: PM

(venv38) jose@lenovo:~/emmett/r1app/api$ emmett -a aplicacion migrations generate -m 'cambio len'

Generated migration for revision 82f20f9c6dbc

from emmett.orm import migrations

class Migration(migrations.Migration):
    revision = '82f20f9c6dbc'
    revises = 'b1b86b19b417'

    def up(self):
        self.alter_column('temas', 'nombre',
            existing_type='string',
            existing_length=100,
            length=120,
            existing_notnull=False)

    def down(self):
        self.alter_column('temas', 'nombre',
            existing_type='string',
            existing_length=120,
            length=100,
            existing_notnull=False)

(venv38) jose@lenovo:~/emmett/r1app/api$ emmett -a aplicacion migrations up

Performing upgrades against postgres://user:password$@localhost/db_appconsultas2 Performing upgrade: b1b86b19b417 -> 82f20f9c6dbc (head), cambio len Failed upgrading to 82f20f9c6dbc Traceback (most recent call last): File "/home/jose/emmett/venv38/bin/emmett", line 8, in sys.exit(main()) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/emmett/cli.py", line 510, in main cli.main(prog_name="python -m emmett" if as_module else None) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/emmett/cli.py", line 283, in main return super().main(args, kwargs) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/click/core.py", line 717, in main rv = self.invoke(ctx) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/click/core.py", line 1137, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/click/core.py", line 1137, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/click/core.py", line 956, in invoke return ctx.invoke(self.callback, ctx.params) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/click/core.py", line 555, in invoke return callback(args, kwargs) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/click/decorators.py", line 64, in new_func return ctx.invoke(f, obj, *args, *kwargs) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/click/core.py", line 555, in invoke return callback(args, kwargs) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/emmett/cli.py", line 494, in migrations_up up(app, dbs, revision) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/emmett/orm/migrations/commands.py", line 371, in up Command(app, dals).up(revision) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/emmett/orm/migrations/commands.py", line 263, in up migration.up() File "/home/jose/emmett/r1app/api/aplicacion/migrations/82f20f9c6dbc_cambio_len.py", line 17, in up self.alter_column('temas', 'nombre', File "/home/jose/emmett/venv38/lib/python3.8/site-packages/emmett/orm/migrations/helpers.py", line 34, in call return op.run() File "/home/jose/emmett/venv38/lib/python3.8/site-packages/emmett/orm/migrations/operations.py", line 419, in run self.engine.alter_column( File "/home/jose/emmett/venv38/lib/python3.8/site-packages/emmett/orm/migrations/engine.py", line 100, in alter_column sql = self._alter_column_sql( File "/home/jose/emmett/venv38/lib/python3.8/site-packages/emmett/orm/migrations/engine.py", line 306, in _alter_column_sql self._represent_changes(changes, field) File "/home/jose/emmett/venv38/lib/python3.8/site-packages/emmett/orm/migrations/engine.py", line 292, in _represent_changes self.adapter.types[ftype] % {'length': change[1]} UnboundLocalError: local variable 'ftype' referenced before assignment

gi0baro commented 4 years ago

@josejachuf try with latest master (5edf4d0)

josejachuf commented 4 years ago

@gi0baro It works fine. I tried increasing and decreasing the length. When any string in the table is longer than the new length, it throws an error, it seems to me it is ok not to lose data

gi0baro commented 4 years ago

@josejachuf yeah that relies on the database itself, since Emmett can't do anything about the data. I'm gonna keep this open until the patch will be published also on 1.x