Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output columns in database schema order, not A-Z order. #998

Open
PeterTakahashi opened this issue Nov 3, 2023 · 1 comment
Open

Output columns in database schema order, not A-Z order. #998

PeterTakahashi opened this issue Nov 3, 2023 · 1 comment

Comments

@PeterTakahashi
Copy link

PeterTakahashi commented Nov 3, 2023

Currently, when columns appear in the model, they are output in A-Z order. I would like the output to be in the order of the columns in the database. This is because I design my models with strict control over the order of columns in the database.

For example, date columns such as created_at and updated_at should be placed last in the table, and foreign keys such as user_id should be placed above them.

Commands

Version

  • annotate version
  • rails version
  • ruby version
@oskarpearson
Copy link

I've had problems with this in the past because, in reality, people run database migrations in different orders.

Let's say we have three developers working at the same time

Consider this timeline:

Monday

  • Developer 1 creates and their a database migration in their branch. It adds column sometable.foo in migration 0001 to their local db.
  • Developer 2 creates and runs a database migration in their branch. It adds column sometable.bar in migration 0002 to their local db.

Tuesday

  • Developer 1 merges their code to production. Production table consists of [:id, :foo]
  • Developer 2 rebases.
  • Developer 2 runs db:migrate. Column foo gets added to their database table. Their table consists of [:id, :bar, :foo]
  • Developer 2 submits a diff that changes the annotate order to db

Every time Developer 2 runs db:migrate and annotate runs, it'll show a 'diff' on developer 2, since their local table is out of order. The only way out of this is to db rollback, and then roll forwards, or reset their db entirely.

Why do this at all?

I'm not convinced there's a lot of value to trying to manage the order of columns in the database. Unless you have an exceptional situation, you're micro-optimising for very little benefit.

  • You can't later insert a column between columns 1 and 2 without rebuilding the whole table. Which involves massive downtime for most db engines
  • Most ID fields should be indexed, so the placement in the table doesn't matter very much anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants