Module: ActiveRecord::ConnectionAdapters::Spanner::SchemaStatements
- Included in:
- ActiveRecord::ConnectionAdapters::SpannerAdapter
- Defined in:
- lib/active_record/connection_adapters/spanner/schema_statements.rb
Overview
Constant Summary collapse
- VERSION_6_1_0 =
Gem::Version.create "6.1.0"
- VERSION_6_0_3 =
Gem::Version.create "6.0.3"
- VERSION_7_2 =
Gem::Version.create "7.2.0"
Instance Method Summary collapse
- #_add_foreign_key(from_table, to_table, **options) ⇒ Object
- #_remove_columns(table_name, *column_names) ⇒ Object
- #add_column(table_name, column_name, type, **options) ⇒ Object
- #add_foreign_key(from_table, to_table, **options) ⇒ Object
- #add_index(table_name, column_name, options = {}) ⇒ Object
-
#add_reference(table_name, ref_name, **options) ⇒ Object
(also: #add_belongs_to)
Reference Column.
- #assume_migrated_upto_version(version) ⇒ Object
- #change_column(table_name, column_name, type, **options) ⇒ Object
- #change_column_default(_table_name, _column_name, _default_or_changes) ⇒ Object
- #change_column_null(table_name, column_name, null, _default = nil) ⇒ Object
-
#check_constraints(table_name) ⇒ Object
Check Contraints.
-
#column_definitions(table_name) ⇒ Object
Column.
-
#create_join_table(table_1, table_2, column_options: {}, **options) ⇒ Object
Creates a join table that uses all the columns in the table as the primary key by default, unless an explicit primary key has been defined for the table.
- #create_schema_dumper(options) ⇒ Object
- #create_table(table_name, id: :primary_key, **options) {|td| ... } ⇒ Object
- #current_database ⇒ Object
-
#data_sources ⇒ Object
(also: #tables)
Table.
- #drop_table(table_name, options = {}) ⇒ Object
- #extract_schema_qualified_name(string) ⇒ Object
- #fetch_type_metadata(sql_type, ordinal_position = nil, allow_commit_timestamp = nil, generated = nil) ⇒ Object
-
#foreign_keys(table_name, column: nil) ⇒ Object
Foreign Keys.
- #index_name_exists?(table_name, index_name) ⇒ Boolean
-
#indexes(table_name) ⇒ Object
Index.
- #insert_versions_sql(versions) ⇒ Object
- #migration_context ⇒ Object
- #new_column_from_field(_table_name, field, _definitions = nil) ⇒ Object
- #primary_and_parent_keys(table_name) ⇒ Object
-
#primary_keys(table_name) ⇒ Object
Primary Keys.
- #quoted_scope(name = nil, type: nil) ⇒ Object
- #remove_column(table_name, column_name, _type = nil, _options = {}) ⇒ Object
- #remove_columns(table_name, *column_names, _type: nil, **_options) ⇒ Object
- #remove_foreign_key(from_table, to_table = nil, **options) ⇒ Object
- #remove_index(table_name, column_name = nil, **options) ⇒ Object
- #rename_column(table_name, column_name, new_column_name) ⇒ Object
- #rename_index(table_name, old_name, new_name) ⇒ Object
- #rename_table(_table_name, _new_name) ⇒ Object
- #schema_migration ⇒ Object
- #table_exists?(table_name) ⇒ Boolean (also: #data_source_exists?)
-
#type_to_sql(type, limit: nil, precision: nil, scale: nil, **opts) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
Instance Method Details
#_add_foreign_key(from_table, to_table, **options) ⇒ Object
371 372 373 374 375 376 377 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 371 def _add_foreign_key from_table, to_table, ** = from_table, to_table, at = create_alter_table from_table at.add_foreign_key to_table, execute_schema_statements schema_creation.accept(at) end |
#_remove_columns(table_name, *column_names) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 181 def _remove_columns table_name, *column_names if column_names.empty? raise ArgumentError, "You must specify at least one column name. "\ "Example: remove_columns(:people, :first_name)" end statements = [] column_names.each do |column_name| statements.concat drop_column_sql(table_name, column_name) end execute_schema_statements statements end |
#add_column(table_name, column_name, type, **options) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 143 def add_column table_name, column_name, type, ** # Add column with NOT NULL not supported by spanner. # It is currently un-implemented state in spanner service. nullable = .delete(:null) == false at = create_alter_table table_name at.add_column column_name, type, ** statements = [schema_creation.accept(at)] # Alter NOT NULL if nullable cd = at.adds.first.column cd.null = false ccd = Spanner::ChangeColumnDefinition.new( table_name, cd, column_name ) statements << schema_creation.accept(ccd) end execute_schema_statements statements end |
#add_foreign_key(from_table, to_table, **options) ⇒ Object
362 363 364 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 362 def add_foreign_key from_table, to_table, = {} _add_foreign_key from_table, to_table, ** end |
#add_index(table_name, column_name, options = {}) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 271 def add_index table_name, column_name, = {} id = create_index_definition table_name, column_name, ** if data_source_exists?(table_name) && index_name_exists?(table_name, id.name) raise ArgumentError, "Index name '#{id.name}' on table" \ "'#{table_name}' already exists" end execute_schema_statements schema_creation.accept(id) end |
#add_reference(table_name, ref_name, **options) ⇒ Object Also known as: add_belongs_to
Reference Column
392 393 394 395 396 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 392 def add_reference table_name, ref_name, ** ReferenceDefinition.new(ref_name, **).add_to( update_table_definition(table_name, self) ) end |
#assume_migrated_upto_version(version) ⇒ Object
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 415 def assume_migrated_upto_version version version = version.to_i sm_table = quote_table_name schema_migration.table_name migrated = migration_context.get_all_versions versions = migration_context.migrations.map(&:version) execute "INSERT INTO #{sm_table} (version) VALUES (#{quote version.to_s})" unless migrated.include? version inserting = (versions - migrated).select { |v| v < version } return unless inserting.any? if (duplicate = inserting.detect { |v| inserting.count(v) > 1 }) raise "Duplicate migration #{duplicate}. Please renumber your migrations to resolve the conflict." end execute insert_versions_sql(inserting) end |
#change_column(table_name, column_name, type, **options) ⇒ Object
197 198 199 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 197 def change_column table_name, column_name, type, = {} _change_column table_name, column_name, type, ** end |
#change_column_default(_table_name, _column_name, _default_or_changes) ⇒ Object
210 211 212 213 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 210 def change_column_default _table_name, _column_name, _default_or_changes raise ActiveRecordSpannerAdapter::NotSupportedError, \ "change column with default value not supported." end |
#change_column_null(table_name, column_name, null, _default = nil) ⇒ Object
206 207 208 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 206 def change_column_null table_name, column_name, null, _default = nil change_column table_name, column_name, nil, null: null end |
#check_constraints(table_name) ⇒ Object
Check Contraints
401 402 403 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 401 def check_constraints table_name information_schema { |i| i.check_constraints table_name } end |
#column_definitions(table_name) ⇒ Object
Column
118 119 120 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 118 def column_definitions table_name information_schema { |i| i.table_columns table_name } end |
#create_join_table(table_1, table_2, column_options: {}, **options) ⇒ Object
Creates a join table that uses all the columns in the table as the primary key by default, unless an explicit primary key has been defined for the table. ActiveRecord will by default generate join tables without a primary key. Cloud Spanner however requires all tables to have a primary key. Instead of adding an additional column to the table only for the purpose of being the primary key, the Spanner ActiveRecord adapter defines a primary key that contains all the columns in the join table, as all values in the table should be unique anyways.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 98 def create_join_table table_1, table_2, column_options: {}, ** super do |td| unless td.columns.any?(&:primary_key?) td.columns.each do |col| def col.primary_key? true end end end yield td if block_given? end end |
#create_schema_dumper(options) ⇒ Object
454 455 456 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 454 def create_schema_dumper SchemaDumper.create self, end |
#create_table(table_name, id: :primary_key, **options) {|td| ... } ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 53 def create_table table_name, id: :primary_key, ** td = create_table_definition table_name, if id pk = .fetch :primary_key do Base.get_primary_key table_name.to_s.singularize end id = id.fetch :type, :primary_key if id.is_a? Hash if pk.is_a? Array td.primary_keys pk else td.primary_key pk, id, **{} end end yield td if block_given? statements = [] if [:force] statements.concat drop_table_with_indexes_sql(table_name, ) end statements << schema_creation.accept(td) td.indexes.each do |column_name, | id = create_index_definition table_name, column_name, ** statements << schema_creation.accept(id) end execute_schema_statements statements end |
#current_database ⇒ Object
28 29 30 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 28 def current_database @connection.database_id end |
#data_sources ⇒ Object Also known as: tables
Table
34 35 36 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 34 def data_sources information_schema { |i| i.tables.map(&:name) } end |
#drop_table(table_name, options = {}) ⇒ Object
87 88 89 90 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 87 def drop_table table_name, = {} statements = drop_table_with_indexes_sql table_name, execute_schema_statements statements end |
#extract_schema_qualified_name(string) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 44 def extract_schema_qualified_name string schema, name = string.to_s.scan(/[^`.\s]+|`[^`]*`/) unless name name = schema schema = nil end [schema, name] end |
#fetch_type_metadata(sql_type, ordinal_position = nil, allow_commit_timestamp = nil, generated = nil) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 135 def sql_type, ordinal_position = nil, = nil, generated = nil Spanner::TypeMetadata.new \ super(sql_type), ordinal_position: ordinal_position, allow_commit_timestamp: , generated: generated end |
#foreign_keys(table_name, column: nil) ⇒ Object
Foreign Keys
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 339 def foreign_keys table_name, column: nil raise ArgumentError if table_name.blank? result = information_schema { |i| i.foreign_keys table_name } if column result = result.select { |fk| fk.columns.include? column.to_s } end result.map do |fk| = { column: fk.columns.first, name: fk.name, primary_key: fk.ref_columns.first, on_delete: fk.on_update, on_update: fk.on_update } ForeignKeyDefinition.new table_name, fk.ref_table, end end |
#index_name_exists?(table_name, index_name) ⇒ Boolean
267 268 269 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 267 def index_name_exists? table_name, index_name information_schema { |i| i.index table_name, index_name }.present? end |
#indexes(table_name) ⇒ Object
Index
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 248 def indexes table_name result = information_schema do |i| i.indexes table_name, index_type: "INDEX" end result.map do |index| IndexDefinition.new( index.table, index.name, index.columns.map(&:name), unique: index.unique, null_filtered: index.null_filtered, interleave_in: index.interleave_in, storing: index.storing, orders: index.orders ) end end |
#insert_versions_sql(versions) ⇒ Object
434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 434 def insert_versions_sql versions sm_table = quote_table_name schema_migration.table_name if versions.is_a? Array sql = +"INSERT INTO #{sm_table} (version) VALUES\n" sql << versions.reverse.map { |v| "(#{quote v.to_s})" }.join(",\n") sql << ";" sql else "INSERT INTO #{sm_table} (version) VALUES (#{quote versions.to_s});" end end |
#migration_context ⇒ Object
406 407 408 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 406 def migration_context pool.migration_context end |
#new_column_from_field(_table_name, field, _definitions = nil) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 122 def new_column_from_field _table_name, field, _definitions = nil Spanner::Column.new \ field.name, field.default, (field.spanner_type, field.ordinal_position, field., field.generated), field.nullable, field.default_function, primary_key: field.primary_key end |
#primary_and_parent_keys(table_name) ⇒ Object
329 330 331 332 333 334 335 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 329 def primary_and_parent_keys table_name columns = information_schema do |i| i.table_primary_keys table_name, true end columns.map(&:name) end |
#primary_keys(table_name) ⇒ Object
Primary Keys
321 322 323 324 325 326 327 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 321 def primary_keys table_name columns = information_schema do |i| i.table_primary_keys table_name end columns.map(&:name) end |
#quoted_scope(name = nil, type: nil) ⇒ Object
447 448 449 450 451 452 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 447 def quoted_scope name = nil, type: nil scope = { schema: quote("") } scope[:name] = quote name if name scope[:type] = quote type if type scope end |
#remove_column(table_name, column_name, _type = nil, _options = {}) ⇒ Object
166 167 168 169 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 166 def remove_column table_name, column_name, _type = nil, = {} statements = drop_column_sql table_name, column_name execute_schema_statements statements end |
#remove_columns(table_name, *column_names, _type: nil, **_options) ⇒ Object
172 173 174 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 172 def remove_columns table_name, *column_names _remove_columns table_name, *column_names end |
#remove_foreign_key(from_table, to_table = nil, **options) ⇒ Object
379 380 381 382 383 384 385 386 387 388 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 379 def remove_foreign_key from_table, to_table = nil, ** fk_name_to_delete = foreign_key_for!( from_table, to_table: to_table, ** ).name at = create_alter_table from_table at.drop_foreign_key fk_name_to_delete execute_schema_statements schema_creation.accept(at) end |
#remove_index(table_name, column_name = nil, **options) ⇒ Object
284 285 286 287 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 284 def remove_index table_name, = {} index_name = index_name_for_remove table_name, execute "DROP INDEX #{quote_table_name index_name}" end |
#rename_column(table_name, column_name, new_column_name) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 215 def rename_column table_name, column_name, new_column_name if ActiveRecord::Base.connection.ddl_batch? raise ActiveRecordSpannerAdapter::NotSupportedError, \ "rename_column in a DDL Batch is not supported." end column = information_schema do |i| i.table_column table_name, column_name end unless column raise ArgumentError, "Column '#{column_name}' not exist for table '#{table_name}'" end # Add Column cast_type = lookup_cast_type column.spanner_type add_column table_name, new_column_name, cast_type.type, **column. # Copy data copy_data table_name, column_name, new_column_name # Recreate Indexes recreate_indexes table_name, column_name, new_column_name # Recreate Foreign keys recreate_foreign_keys table_name, column_name, new_column_name # Drop Indexes, Drop Foreign keys and columns remove_column table_name, column_name end |
#rename_index(table_name, old_name, new_name) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 295 def rename_index table_name, old_name, new_name validate_index_length! table_name, new_name old_index = information_schema { |i| i.index table_name, old_name } return unless old_index statements = [ schema_creation.accept(DropIndexDefinition.new(old_name)) ] id = IndexDefinition.new \ old_index.table, new_name, old_index.columns.map(&:name), unique: old_index.unique, null_filtered: old_index.null_filtered, interleave_in: old_index.interleave_in, storing: old_index.storing, orders: old_index.orders statements << schema_creation.accept(id) execute_schema_statements statements end |
#rename_table(_table_name, _new_name) ⇒ Object
111 112 113 114 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 111 def rename_table _table_name, _new_name raise ActiveRecordSpannerAdapter::NotSupportedError, \ "rename_table is not implemented" end |
#schema_migration ⇒ Object
410 411 412 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 410 def schema_migration pool.schema_migration end |
#table_exists?(table_name) ⇒ Boolean Also known as: data_source_exists?
39 40 41 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 39 def table_exists? table_name information_schema { |i| i.table table_name }.present? end |
#type_to_sql(type, limit: nil, precision: nil, scale: nil, **opts) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/active_record/connection_adapters/spanner/schema_statements.rb', line 459 def type_to_sql type, limit: nil, precision: nil, scale: nil, **opts type = opts[:passed_type] || type&.to_sym native = native_database_types[type] return type.to_s unless native sql_type = (native.is_a?(Hash) ? native[:name] : native).dup sql_type = "#{sql_type}(#{limit || native[:limit]})" if [:string, :text, :binary].include? type sql_type = "ARRAY<#{sql_type}>" if opts[:array] sql_type end |