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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
-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.

3 Comments

No, sorry I wasn't more clear, it is using the ruby Sequel gem, an orm of sorts.
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.