Reputation: 932
Is there a way to get the CREATE TABLE …
SQL string generated by the following Seqel statement? I have looked at the source but found nothing.
DB.create_table :organisations do
primary_key :id
String :external_id, size: 255
String :external_source, size: 255
String :name, size: 255
DateTime :created_at
DateTime :updated_at
DateTime :deleted_at
end
Upvotes: 0
Views: 600
Reputation: 160571
Yes, there are a couple ways to see what's being generated.
-E
flag if you're using the Sequel console to process migrations.In code you can define a Logger instance and tell Sequel to use it:
require 'sequel'
require 'logger'
DB = Sequel.connect('some DSN')
DB.loggers << Logger.new(STDOUT)
Run your code and you'll see the SQL being generated as Sequel runs. You can do this instead of using -E
in the console if you choose.
If you take this code, and save it to a file and run it:
require 'sequel'
require 'logger'
DB = Sequel.sqlite
DB.loggers << Logger.new(STDOUT)
DB.create_table :organisations do
primary_key :id
String :external_id, size: 255
String :external_source, size: 255
String :name, size: 255
DateTime :created_at
DateTime :updated_at
DateTime :deleted_at
end
You'll get:
I, [2014-09-12T14:14:49.509469 #75341] INFO -- : (0.000111s) PRAGMA foreign_keys = 1
I, [2014-09-12T14:14:49.509560 #75341] INFO -- : (0.000025s) PRAGMA case_sensitive_like = 1
I, [2014-09-12T14:14:49.509870 #75341] INFO -- : (0.000244s) CREATE TABLE `organisations` (`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT, `external_id` varchar(255), `external_source` varchar(255), `name` varchar(255), `created_at` timestamp, `updated_at` timestamp, `deleted_at` timestamp)
If you want that output to go to a file you can use a file handle instead of STDOUT:
DB.loggers << Logger.new('sql_logger.log')
See the Logger documentation for more information.
You can even capture that into a String using StringIO or by temporarily assigning where STDOUT or STDERR go.
Upvotes: 1