How to generate SQL statements from Sequel migrations

939 Views Asked by At

I would like to output the generated SQL statements from Sequel migrations to a file without having a connection to the actual database. I need this because I want to export the statements in MySQL form, but I use PostgreSQL normally.

I could try to set up MySQL and output with the -E option while creating the database, but that is not how things should work right?

What I need is something like:

sequel -m db/migrations/ -E --type mysql > msysql_statements.sql

I'm using Sequel with migrations.

Is there a way to achieve this?

Trials according to answers

I tried the command proposed Jeremy Evans'es answer:

sequel -m db/migrations/ -t -E mock://mysql

the error is following output:

  I, [2016-07-18T13:57:09.020630 #21573]  INFO -- : SELECT NULL AS `nil` FROM `schema_migrations` LIMIT 1
  I, [2016-07-18T13:57:09.020802 #21573]  INFO -- : SELECT * FROM `schema_migrations` LIMIT 1
  /home/me/.rvm/gems/ruby-2.2.1/gems/sequel-4.34.0/lib/sequel/extensions/migration.rb:751:in `schema_dataset': Migrator table schema_migrations does not contain column filename (Sequel::Migrator::Error)
    from /home/me/.rvm/gems/ruby-2.2.1/gems/sequel-4.34.0/lib/sequel/extensions/migration.rb:450:in `initialize'
    from /home/me/.rvm/gems/ruby-2.2.1/gems/sequel-4.34.0/lib/sequel/extensions/migration.rb:652:in `initialize'
    from /home/me/.rvm/gems/ruby-2.2.1/gems/sequel-4.34.0/lib/sequel/extensions/migration.rb:397:in `new'
    from /home/me/.rvm/gems/ruby-2.2.1/gems/sequel-4.34.0/lib/sequel/extensions/migration.rb:397:in `run'
    from /home/me/.rvm/gems/ruby-2.2.1/gems/sequel-4.34.0/lib/sequel/extensions/migration.rb:368:in `apply'
    from /home/me/.rvm/gems/ruby-2.2.1/gems/sequel-4.34.0/bin/sequel:160:in `<top (required)>'
    from /home/me/.rvm/gems/ruby-2.2.1/bin/sequel:23:in `load'
    from /home/me/.rvm/gems/ruby-2.2.1/bin/sequel:23:in `<main>'
    from /home/me/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
    from /home/me/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'

Unfortunately it seems to try to access something from the mock-database.

Update

With Jeremy's answer i had something to work with, i came up with following hacky workaround, which generates at least most of the statements, but not all :( :

  • issue sequel -m db/migrations/ -t -E mock://mysql > mysql-statements.sql
  • if you get an exeption, comment out raise statement in sequel code
  • at the and you will have an output with the correct sql statement, but with extra logging information
  • remove that extra information by using some replacement tool or easier with multi-cursor-editors like sublime or atom
  • put semicolons at the end of the lines
  • now you have correct mysql statements
  • Dont forget to revert modifications on sequel-gem-code ;)
3

There are 3 best solutions below

3
AnoE On

Nulldb https://github.com/nulldb/nulldb will be perfect for this.

0
Maxim Fedotov On

There is no way to do it in bash like you want to, but you can use tools for that. Dump your PostgreSQL into this tool and it should create a MySQL from it.

There is another tool with various DB frameworks for your choosing - SQLines

2
Jeremy Evans On
sequel -m db/migrations/ -E mock://mysql > msysql_statements.sql