3

I am wondering the best way to create a migration in sequel that is based on an existing table dump from postgres. I realise that I will have to write the down method myself to remove the table / functions / sequences, but is there a way to tell the sequel up migration to load an existing sql script and execute everything in the script?

Edit: Just in case its unclear, this is using the ruby Sequel SQL library / gem

kelso
  • 394
  • 1
  • 2
  • 11

2 Answers2

8

You wouldn't create a migration from a table dump, but you can easily create one by using Sequel's schema_dumper extension. There's actually built-in support for this in the sequel command line program:

sequel -d postgres://postgres@localhost/mydb > 001_initial_migration.rb

There's no support for dumping functions or sequences, as this is designed to produce a database-independent migration.

If you are using functions and custom sequences, you are fully in database-specific territory, and may be better off just using the database's tools.

Jeremy Evans
  • 11,959
  • 27
  • 26
  • Thanks for the tips about functions and sequences, which I am having to use (for spatial support). I will stick with the DB tools for setting up in this case. – kelso May 15 '12 at 05:06
-1

Are you asking how to execute an SQL script with the PostgreSQL command-line client? If so, the answer is to use the --file option:

psql -U postgres -d mydb -f dump.sql.
theory
  • 9,178
  • 10
  • 59
  • 129
  • No, sorry I wasn't more clear, it is using the ruby Sequel gem, an orm of sorts. – kelso May 12 '12 at 11:48
  • 1
    Ah, I'm not familiar with it, but if there is some sort of way to do stuff directly in SQL, you can perhaps send the contents of your file to such a method call. I doubt that you'll get any client to read a PostgreSQL dump file as well as `psql` does, though. Perhaps you could make a `system` call to `psql` yourself? – theory May 13 '12 at 03:52
  • There is a method to pass a sql string, which I could use, but I would have to parse the file (I think), which I am not all that keep to. The idea to make the call to psql would work I think, seems like a pretty good option if the library doesn't have anything built in that I am missing. – kelso May 13 '12 at 05:02