Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Don't drop tables if they already exist

  • Loading branch information...
commit 4ef110188b0c3c1268eaaf30bb64a990da27f498 1 parent b771802
Jeremy Evans authored

Showing 1 changed file with 15 additions and 5 deletions. Show diff stats Hide diff stats

  1. +15 5 demo-site/models.rb
20 demo-site/models.rb
@@ -7,13 +7,16 @@
7 7 DB = Sequel.connect(ENV['DATABASE_URL'] || 'sqlite:/')
8 8 DB.loggers << Logger.new($stdout) unless DEMO_MODE
9 9
10   -DB.create_table!(:artists) do
  10 +unless DB.table_exists?(:artists)
  11 +DB.create_table(:artists) do
11 12 primary_key :id
12 13 String :name, :null=>false, :unique=>true
13 14 end
14 15 1.upto(3){|x| DB[:artists].insert(:name=>"Example Artist #{x}")}
  16 +end
15 17
16   -DB.create_table!(:albums) do
  18 +unless DB.table_exists?(:albums)
  19 +DB.create_table(:albums) do
17 20 primary_key :id
18 21 String :name, :null=>false
19 22 foreign_key :artist_id, :artists, :null=>false
@@ -25,8 +28,10 @@
25 28 index [:name, :artist_id], :unique=>true
26 29 end
27 30 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)
  31 +end
28 32
29   -DB.create_table!(:tracks) do
  33 +unless DB.table_exists?(:tracks)
  34 +DB.create_table(:tracks) do
30 35 primary_key :id
31 36 Integer :number, :null=>false
32 37 String :name, :null=>false
@@ -37,20 +42,25 @@
37 42 DB[:tracks].insert(:name=>'Example Track 2', :number=>2, :album_id=>1, :length=>6.4)
38 43 DB[:tracks].insert(:name=>'Example Track 3', :number=>1, :length=>0.1)
39 44 DB[:tracks].insert(:name=>'Example Track 4', :number=>2, :length=>0.2)
  45 +end
40 46
41   -DB.create_table!(:tags) do
  47 +unless DB.table_exists?(:tags)
  48 +DB.create_table(:tags) do
42 49 primary_key :id
43 50 String :name, :null=>false, :unique=>true
44 51 end
45 52 1.upto(3){|x| DB[:tags].insert(:name=>"Example Tag #{x}")}
  53 +end
46 54
47   -DB.create_table!(:albums_tags) do
  55 +unless DB.table_exists?(:albums_tags)
  56 +DB.create_table(:albums_tags) do
48 57 foreign_key :album_id, :albums
49 58 foreign_key :tag_id, :tags
50 59 primary_key [:album_id, :tag_id]
51 60 end
52 61 DB[:albums_tags].insert(1, 1)
53 62 DB[:albums_tags].insert(1, 2)
  63 +end
54 64
55 65 Sequel::Model.plugin :defaults_setter
56 66 Sequel::Model.plugin :forme

0 comments on commit 4ef1101

Please sign in to comment.
Something went wrong with that request. Please try again.