Commit c77e4bbc authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor packages migrations to make it work with both PG and MySQL [ci skip]

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 29249536
# This patches ActiveRecord so indexes for binary columns created using the # This patches ActiveRecord so indexes for binary and text columns created
# MySQL adapter apply a length of 20. Otherwise MySQL can't create an index on # using the MySQL adapter apply a length of 20. Otherwise MySQL can't create an
# binary columns. # index on binary and text columns.
module MysqlSetLengthForBinaryIndex module MysqlSetLengthForBinaryAndTextIndex
def add_index(table_name, column_names, options = {}) def add_index(table_name, column_names, options = {})
Array(column_names).each do |column_name| Array(column_names).each do |column_name|
column = ActiveRecord::Base.connection.columns(table_name).find { |c| c.name == column_name } column = ActiveRecord::Base.connection.columns(table_name).find { |c| c.name == column_name }
if column&.type == :binary if column&.type == :binary || column&.type == :text
options[:length] = 20 options[:length] = 20
end end
end end
...@@ -17,5 +17,5 @@ module MysqlSetLengthForBinaryIndex ...@@ -17,5 +17,5 @@ module MysqlSetLengthForBinaryIndex
end end
if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
ActiveRecord::ConnectionAdapters::Mysql2Adapter.send(:prepend, MysqlSetLengthForBinaryIndex) ActiveRecord::ConnectionAdapters::Mysql2Adapter.send(:prepend, MysqlSetLengthForBinaryAndTextIndex)
end end
...@@ -8,8 +8,12 @@ class CreatePackagesPackages < ActiveRecord::Migration ...@@ -8,8 +8,12 @@ class CreatePackagesPackages < ActiveRecord::Migration
def change def change
create_table :packages_packages, id: :bigserial do |t| create_table :packages_packages, id: :bigserial do |t|
t.references :project,
index: true,
foreign_key: { on_delete: :cascade },
null: false
t.timestamps_with_timezone null: false t.timestamps_with_timezone null: false
t.references :project, index: true, foreign_key: { on_delete: :cascade }, null: false
t.string :name, null: false t.string :name, null: false
t.string :version t.string :version
......
...@@ -8,11 +8,11 @@ class CreatePackagesPackageFiles < ActiveRecord::Migration ...@@ -8,11 +8,11 @@ class CreatePackagesPackageFiles < ActiveRecord::Migration
def up def up
create_table :packages_package_files, id: :bigserial do |t| create_table :packages_package_files, id: :bigserial do |t|
t.references :package, type: :bigint, index: true, null: false
t.timestamps_with_timezone null: false t.timestamps_with_timezone null: false
t.bigint :size t.bigint :size
t.references :package, index: true, null: false
t.integer :file_type t.integer :file_type
t.integer :file_store t.integer :file_store
t.binary :file_md5 t.binary :file_md5
......
...@@ -8,8 +8,9 @@ class CreatePackagesMavenMetadata < ActiveRecord::Migration ...@@ -8,8 +8,9 @@ class CreatePackagesMavenMetadata < ActiveRecord::Migration
def up def up
create_table :packages_maven_metadata, id: :bigserial do |t| create_table :packages_maven_metadata, id: :bigserial do |t|
t.references :package, type: :bigint, index: true, null: false
t.timestamps_with_timezone null: false t.timestamps_with_timezone null: false
t.references :package, index: true, null: false
t.string :app_group, null: false t.string :app_group, null: false
t.string :app_name, null: false t.string :app_name, null: false
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment