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

Commit

Permalink
hotfix: seeds and added env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusGautamah committed Dec 8, 2022
1 parent db822e5 commit dfc7101
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
FIRST_CHAIN_NAME=YOUR_CHAIN_NAME
FIRST_CHAIN_MAINTAINER=YOUR_NAME
FIRST_USER_NAME=YOUR_FIRST_USERNAME
FIRST_USER_EMAIL=firstuser@email.com
FIRST_USER_PASSWORD=YOUR_FIRST_USER_PASSWORD
CONTRACTS_LIMIT=2
SIGNATURES_LIMIT=5
REDIS_URL=redis://redis:6379
ENV_MACHINE=docker

3 changes: 2 additions & 1 deletion app/models/chain.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

class Chain < ApplicationRecord
validates :name, presence: true, uniqueness: true, length: { maximum: 20 }
validates :name, presence: true, uniqueness: true, length: { maximum: 40 }
validates :maintainer, presence: true, length: { maximum: 50 }
validates :chain_version, presence: true, length: { maximum: 20 }
validates :description, presence: true, length: { maximum: 1000 }

has_many :blocks, dependent: :destroy
end

27 changes: 17 additions & 10 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def seed_exec
create_first_chain
create_acceptable_word_lists
create_first_block
create_first_user
end

def dev_seed_exec
Expand All @@ -19,24 +20,21 @@ def dev_seed_exec
end

def create_first_chain
Chain.create(
chain = Chain.find_or_create_by(
name: ENV["FIRST_CHAIN_NAME"],
maintainer: ENV["FIRST_CHAIN_MAINTAINER"],
chain_version: "0.0.1",
description: "This is a blockchain project generated with outerspace-blockchain."
)

puts "Chain created"
puts "Chain name: #{ENV["FIRST_CHAIN_NAME"]}"
puts "Chain maintainer: #{ENV["FIRST_CHAIN_MAINTAINER"]}"
chain.present? ? puts("First chain OK") : puts("Chain seed error")
end

def create_acceptable_word_lists
create_acceptable_words
create_acceptable_symbol_sequences
create_acceptable_number_sequences
end

def create_acceptable_words
word_list = %w[the of and to a in
is you that it he
Expand Down Expand Up @@ -93,18 +91,16 @@ def create_acceptable_number_sequences


def create_first_block
Block.create(
block = Block.find_or_create_by(
chain: Chain.first,
previous_hash: "0000000000000000000000000000000000000000000000000000000000000000",
block_data: "This is the first block of the blockchain.",
nonce: 0,
connections: 0,
contracts_count: 0,
contracts_limit: ENV["CONTRACTS_LIMIT"].to_i,
# timestamp: Time.now
)

puts "First block created"
block.present? ? puts("First block OK") : puts("Block seed error")
end

def create_test_users
Expand All @@ -127,6 +123,17 @@ def create_test_users
end
end

def create_first_user
if User.find_by(email: ENV["FIRST_USER_EMAIL"], username: ENV["FIRST_USER_USERNAME"])
puts "First user already exists"
else
User.create(
email: ENV["FIRST_USER_EMAIL"], username: ENV["FIRST_USER_USERNAME"], password: ENV["FIRST_USER_PASSWORD"], password_confirmation: ENV["FIRST_USER_PASSWORD"]
)
puts "First user created"
end
end

if Rails.env.development?
dev_seed_exec
else
Expand Down

0 comments on commit dfc7101

Please sign in to comment.