Browse Source

Create database through makefile.

develop
Bèr Kessels 4 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:
30 30
 
31 31
 db-setup:
32 32
 	$(CMD_PREFIX) rake db:create
33
-	$(CMD_PREFIX) rake db:migrate
33
+	$(CMD_PREFIX) rake db:event_store
34
+	$(CMD_PREFIX) rake db:projections
34 35
 
35 36
 ##
36 37
 # Set up the project for building

+ 20
- 13
Rakefile View File

@@ -2,9 +2,8 @@ $LOAD_PATH.unshift '.'
2 2
 
3 3
 require 'dotenv/load'
4 4
 
5
-task :unset_db_name do
6
-  @db_name = ENV['DB_NAME']
7
-  ENV['DB_NAME'] = ''
5
+task :event_sourcery do
6
+  require 'config/event_sourcery.rb'
8 7
 end
9 8
 
10 9
 task :environment do
@@ -42,22 +41,30 @@ task run_web: :environment do
42 41
 end
43 42
 
44 43
 namespace :db do
45
-  desc 'Create database'
46
-  task create: %i[unset_db_name environment] do
47
-    database = Sequel.connect(Hours.config.database_url)
48
-    database.run("CREATE DATABASE #{@db_name}")
49
-    database.disconnect
50
-    ENV['DB_NAME'] = @db_name
44
+  task create: :environment do
45
+    pg_db = Sequel.connect(Hours.config.postgres_database_url)
46
+    begin
47
+      pg_db.run("CREATE DATABASE #{ENV['DB_NAME']}")
48
+      pg_db.disconnect
49
+      app_db = Sequel.connect(Hours.config.database_url)
50
+      app_db.run('CREATE EXTENSION postgis')
51
+    rescue Sequel::DatabaseError => e
52
+      raise unless e.message.include?(
53
+        "database \"#{ENV['DB_NAME']}\" already exists"
54
+      )
55
+
56
+      warn 'database already exists'
57
+    end
51 58
   end
52 59
 
53
-  desc 'Create Event Store database'
54
-  task event_store: :environment do
60
+  desc 'Re Create Event Store datatabase and tables'
61
+  task event_store: %i[environment event_sourcery] do
55 62
     database = EventSourcery::Postgres.config.event_store_database
56 63
     EventSourcery::Postgres::Schema.create_event_store(db: database)
57 64
   end
58 65
 
59
-  desc 'Create Projections databases'
60
-  task projections: :environment do
66
+  desc 'Re Create Projections database and tables'
67
+  task projections: %i[environment event_sourcery] do
61 68
     Hours::Projections::Nodes::Projector.new.setup
62 69
   end
63 70
 end

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

@@ -9,10 +9,10 @@ module Hours
9 9
         projector_name :nodes
10 10
 
11 11
         # Database tables that form the projection.
12
-
13 12
         table :query_nodes do
14 13
           column :node_id, 'UUID NOT NULL'
15 14
           column :name, :text
15
+          column :opening_hours, :text
16 16
           column :location, 'geography(POINT)'
17 17
         end
18 18
 
@@ -23,6 +23,7 @@ module Hours
23 23
           table.insert(
24 24
             node_id: event.aggregate_id,
25 25
             name: event.body['name'],
26
+            opening_hours: event.body['opening_hours'],
26 27
             location: GeoRuby::SimpleFeatures::Point.from_x_y(
27 28
               event.body['lon'],
28 29
               event.body['lat']

+ 5
- 14
config/environment.rb View File

@@ -1,5 +1,7 @@
1
+##
2
+# Setup of database.
1 3
 Hours.configure do |config|
2
-  default = URI::Generic.new(
4
+  default_app_db_url = URI::Generic.new(
3 5
     'postgres',
4 6
     "#{ENV['DB_USER']}:#{ENV['DB_PASSWORD']}",
5 7
     ENV['DB_HOST'],
@@ -9,18 +11,7 @@ Hours.configure do |config|
9 11
     nil,
10 12
     nil,
11 13
     nil
12
-  ).to_s
14
+  )
13 15
 
14
-  config.database_url = ENV['DATABASE_URL'] || default
15
-end
16
-
17
-EventSourcery::Postgres.configure do |config|
18
-  database = Sequel.connect(Hours.config.database_url)
19
-
20
-  # NOTE: Often we choose to split our events and projections into separate
21
-  # databases. For the purposes of this example we'll use one.
22
-  config.event_store_database = database
23
-  config.projections_database = database
24
-
25
-  config.projections_database.extension :postgis_georuby
16
+  config.database_url = ENV['DATABASE_URL'] || default_app_db_url.to_s
26 17
 end

+ 13
- 0
config/event_sourcery.rb View File

@@ -0,0 +1,13 @@
1
+##
2
+# Configure the event sourcery databases.
3
+# This assumes they already exist!
4
+EventSourcery::Postgres.configure do |config|
5
+  database = Sequel.connect(Hours.config.database_url)
6
+
7
+  # NOTE: Often we choose to split our events and projections into separate
8
+  # databases. For the purposes of this example we'll use one.
9
+  config.event_store_database = database
10
+  config.projections_database = database
11
+
12
+  config.projections_database.extension :postgis_georuby
13
+end

+ 1
- 0
lib/app.rb View File

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

+ 6
- 0
lib/hours.rb View File

@@ -27,6 +27,12 @@ module Hours
27 27
   ## Holds the configuration, singleton with a class variable.
28 28
   class Config
29 29
     attr_accessor :database_url
30
+
31
+    # Helper to connect to the main PG database in order to create extensions,
32
+    # drop, alter or create databases.
33
+    def postgres_database_url
34
+      URI.parse(database_url).tap { |url| url.path = '/postgres' }.to_s
35
+    end
30 36
   end
31 37
 
32 38
   def self.config

Loading…
Cancel
Save