Permalink
Browse files

Don't drop tables if they already exist

  • Loading branch information...
1 parent b771802 commit 4ef110188b0c3c1268eaaf30bb64a990da27f498 @jeremyevans committed Jul 5, 2011
Showing with 15 additions and 5 deletions.
  1. +15 −5 demo-site/models.rb
View
@@ -7,13 +7,16 @@
DB = Sequel.connect(ENV['DATABASE_URL'] || 'sqlite:/')
DB.loggers << Logger.new($stdout) unless DEMO_MODE
-DB.create_table!(:artists) do
+unless DB.table_exists?(:artists)
+DB.create_table(:artists) do
primary_key :id
String :name, :null=>false, :unique=>true
end
1.upto(3){|x| DB[:artists].insert(:name=>"Example Artist #{x}")}
+end
-DB.create_table!(:albums) do
+unless DB.table_exists?(:albums)
+DB.create_table(:albums) do
primary_key :id
String :name, :null=>false
foreign_key :artist_id, :artists, :null=>false
@@ -25,8 +28,10 @@
index [:name, :artist_id], :unique=>true
end
DB[:albums].insert(:name=>'Example Album', :artist_id=>1, :release_date=>'1979-01-02', :release_party_time=>'1979-01-03 04:05:06', :debut_album=>true, :out_of_print=>false)
+end
-DB.create_table!(:tracks) do
+unless DB.table_exists?(:tracks)
+DB.create_table(:tracks) do
primary_key :id
Integer :number, :null=>false
String :name, :null=>false
@@ -37,20 +42,25 @@
DB[:tracks].insert(:name=>'Example Track 2', :number=>2, :album_id=>1, :length=>6.4)
DB[:tracks].insert(:name=>'Example Track 3', :number=>1, :length=>0.1)
DB[:tracks].insert(:name=>'Example Track 4', :number=>2, :length=>0.2)
+end
-DB.create_table!(:tags) do
+unless DB.table_exists?(:tags)
+DB.create_table(:tags) do
primary_key :id
String :name, :null=>false, :unique=>true
end
1.upto(3){|x| DB[:tags].insert(:name=>"Example Tag #{x}")}
+end
-DB.create_table!(:albums_tags) do
+unless DB.table_exists?(:albums_tags)
+DB.create_table(:albums_tags) do
foreign_key :album_id, :albums
foreign_key :tag_id, :tags
primary_key [:album_id, :tag_id]
end
DB[:albums_tags].insert(1, 1)
DB[:albums_tags].insert(1, 2)
+end
Sequel::Model.plugin :defaults_setter
Sequel::Model.plugin :forme

0 comments on commit 4ef1101

Please sign in to comment.