开发者

Rails/MySQL: Knowing a version number for a table, or the entire db schema

开发者 https://www.devze.com 2022-12-12 09:51 出处:网络
As 开发者_如何转开发an API endpoint, I need to provide a table schema with a version number so that the mobile developers I am working with can cache the schema until it changes again.

As 开发者_如何转开发an API endpoint, I need to provide a table schema with a version number so that the mobile developers I am working with can cache the schema until it changes again.

I am automating this process, which complicates the "versioning" idea.

At first I thought that I could use the latest migration number as the version # but it occurred to me that Rails migrations are non-sequential now, so it would mean that I would need to give them all migration numbers and they would need to decide if there was a new migration present by comparing the arrays (maybe this is the solution?)

I’m wondering if I am missing anything - if there is any other schema version number in Rails apart from those stored in the migration table, or also if there was any way of tracking this through mysql.


Using the migration version could be a good idea. If you don't want to expose the full list of missing migrations, you can provide the current schema version for a quick comparison, then expose the full list on a second request.

You can obtain the list of pending migration using the ActiveRecord::Migrator object.

migrations = ActiveRecord::Migrator.new(:up, 'db/migrate').pending_migrations
migrations.each do |migration|
  migration.version
end

And for the current schema version.

schema  = File.read("#{RAILS_ROOT}/db/schema.rb")
version = ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, schema)
puts version
0

精彩评论

暂无评论...
验证码 换一张
取 消