File: active_record_setup.rb

package info (click to toggle)
ruby-graphql 2.5.19-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,868 kB
  • sloc: ruby: 80,420; ansic: 1,808; yacc: 845; javascript: 480; makefile: 6
file content (256 lines) | stat: -rw-r--r-- 6,760 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# frozen_string_literal: true
if testing_rails?
  # Remove the old sqlite database
  sqlite_path = File.expand_path(File.join(__FILE__, "../../../_test_.db"))
  puts "Removing #{sqlite_path}"
  `rm -f #{sqlite_path}`

  if ActiveRecord.respond_to?(:async_query_executor=) # Rails 7.1+
    ActiveRecord.async_query_executor ||= :global_thread_pool
  end

  if ENV['DATABASE'] == 'POSTGRESQL'
    ar_connection_options = {
      host: "localhost",
      adapter: "postgresql",
      username: "postgres",
      password: ENV["PGPASSWORD"], # empty in development, populated for GH Actions
      database: "graphql_ruby_test",
    }
    ActiveRecord::Base.establish_connection(ar_connection_options.merge(
      database: "postgres"
    ))
    databases = ActiveRecord::Base.connection.execute("select datname from pg_database;")
    test_db = databases.find { |d| d["datname"] == "graphql_ruby_test" }
    if test_db
      ActiveRecord::Base.connection.execute("drop database graphql_ruby_test;")
    end
    ActiveRecord::Base.connection.execute("create database graphql_ruby_test;")

    ActiveRecord::Base.configurations = {
      starwars: ar_connection_options,
      starwars_replica: ar_connection_options,
    }

    SequelDB = Sequel.connect("postgres://postgres:#{ENV["PGPASSWORD"]}@localhost:5432/graphql_ruby_test")
  else
    ActiveRecord::Base.configurations = {
      starwars: { adapter: "sqlite3", database: sqlite_path },
      starwars_replica: { adapter: "sqlite3", database: sqlite_path },
    }
    SequelDB = Sequel.sqlite(sqlite_path)
  end

  ActiveRecord::Base.establish_connection(:starwars)
  ActiveRecord::Schema.define(force: true) do
    self.verbose = !!ENV["GITHUB_ACTIONS"]
    create_table :bases, force: true do |t|
      t.column :name, :string
      t.column :planet, :string
      t.column :faction_id, :integer
    end

    create_table :foods, force: true do |t|
      t.column :name, :string
    end

    create_table :things, force: true do |t|
      t.string :name
      t.integer :other_thing_id
    end

    create_table :bands, force: true do |t|
      t.string :name
      t.integer :genre
      t.integer :thing_id
      t.string :thing_type
    end

    create_table :albums, force: true do |t|
      t.string :name
      t.integer :band_id
      t.string :band_name
      t.integer :band_genre
      t.timestamps
    end

    create_table :books do |t|
      t.string :title
      t.integer :author_id
    end

    create_table :reviews do |t|
      t.integer :stars
      t.integer :user_id
      t.integer :book_id
    end

    create_table :authors do |t|
      t.string :name
    end

    create_table :users do |t|
      t.string :username
    end

    create_table :input_test_users, force: true do |t|
      t.datetime :created_at
      t.date :birthday
      t.integer :points
      t.decimal :rating
      t.references :friend, foreign_key: { to_table: :input_test_users}
    end

    create_table :test_users, force: true do |t|
      t.datetime :created_at
      t.date :birthday
      t.integer :points, null: false
      t.decimal :rating, null: false
    end

    create_table :graphql_detailed_traces, force: true do |t|
      t.bigint :begin_ms
      t.float :duration_ms
      t.binary :trace_data
      t.string :operation_name
    end
  end

  class Food < ActiveRecord::Base
    include GlobalID::Identification
  end

  class Album < ActiveRecord::Base
    belongs_to :band
    enum :band_genre, [:rock, :country, :jazz]
    if Rails::VERSION::STRING > "8"
      belongs_to :composite_band, foreign_key: [:band_name, :band_genre]
    elsif Rails::VERSION::STRING > "7.1"
      belongs_to :composite_band, query_constraints: [:band_name, :band_genre]
    end

    before_save :populate_band_fields

    def populate_band_fields
      self.band_name = self.band.name
      self.band_genre = self.band.genre
    end
  end
  class Band < ActiveRecord::Base
    has_many :albums
    enum :genre, [:rock, :country, :jazz]
    belongs_to :thing, polymorphic: true
  end

  class AlternativeBand < Band
    self.table_name = :bands
    self.primary_key = :name
  end

  class CompositeBand < Band
    self.table_name = :bands
    self.primary_key = [:name, :genre]
  end

  v = Band.create!(id: 1, name: "Vulfpeck", genre: :rock)
  t = Band.create!(id: 2, name: "Tom's Story", genre: :rock, thing: v)
  c = Band.create!(id: 3, name: "Chon", genre: :rock, thing: v)
  w = Band.create!(id: 4, name: "Wilco", genre: :country, thing: v)

  v.albums.create!(id: 1, name: "Mit Peck")
  v.albums.create!(id: 2, name: "My First Car")
  t.albums.create!(id: 3, name: "Tom's Story")
  c.albums.create!(id: 4, name: "Homey")
  c.albums.create!(id: 5, name: "Chon")
  w.albums.create!(id: 6, name: "Summerteeth")
  class Author < ActiveRecord::Base
    has_many :books

    def self.inspect
      sleep 0.2
      super
    end
  end

  class User < ActiveRecord::Base
    has_many :reviews
  end

  class Book < ActiveRecord::Base
    has_many :reviews
    belongs_to :author
  end

  class Review < ActiveRecord::Base
    belongs_to :user
    belongs_to :book
  end

  data = [
    {
      author: "William Shakespeare",
      titles: [
        "A Midsummer Night's Dream",
        "The Merry Wives of Windsor",
        "Much Ado about Nothing",
        "Julius Caesar",
        "Hamlet",
        "King Lear",
        "Macbeth",
        "Romeo and Juliet",
        "Othello"
      ]
    },
    {
      author: "Beatrix Potter",
      titles: [
        "The Tale of Peter Rabbit",
        "The Tale of Squirrel Nutkin",
        "The Tailor of Gloucester",
        "The Tale of Benjamin Bunny",
        "The Tale of Two Bad Mice",
        "The Tale of Mrs. Tiggy-Winkle",
        "The Tale of The Pie and the Patty-Pan",
        "The Tale of Mr. Jeremy Fisher",
        "The Story of a Fierce Bad Rabbit",
      ]
    },
    {
      author: "Charles Dickens",
      titles: [
        "The Pickwick Papers",
        "Oliver Twist",
        "A Christmas Carol",
        "David Copperfield",
        "Little Dorrit 	",
        "A Tale of Two Cities",
        "Great Expectations",
      ]
    },
    {
      author: "한강",
      titles: [
        "작별하지 않는다",
        "흰",
        "소년이 온다",
        "노랑무늬영원"
      ]
    }
  ]

  data.each do |info|
    author = Author.create!(name: info[:author])
    info[:titles].each do |title|
      Book.create!(author: author, title: title)
    end
  end

  users = ["matz", "tenderlove", "dhh", "_why"].map { |un| User.create!(username: un) }

  possible_stars = [1,2,3,4,5]
  Book.all.each do |book|
    users.each_with_index do |user, idx|
      Review.create!(book: book, user: user, stars: possible_stars[idx])
    end
  end
end