Module: ActiveRecord::ConnectionAdapters::SchemaStatements

Defined in:
lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#add_content_column(table_name, column_name, type, options = {}) ⇒ Object

Adds a column to both the primary and versioned table. Save needing two calls. This is only needed if your content block is versioned, otherwise add_column will work just fine.



111
112
113
114
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 111

def add_content_column(table_name, column_name, type, options={})
  add_column table_name, column_name, type, options
  add_column version_table_name(table_name), column_name, type, options
end

#content_table_exists?(table_name) ⇒ Boolean

Will namespace the content table

Returns:

  • (Boolean)


122
123
124
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 122

def content_table_exists?(table_name)
  table_exists?(Cms::Namespacing.prefixed_table_name(table_name))
end

#create_content_table(table_name, options = {}) {|t| ... } ⇒ Object Also known as: create_versioned_table

Pass in “:versioned => false” in the options hash to create a non-versioned table.

Parameters:

  • table_name
  • options (defaults to: {})
  • :prefix (Hash)

    a customizable set of options

Yields:

  • (t)


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 17

def create_content_table(table_name, options={}, &block)
  defaults = {
      versioned: true,
      prefix: true
  }
  options = defaults.merge(options)

  if options[:prefix]
    table_name = Cms::Namespacing.prefixed_table_name(table_name)
  end

  #Create the primary table
  t = TableDefinition.new(self)
  t.primary_key(options[:primary_key] || Base.get_primary_key(table_name)) unless options[:id] == false

  unless options[:versioned] == false
    t.integer :version
    t.integer :lock_version, :default => 0
  end

  yield t

  # Blocks currently must have a name column, otherwise the UI fails in several places.
  # Some migrations may have already specified a name attribute, so we don't want to overwrite it here.
  t.string :name unless t[:name]

  t.boolean :published, :default => false
  t.boolean :deleted, :default => false
  t.boolean :archived, :default => false
  t.integer :created_by_id
  t.integer :updated_by_id
  t.timestamps

  create_table_from_definition(table_name, options, t)

  unless options[:versioned] == false
    #Do the versions table
    vt = TableDefinition.new(self)
    vt.primary_key(options[:primary_key] || Base.get_primary_key(table_name)) unless options[:id] == false

    vt.integer :original_record_id
    vt.integer :version
    yield vt

    # Create implicit name column in version table as well.
    vt.string :name unless vt[:name]

    vt.boolean :published, :default => false
    vt.boolean :deleted, :default => false
    vt.boolean :archived, :default => false
    vt.string :version_comment
    vt.integer :created_by_id
    vt.integer :updated_by_id
    vt.timestamps

    create_table_from_definition("#{table_name.to_s.singularize}_versions".to_sym, options, vt)
  end

end

#create_table_from_definition(table_name, options, table_definition) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 83

def create_table_from_definition(table_name, options, table_definition)
  if options[:force] && table_exists?(table_name)
    drop_table(table_name, options)
  end

  create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
  create_sql << "#{quote_table_name(table_name)} ("
  create_sql << table_definition.to_sql
  create_sql << ") #{options[:options]}"
  execute create_sql
end

#drop_versioned_table(table_name) ⇒ Object Also known as: drop_content_table



95
96
97
98
99
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 95

def drop_versioned_table(table_name)
  table_name = Cms::Namespacing.prefixed_table_name(table_name)
  drop_table "#{table_name.singularize}_versions".to_sym
  drop_table table_name
end

#prefix(table_name) ⇒ Object

Applies the current CMS prefix to the given table name. Useful for where you need CMS related migrations using stock rails migrations (like add_index or create_table) but still want to dynamically apply the CMS prefixes.



8
9
10
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 8

def prefix(table_name)
  Cms::Namespacing.prefixed_table_name(table_name)
end

#remove_content_column(table_name, column_name) ⇒ Object



116
117
118
119
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 116

def remove_content_column(table_name, column_name)
  remove_column table_name, column_name
  remove_column version_table_name(table_name), column_name
end

#rename_content_column(table_name, old_name, new_name) ⇒ Object

Rename a column for both its



104
105
106
107
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 104

def rename_content_column(table_name, old_name, new_name)
  rename_column table_name, old_name, new_name
  rename_column version_table_name(table_name), old_name, new_name
end