Question

I see that the Sequel gem supports migrations here, but I don't see any type of generator documented. Does one exist; or should I be manually creating all of my migrations (or alternately creating my own task to generate migrations)?

Was it helpful?

Solution

From the documentation:

Sequel doesn't come with generators that create migrations for you. However, creating a migration is as simple as creating a file with the appropriate filename in your migrations directory that contains a Sequel.migration call.

The contents of the migration file doesn't have to specify a timestamp or index and it's an extremely simple format.

I generally just copy a previous migration (maybe one similar to the migration I'm creating) and alter the filename. See existing migrations with:

$ ls -1 db/migrate/
20170320075430_check_postgres_extensions.rb
...

For running migrations, I use the rake task that's available here.

OTHER TIPS

Sequel has no migration generator. But you can easily write your own, for example using a rake task. Here is one:

# Rakefile
require 'fileutils'

namespace :db do
  MIGRATIONS_DIR = 'db/migrations'

  desc "generates a migration file with a timestamp and name"
  task :generate_migration, :name do |_, args|
    args.with_defaults(name: 'migration')

    migration_template = <<~MIGRATION
      Sequel.migration do
        up do
        end

        down do
        end
      end
    MIGRATION

    file_name = "#{Time.now.strftime('%Y%m%d%H%M%S')}_#{args.name}.rb"
    FileUtils.mkdir_p(MIGRATIONS_DIR)

    File.open(File.join(MIGRATIONS_DIR, file_name), 'w') do |file|
      file.write(migration_template)
    end
  end
end

Then run the rake task like this rake db:generate_migration[my_migration_name]. This command will create a Sequel migration file 20220720233750_my_migration_name.rb in the directory db/migrations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top