Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #61 from technologiestiftung/fix/unique-constraint…
Browse files Browse the repository at this point in the history
…-shortcode

fix: unique constraint shortcode
  • Loading branch information
dnsos authored Sep 4, 2023
2 parents 8692c98 + e726cbc commit 080d230
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 15 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,17 @@ Once the app is deployed, you need to manually run `fly ssh console -C "/rails/b

In the lifecycle of the app, you might find the following commands useful.

#### Sclaing your app server
#### Restarting the app server

This has become necessary a few times, because the connection between the app and the Redis server had timed out.

```bash
fly apps restart idea-machine
```

> Replace `idea-machine` with your app name on Fly.
#### Scaling your app server

```bash
fly scale vm shared-cpu-2x
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v2/rolls_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Api::V2::RollsController < ApplicationController
before_action :set_game

def create
die_shortcode, side_shortcode = roll_params[:shortcode].chars
die_shortcode, side_shortcode = roll_params[:shortcode]&.chars
die = @game.dice.where(shortcode: die_shortcode)
side = Side.where(shortcode: side_shortcode.to_i).where(die: die).first

Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20230901151145_remove_shortcode_index_from_dice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class RemoveShortcodeIndexFromDice < ActiveRecord::Migration[7.0]
def change
if index_exists?(:dice, :shortcode)
remove_index :dice, :shortcode
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class RemoveShortcodeGameIndexFromDiceIfExists < ActiveRecord::Migration[7.0]
def change
if index_exists?(:dice, [:shortcode, :game_id])
remove_index :dice, [:shortcode, :game_id]
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20230901153201_make_game_non_nullable_on_dice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MakeGameNonNullableOnDice < ActiveRecord::Migration[7.0]
def change
change_column_null(:dice, :game_id, false)
end
end
7 changes: 7 additions & 0 deletions db/migrate/20230901153508_add_game_index_to_dice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddGameIndexToDice < ActiveRecord::Migration[7.0]
def change
unless index_exists?(:dice, :game_id)
add_index :dice, :game_id
end
end
end
7 changes: 7 additions & 0 deletions db/migrate/20230901153626_add_shortcode_game_index_to_dice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddShortcodeGameIndexToDice < ActiveRecord::Migration[7.0]
def change
unless index_exists?(:dice, [:shortcode, :game_id])
add_index :dice, [:shortcode, :game_id], unique: true
end
end
end
5 changes: 3 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/data/tempelhofer_feld.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"game": {
"title": "Ideenwürfel beim Kiezlabor im Graefekiez",
"title": "Ideenwürfel beim Kiezlabor auf dem Tempelhofer Feld",
"dice": [
{
"title": "focus_group",
Expand Down
7 changes: 3 additions & 4 deletions test/controllers/api/v2/rolls_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

class Api::V2::RollsControllerTest < ActionDispatch::IntegrationTest
setup do
@game = games(:summer)
@game = games(:autumn)
@roll = rolls(:one)
@valid_shortcode = "#{dice(:one).shortcode}#{sides(:one).shortcode}"
@invalid_shortcode = "#{dice(:one).shortcode}#{sides(:two).shortcode}" # Invalid because side does not belong to die
@valid_shortcode = "#{dice(:four).shortcode}#{sides(:five).shortcode}"
@authorization = "Bearer #{Rails.application.credentials.http_token}"
end

Expand All @@ -21,7 +20,7 @@ class Api::V2::RollsControllerTest < ActionDispatch::IntegrationTest
end

test "responds with status code :unprocessable_entity after unprocessable creation attempt" do
post api_v2_game_rolls_url(@game), params: {shortcode: @invalid_shortcode}, headers: {Authorization: @authorization}, as: :json
post api_v2_game_rolls_url(@game), params: {shortcode: nil}, headers: {Authorization: @authorization}, as: :json
assert_response :unprocessable_entity
end
end
17 changes: 14 additions & 3 deletions test/fixtures/dice.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ three:
shortcode: "C"
game: summer

unused:
title: "vegetables"
shortcode: "C"
four:
title: "focus_group"
shortcode: "A"
game: autumn

five:
title: "topic"
shortcode: "B"
game: autumn

six:
title: "medium"
shortcode: "C"
game: autumn
14 changes: 12 additions & 2 deletions test/fixtures/sides.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ three:
title: "App"
shortcode: 13

unused:
die: unused
four:
die: four
title: "Cucumber"
shortcode: 21

five:
die: four
title: "Tomato"
shortcode: 4

six:
die: four
title: "Banana"
shortcode: 7
2 changes: 1 addition & 1 deletion test/services/idea_generation_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class IdeaGenerationServiceTest < ActiveSupport::TestCase
setup do
@valid_sides = Side.limit(3)
@invalid_sides = dice(:unused).sides
@invalid_sides = dice(:four).sides
end

test "generates idea with valid input" do
Expand Down

0 comments on commit 080d230

Please sign in to comment.