Module: PGSpecHelper::Models

Included in:
PGSpecHelper
Defined in:
lib/pg_spec_helper/models.rb

Instance Method Summary collapse

Instance Method Details

#create_model(schema_name, table_name, &block) ⇒ Object

create a new table in the provided schema with an auto incrementing id created_at and updated_at, optionally provide a block which will be yeilded This allows for syntax such as:

create_model :users, :optional_schema_name do |t|

t.add_colmn string :name

end



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pg_spec_helper/models.rb', line 12

def create_model schema_name, table_name, &block
  unless schema_exists? schema_name
    create_schema schema_name
  end
  # create the table
  create_table schema_name, table_name
  # required for auto increment
  connection.exec("    -- temporarily set the client_min_messages to WARNING to\n    -- suppress the NOTICE messages about extension already existing\n    SET client_min_messages TO WARNING;\n    CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";\n    SET client_min_messages TO NOTICE;\n  SQL\n  # create the standard columns\n  create_column schema_name, table_name, :id, :uuid, false, \"uuid_generate_v4()\"\n  create_column schema_name, table_name, :created_at, :timestamp\n  create_column schema_name, table_name, :updated_at, :timestamp\n  # add the primary key\n  create_primary_key schema_name, table_name, [:id], :\"\#{table_name}_pkey\"\n  # execute the optional block\n  if block\n    this = self\n    if this.is_a? PGSpecHelper\n      TableExecuter.new(this, schema_name, table_name, &block)\n    else\n      raise \"Module should be added to PGSpecHelper\"\n    end\n  end\nend\n")