Skip to content

Commit

Permalink
Introduce json for mysql into template and generators
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszreszke committed Feb 7, 2023
1 parent 5ba316a commit 8e4850b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
13 changes: 9 additions & 4 deletions rails_event_store/spec/rails_migration_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,19 @@ module RailsEventStore

context "json type is not used when adapter is not postgres" do
let(:data_type) { "json" }
it { is_expected.to match(/t.binary\s+:metadata/) }
it { is_expected.to match(/t.binary\s+:data/) }
it { is_expected.to match(/t.json\s+:metadata/) }
it { is_expected.to match(/t.json\s+:data/) }
end

context "jsonb type is not used when adapter is not postgres" do
let(:data_type) { "jsonb" }
it { is_expected.to match(/t.binary\s+:metadata/) }
it { is_expected.to match(/t.binary\s+:data/) }
it "raises an error" do
expect { RubyEventStore::ActiveRecord::RailsMigrationGenerator.new([], data_type: data_type) }
.to raise_error(
RubyEventStore::ActiveRecord::RailsMigrationGenerator::Error,
"jsonb is not supported for MySQL. Please use binary or json."
)
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def initialize(*args)
if DATA_TYPES.exclude?(data_type)
raise Error, "Invalid value for --data-type option. Supported for options are: #{DATA_TYPES.join(", ")}."
end

if adapter == "mysql2" && data_type == "jsonb"
raise Error, "jsonb is not supported for MySQL. Please use binary or json."
end
end

def create_migration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class CreateEventStoreEvents < ActiveRecord::Migration[<%= migration_version %>]
create_table(:event_store_events, force: false) do |t|
t.references :event, null: false, type: :string, limit: 36, index: { unique: true }
t.string :event_type, null: false, index: true
t.binary :metadata
t.binary :data, null: false
t.<%= data_type %> :metadata
t.<%= data_type %> :data, null: false
t.datetime :created_at, null: false, precision: 6, index: true
t.datetime :valid_at, null: true, precision: 6, index: true
end
Expand Down
18 changes: 16 additions & 2 deletions ruby_event_store-active_record/spec/migration_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,27 @@ module ActiveRecord
expect(read_migration(@dir)).to match(/t.binary\s+:metadata/)
end

specify "creates migration with binary data type when adapter is not postgres" do
migration_generator(@dir, "json", "MySQL2")
specify "creates migration with binary data type when adapter is sqlite" do
migration_generator(@dir, "json", "sqlite")

expect(read_migration(@dir)).to match(/t.binary\s+:data/)
expect(read_migration(@dir)).to match(/t.binary\s+:metadata/)
end

xspecify "creates migration with jsonb data type when adapter is not postgres" do
migration_generator(@dir, "jsonb", "MySQL2")

expect(read_migration(@dir)).to match(/t.binary\s+:data/)
expect(read_migration(@dir)).to match(/t.binary\s+:metadata/)
end

specify "creates migration with json data type when adapter is MySQL2" do
migration_generator(@dir, "json", "MySQL2")

expect(read_migration(@dir)).to match(/t.json\s+:data/)
expect(read_migration(@dir)).to match(/t.json\s+:metadata/)
end

specify "creates migration with json data type" do
migration_generator(@dir, "json", "PostgreSQL")

Expand Down
10 changes: 8 additions & 2 deletions ruby_event_store-active_record/spec/migration_test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ module ActiveRecord
specify "mysql" do
skip unless ENV["DATABASE_URL"].include?("mysql")

data_type = data_type_to_mysql_type(ENV["DATA_TYPE"])

my_sql_major_version = ::ActiveRecord::Base.connection.select_value("SELECT VERSION();").to_i
collation = my_sql_major_version == 8 ? " COLLATE=utf8mb4_0900_ai_ci" : ""
charset = my_sql_major_version == 8 ? "utf8mb4" : "latin1"
Expand All @@ -79,8 +81,8 @@ module ActiveRecord
`id` bigint#{bigint_lenght} NOT NULL AUTO_INCREMENT,
`event_id` varchar(36) NOT NULL,
`event_type` varchar(255) NOT NULL,
`metadata` blob,
`data` blob NOT NULL,
`metadata` #{data_type}#{" DEFAULT NULL" if data_type == "json"},
`data` #{data_type} NOT NULL,
`created_at` datetime(6) NOT NULL,
`valid_at` datetime(6) DEFAULT NULL,
PRIMARY KEY (`id`),
Expand Down Expand Up @@ -129,6 +131,10 @@ def data_type_to_pg_type(data_type)
{ "binary" => "bytea", "json" => "json", "jsonb" => "jsonb" }.fetch(data_type)
end

def data_type_to_mysql_type(data_type)
{ "binary" => "blob", "json" => "json" }.fetch(data_type)
end

def pg_show_create_table(table_name)
db_config = ::ActiveRecord::Base.connection_db_config
<<~RESULT.strip
Expand Down

0 comments on commit 8e4850b

Please sign in to comment.