1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 05:09:38 +02:00

Stock Exchanges with seed (#1351)

* Stock Exchanges with seed

* Run the seed file on migration

* Fix for enum column
This commit is contained in:
Josh Pigford 2024-10-22 14:30:57 -05:00 committed by GitHub
parent d3a6f7e0f0
commit 73e184ad3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 1126 additions and 1 deletions

View file

@ -0,0 +1,2 @@
class StockExchange < ApplicationRecord
end

1020
config/exchanges.yml Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,30 @@
class CreateStockExchanges < ActiveRecord::Migration[7.2]
def change
create_table :stock_exchanges, id: :uuid do |t|
t.string :name, null: false
t.string :acronym
t.string :mic, null: false
t.string :country, null: false
t.string :country_code, null: false
t.string :city, null: false
t.string :website
t.string :timezone_name, null: false
t.string :timezone_abbr, null: false
t.string :timezone_abbr_dst
t.string :currency_code, null: false
t.string :currency_symbol, null: false
t.string :currency_name, null: false
t.timestamps
end
add_index :stock_exchanges, :country
add_index :stock_exchanges, :country_code
add_index :stock_exchanges, :currency_code
reversible do |dir|
dir.up do
load Rails.root.join('db/seeds/exchanges.rb')
end
end
end
end

View file

@ -0,0 +1,16 @@
class FixUserRoleColumnType < ActiveRecord::Migration[7.2]
def change
# First remove any constraints/references to the enum
execute <<-SQL
ALTER TABLE users ALTER COLUMN role TYPE varchar USING role::text;
SQL
# Then set the default
change_column_default :users, :role, 'member'
# Finally drop the enum type
execute <<-SQL
DROP TYPE IF EXISTS user_role;
SQL
end
end

23
db/schema.rb generated
View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2024_10_18_201653) do
ActiveRecord::Schema[7.2].define(version: 2024_10_22_192319) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
@ -506,6 +506,27 @@ ActiveRecord::Schema[7.2].define(version: 2024_10_18_201653) do
t.index ["var"], name: "index_settings_on_var", unique: true
end
create_table "stock_exchanges", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name", null: false
t.string "acronym"
t.string "mic", null: false
t.string "country", null: false
t.string "country_code", null: false
t.string "city", null: false
t.string "website"
t.string "timezone_name", null: false
t.string "timezone_abbr", null: false
t.string "timezone_abbr_dst"
t.string "currency_code", null: false
t.string "currency_symbol", null: false
t.string "currency_name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["country"], name: "index_stock_exchanges_on_country"
t.index ["country_code"], name: "index_stock_exchanges_on_country_code"
t.index ["currency_code"], name: "index_stock_exchanges_on_currency_code"
end
create_table "taggings", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "tag_id", null: false
t.string "taggable_type"

View file

@ -3,3 +3,8 @@
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
puts 'Run the following command to create demo data: `rake demo_data:reset`' if Rails.env.development?
Dir[Rails.root.join('db', 'seeds', '*.rb')].sort.each do |file|
puts "Loading seed file: #{File.basename(file)}"
require file
end

31
db/seeds/exchanges.rb Normal file
View file

@ -0,0 +1,31 @@
# Load exchanges from YAML configuration
exchanges_config = YAML.load_file(Rails.root.join('config', 'exchanges.yml'))
exchanges_config.each do |exchange|
next unless exchange['mic'].present? # Skip any invalid entries
StockExchange.find_or_create_by!(mic: exchange['mic']) do |ex|
ex.name = exchange['name']
ex.acronym = exchange['acronym']
ex.country = exchange['country']
ex.country_code = exchange['country_code']
ex.city = exchange['city']
ex.website = exchange['website']
# Timezone details
if exchange['timezone']
ex.timezone_name = exchange['timezone']['timezone']
ex.timezone_abbr = exchange['timezone']['abbr']
ex.timezone_abbr_dst = exchange['timezone']['abbr_dst']
end
# Currency details
if exchange['currency']
ex.currency_code = exchange['currency']['code']
ex.currency_symbol = exchange['currency']['symbol']
ex.currency_name = exchange['currency']['name']
end
end
end
puts "Created #{StockExchange.count} stock exchanges"