MasoniteFramework / orm

Masonite ORM is a beautiful Python ORM. It's also a nearly drop in replacement of the Orator ORM
https://orm.masoniteproject.com
MIT License
160 stars 47 forks source link

Add Enum column in migration not working (MySQL) #884

Open Kyrela opened 2 weeks ago

Kyrela commented 2 weeks ago

Describe the bug

When running a migration containing an enum column to add to an existing table, using the MySQL connector, the migration fails.

To Reproduce

  1. Create a MySQL database
  2. Download masonite-minimal.zip, extract files
  3. Replace the config/database.py content with your previously created database information
  4. Open terminal at folder root
  5. Run pip install -r requirements.txt
  6. Run masonite-orm migrate

Expected behavior

The masonite-orm migrate command exit with code 0, the database is updated correctly to reflect the migrations.

Actual behavior

The masonite-orm migrate command exit with code 1, and displays an error code. The last migration is not migrated.

Screenshots or code snippets

Log showed by console:

Migrating: 2024_06_19_201336_test_table
Migrated: 2024_06_19_201336_test_table (0.03s)
Migrating: 2024_06_19_201408_test_enum

  QueryException

  (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NOT NULL DEFAULT 'foo'' at line 1")

  at .venv\Lib\site-packages\masoniteorm\connections\MySQLConnection.py:176 in query
      172│                     return self.format_cursor_results(cursor.fetchone())
      173│                 else:
      174│                     return self.format_cursor_results(cursor.fetchall())
      175│         except Exception as e:
    → 176│             raise QueryException(str(e)) from e
      177│         finally:
      178│             self._cursor.close()
      179│             if self.get_transaction_level() <= 0:
      180│                 self.open = 0

SQL statement generated by MySQLPlateform (retrived using masonite-orm migrate --show):

ALTER TABLE `test` ADD `test_enum` ENUM NOT NULL DEFAULT 'foo'

SQL statement that should have been generated:

ALTER TABLE `test` ADD `test_enum` ENUM('foo', 'bar', 'baz') NOT NULL DEFAULT 'foo'

Desktop

What database are you using?

Additional context

The src/masoniteorm/schema/platforms/MySQLPlatform.py:add_column_string function simply has no column_constraint format field (as used in src/masoniteorm/schema/platforms/MySQLPlatform.py:columnize_string to fill the values in src/masoniteorm/schema/platforms/MySQLPlatform.py:columnize)

This comment also applies when modifying a existing column, as can be seen in src/masoniteorm/schema/platforms/MySQLPlatform.py:change_column_string, but this function isn't used ; for changed columns, src/masoniteorm/schema/platforms/MySQLPlatform.py:columnize is used instead, as can be seen in src/masoniteorm/schema/platforms/MySQLPlatform.py:compile_alter_sql, so the whole issue doesn't seems to apply when editing a column.

josephmancuso commented 2 weeks ago

can you paste your migration file here?

josephmancuso commented 2 weeks ago

your 2024_06_19_201408_test_enum

josephmancuso commented 2 weeks ago

nvm i see a zip