0

I am using Sequel with SQLite and want like to create the database from Ruby code if the SQLite database file does not exist yet. I could use the File class to look for the file but I am wondering if there is something in Sequel to handle this for me?

At first I was creating all the tables each run of the program using create_table? that only creates a table if it does not already exist. This worked fine until I needed a join table. There is no create_join_table? that only creates a join table if it does not exist.

Any suggestions for handling database creation in the application?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Mark M
  • 1,807
  • 2
  • 21
  • 40

1 Answers1

1

The default behavior of SQLite access in Sequel is to create the database if it does not exist.

You can do:

join_table_opts = {...}
DB.create_join_table(join_table_opts) unless DB.table_exists?(DB.send(:join_table_name, join_table_opts))

but it may be easier t to use create_table? and create the join table manually. create_join_table doesn't really save you all that much code (a couple lines at most).

Jeremy Evans
  • 11,959
  • 27
  • 26