Sequel generate migration
Asked Answered
N

2

8

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)?

Nikolas answered 2/4, 2014 at 1:41 Comment(3)
At my workplace, we have Sinatra/Sequel, and manually create migrations. That might simply be because we don't know any better (hence I cannot make this an answer).Inkle
@MarcoPrins, I use Rails for some projects; typically large monolithic web sites. For this particular case I'm using Sinatra to build a Restful web service.Nikolas
Looks like a generator does not exist for Sequel.Musk
N
10

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.

Newsom answered 4/10, 2016 at 4:8 Comment(0)
H
5

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.

Hereunto answered 20/7, 2022 at 23:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.