Browse Source

Create database through makefile.

tags/0.3.0^2
Bèr Kessels 11 months ago
parent
commit
a6bd6bf9ab
7 changed files with 49 additions and 29 deletions
  1. 2
    1
      Makefile
  2. 20
    13
      Rakefile
  3. 2
    1
      app/projections/nodes.rb
  4. 5
    14
      config/environment.rb
  5. 13
    0
      config/event_sourcery.rb
  6. 1
    0
      lib/app.rb
  7. 6
    0
      lib/hours.rb

+ 2
- 1
Makefile View File

@@ -30,7 +30,8 @@ docker:

db-setup:
$(CMD_PREFIX) rake db:create
$(CMD_PREFIX) rake db:migrate
$(CMD_PREFIX) rake db:event_store
$(CMD_PREFIX) rake db:projections

##
# Set up the project for building

+ 20
- 13
Rakefile View File

@@ -2,9 +2,8 @@ $LOAD_PATH.unshift '.'

require 'dotenv/load'

task :unset_db_name do
@db_name = ENV['DB_NAME']
ENV['DB_NAME'] = ''
task :event_sourcery do
require 'config/event_sourcery.rb'
end

task :environment do
@@ -42,22 +41,30 @@ task run_web: :environment do
end

namespace :db do
desc 'Create database'
task create: %i[unset_db_name environment] do
database = Sequel.connect(Hours.config.database_url)
database.run("CREATE DATABASE #{@db_name}")
database.disconnect
ENV['DB_NAME'] = @db_name
task create: :environment do
pg_db = Sequel.connect(Hours.config.postgres_database_url)
begin
pg_db.run("CREATE DATABASE #{ENV['DB_NAME']}")
pg_db.disconnect
app_db = Sequel.connect(Hours.config.database_url)
app_db.run('CREATE EXTENSION postgis')
rescue Sequel::DatabaseError => e
raise unless e.message.include?(
"database \"#{ENV['DB_NAME']}\" already exists"
)

warn 'database already exists'
end
end

desc 'Create Event Store database'
task event_store: :environment do
desc 'Re Create Event Store datatabase and tables'
task event_store: %i[environment event_sourcery] do
database = EventSourcery::Postgres.config.event_store_database
EventSourcery::Postgres::Schema.create_event_store(db: database)
end

desc 'Create Projections databases'
task projections: :environment do
desc 'Re Create Projections database and tables'
task projections: %i[environment event_sourcery] do
Hours::Projections::Nodes::Projector.new.setup
end
end

+ 2
- 1
app/projections/nodes.rb View File

@@ -9,10 +9,10 @@ module Hours
projector_name :nodes

# Database tables that form the projection.

table :query_nodes do
column :node_id, 'UUID NOT NULL'
column :name, :text
column :opening_hours, :text
column :location, 'geography(POINT)'
end

@@ -23,6 +23,7 @@ module Hours
table.insert(
node_id: event.aggregate_id,
name: event.body['name'],
opening_hours: event.body['opening_hours'],
location: GeoRuby::SimpleFeatures::Point.from_x_y(
event.body['lon'],
event.body['lat']

+ 5
- 14
config/environment.rb View File

@@ -1,5 +1,7 @@
##
# Setup of database.
Hours.configure do |config|
default = URI::Generic.new(
default_app_db_url = URI::Generic.new(
'postgres',
"#{ENV['DB_USER']}:#{ENV['DB_PASSWORD']}",
ENV['DB_HOST'],
@@ -9,18 +11,7 @@ Hours.configure do |config|
nil,
nil,
nil
).to_s
)

config.database_url = ENV['DATABASE_URL'] || default
end

EventSourcery::Postgres.configure do |config|
database = Sequel.connect(Hours.config.database_url)

# NOTE: Often we choose to split our events and projections into separate
# databases. For the purposes of this example we'll use one.
config.event_store_database = database
config.projections_database = database

config.projections_database.extension :postgis_georuby
config.database_url = ENV['DATABASE_URL'] || default_app_db_url.to_s
end

+ 13
- 0
config/event_sourcery.rb View File

@@ -0,0 +1,13 @@
##
# Configure the event sourcery databases.
# This assumes they already exist!
EventSourcery::Postgres.configure do |config|
database = Sequel.connect(Hours.config.database_url)

# NOTE: Often we choose to split our events and projections into separate
# databases. For the purposes of this example we'll use one.
config.event_store_database = database
config.projections_database = database

config.projections_database.extension :postgis_georuby
end

+ 1
- 0
lib/app.rb View File

@@ -1,5 +1,6 @@
# This contains the Sinatra App
require_relative './hours.rb'
require_relative '../config/event_sourcery.rb'

require 'sinatra'


+ 6
- 0
lib/hours.rb View File

@@ -27,6 +27,12 @@ module Hours
## Holds the configuration, singleton with a class variable.
class Config
attr_accessor :database_url

# Helper to connect to the main PG database in order to create extensions,
# drop, alter or create databases.
def postgres_database_url
URI.parse(database_url).tap { |url| url.path = '/postgres' }.to_s
end
end

def self.config

Loading…
Cancel
Save