martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
405 stars 23 forks source link

Syntax bug in genmigrations #247

Closed igbanam closed 1 month ago

igbanam commented 1 month ago

I had a model with a UUID primary key.

field :id, :uuid, primary_key: true

genmigrations and migrate worked fine here.

I changed this to use an integer instead

field :id, :int, primary_key: true, auto: true

genmigrations works, but I have this error when I migrate

Unhandled exception: near "AUTOINCREMENT": syntax error (SQLite3::Exception)
  from lib/sqlite3/src/sqlite3/statement.cr:81:5 in 'check'
  from lib/sqlite3/src/sqlite3/statement.cr:4:5 in 'initialize'
  from lib/sqlite3/src/sqlite3/statement.cr:2:3 in 'new'
  from lib/sqlite3/src/sqlite3/connection.cr:63:5 in 'build_prepared_statement'
  from lib/db/src/db/connection.cr:60:42 in 'fetch_or_build_prepared_statement'
  from lib/db/src/db/session_methods.cr:25:16 in 'build'
  from lib/db/src/db/query_methods.cr:275:7 in 'exec'
  from lib/marten/src/marten/db/management/schema_editor/base.cr:109:15 in 'execute'
  from lib/marten/src/marten/db/management/schema_editor/concerns/core.cr:211:13 in 'create_table'
  from lib/marten/src/marten/db/management/schema_editor/sqlite.cr:257:13 in 'change_column'
  from lib/marten/src/marten/db/migration/operation/change_column.cr:40:13 in 'mutate_db_forward'
  from lib/marten/src/marten/db/migration.cr:138:11 in 'apply_forward'
  from lib/marten/src/marten/db/management/migrations/runner.cr:220:25 in 'migrate_forward'
  from lib/marten/src/marten/db/management/migrations/runner.cr:99:15 in 'run_migrations'
  from lib/marten/src/marten/cli/manage/command/migrate.cr:57:15 in 'run'
  from lib/marten/src/marten/cli/manage/command/base.cr:441:13 in 'setup_and_run'
  from lib/marten/src/marten/cli/manage/command/base.cr:106:13 in 'handle!'
  from lib/marten/src/marten/cli/manage.cr:72:9 in 'run'
  from lib/marten/src/marten/cli.cr:24:7 in 'run'
  from lib/marten/src/marten/cli.cr:23:5 in 'run'
  from manage.cr:4:1 in '__crystal_main'
  from /opt/homebrew/Cellar/crystal/1.12.2/share/crystal/src/crystal/main.cr:129:5 in 'main_user_code'
  from /opt/homebrew/Cellar/crystal/1.12.2/share/crystal/src/crystal/main.cr:115:7 in 'main'
  from /opt/homebrew/Cellar/crystal/1.12.2/share/crystal/src/crystal/main.cr:141:3 in 'main'
igbanam commented 1 month ago

Is there some way I can see the generated SQL?

ellmetha commented 1 month ago

I quickly tried to reproduce this with a new project and a SQLite3 database but I wasn't able to get this syntax error. Which versions of Marten and SQLite3 are you using exactly?

igbanam commented 1 month ago

This is my shards.lock

version: 2.0
shards:
  db:
    git: https://github.com/crystal-lang/crystal-db.git
    version: 0.13.1

  i18n:
    git: https://github.com/crystal-i18n/i18n.git
    version: 0.2.1

  marten:
    git: https://github.com/martenframework/marten.git
    version: 0.5.0

  msgpack:
    git: https://github.com/crystal-community/msgpack-crystal.git
    version: 1.3.4

  sqlite3:
    git: https://github.com/crystal-lang/crystal-sqlite3.git
    version: 0.21.0
ellmetha commented 1 month ago

Is there some way I can see the generated SQL?

You can set the log_level setting to Log::Severity::Debug in your development settings file.

I am still not able to reproduce, and I have the exact same shards as you. Would you be able to share the full model definition with which you are encountering this and the generated migrations? 🙏

igbanam commented 1 month ago

Unfortunately, the project I saw this in is not backed by Git properly. So I can't go back in time and recreate the issue. But if I run into this issue again, I'll be sure to document the path to the issue properly, and share that.