[UNSTABLE] More fixes for new send_message AST node

pull/303/head
Rick Carlino 2016-12-15 14:11:48 -06:00
parent 388899e07e
commit a36ba2fa14
11 changed files with 107 additions and 92 deletions

View File

@ -29,8 +29,8 @@ module CeleryScript
def initialize(name, allowed_args, allowed_body_types)
@name = name
@allowed_args = allowed_args
@allowed_body_types = allowed_body_types
@allowed_args = allowed_args
@allowed_body_types = allowed_body_types
end
end
@ -39,7 +39,7 @@ module CeleryScript
def initialize
@arg_def_list = {}
@node_def_list = {}
@node_def_list = {}
end
def fetchArg(name)
@ -68,7 +68,7 @@ module CeleryScript
end
def as_json(optns)
{ "tag": 0,
{ "tag": SequenceMigration::Base.latest_version,
"args": @arg_def_list.to_a.map(&:last).map{|x| x.as_json({}) },
"nodes": @node_def_list.to_a.map(&:last).map{|x| x.as_json({}) }}
end

View File

@ -1,52 +1,63 @@
class SequenceMigration
# Assume that versionless sequences are "legacy" sequences from a time before
# versioning. Since the lowest migration version is 0, a version of -1 will
# require all migrations
LEGACY_VERSION = -1
VERSION = "YOU MUST CHANGE THIS!!!"
module SequenceMigration
class Base
# Assume that versionless sequences are "legacy" sequences from a time before
# versioning. Since the lowest migration version is 0, a version of -1 will
# require all migrations
LEGACY_VERSION = -1
VERSION = "YOU MUST CHANGE THIS!!!"
def self.latest_version
@latest_version ||= self.descendants.map{ |k| k::VERSION }.max
end
# I shouldn't need to do this, as this method comes with ActiveSupport, but
# its acting weird with autoloading right now :shipit:.
def self.descendants
[
AddVersionInfo,
UpdateChannelNames
]
end
attr_accessor :sequence
def self.latest_version
self.descendants.map{ |k| k::VERSION }.max
end
def initialize(sequence)
@sequence = sequence
end
attr_accessor :sequence
def before
expected_version = self.class::VERSION - 1
incorrect_version = sequence_version != expected_version
if incorrect_version
raise "Version must be #{expected_version} to run #{self.class}"
def initialize(sequence)
@sequence = sequence
end
def before
expected_version = self.class::VERSION - 1
incorrect_version = sequence_version != expected_version
if incorrect_version
raise "Version must be #{expected_version} to run #{self.class}"
end
end
def after
sequence.args["version"] ||= LEGACY_VERSION
sequence.args["version"] += 1
end
def up
throw "You forgot to implement an `up()` method on your migration"
end
def run
before
up
after
end
def self.generate_list(sequence)
theirs = sequence.args["version"] || LEGACY_VERSION
descendants
.select { |x| x::VERSION > theirs }
.sort { |a, b| a::VERSION <=> b::VERSION }
.map { |x| x.new(sequence) }
end
def sequence_version
sequence.args["version"] || LEGACY_VERSION
end
end
def after
sequence.args["version"] ||= LEGACY_VERSION
sequence.args["version"] += 1
end
def up
throw "You forgot to implement an `up()` method on your migration"
end
def run
before
up
after
end
def self.generate_list(sequence)
theirs = sequence.args["version"] || LEGACY_VERSION
descendants
.select { |x| x::VERSION > theirs }
.sort { |a, b| a::VERSION <=> b::VERSION }
.map { |x| x.new(sequence) }
end
def sequence_version
sequence.args["version"] || LEGACY_VERSION
end
end
end

View File

@ -0,0 +1,14 @@
module SequenceMigration
# Background:
# This was the first migration created.
# It will attach a version number of "0" to any sequence which is not under a
# version control scheme. Nothing else.
class AddVersionInfo < Base
VERSION = 0
CREATED_ON = "DECEMBER 15 2016"
def up
# Since we're only incrementing the version, and because versions are
# auto incremented after running up(), there is nothing to do here.
end
end
end

View File

@ -0,0 +1,23 @@
# This migration adds:
# * adds `message_type` to `send_message` nodes
# * Sets all `channel_name`s to "toast", since that's the only one that survived
# this migration.
module SequenceMigration
class UpdateChannelNames < Base
VERSION = 1
CREATED_ON = "DECEMBER 15 2016"
def up
sequence
.body
.select { |x| x["kind"] == "send_message" }
.each { |x| x["args"]["message_type"] = "info" }
.map { |x| x["body"] }
.flatten
.compact
.select { |x| x["kind"] == "channel" }
.compact
.map { |x| x["args"]["channel_name"] = "toast" }
end
end
end

View File

@ -1,12 +0,0 @@
# Background:
# This was the first migration created.
# It will attach a version number of "0" to any sequence which is not under a
# version control scheme. Nothing else.
class AddVersionInfo < SequenceMigration
VERSION = 0
CREATED_ON = "DECEMBER 15 2016"
def up
# Since we're only incrementing the version, and because versions are
# auto incremented after running up(), there is nothing to do here.
end
end

View File

@ -1,21 +0,0 @@
# This migration adds:
# * adds `message_type` to `send_message` nodes
# * Sets all `channel_name`s to "toast", since that's the only one that survived
# this migration.
class UpdateChannelNames < SequenceMigration
VERSION = 1
CREATED_ON = "DECEMBER 15 2016"
def up
sequence
.body
.select { |x| x["kind"] == "send_message" }
.each { |x| x["args"]["message_type"] = "info" }
.map { |x| x["body"] }
.flatten
.compact
.select { |x| x["kind"] == "channel" }
.compact
.map { |x| x["args"]["channel_name"] = "toast" }
end
end

View File

@ -6,7 +6,7 @@ module Sequences
def seq
@seq ||= {body: [],
args: { version: SequenceMigration.latest_version },
args: { version: SequenceMigration::Base.latest_version },
kind: "sequence"}.merge(inputs.symbolize_keys.slice(:body,
:kind,
:args))

View File

@ -14,10 +14,9 @@ module Sequences
def execute
theirs = sequence.args["version"]
ours = SequenceMigration.latest_version
ours = SequenceMigration::Base.latest_version
if theirs != ours
puts "RUNNING MIGRATION ON SEQUENCE ##{sequence.id || 0}"
SequenceMigration.generate_list(sequence).map(&:run)
SequenceMigration::Base.generate_list(sequence).map(&:run)
sequence.args["is_outdated"] = true
end
sequence

View File

@ -29,6 +29,7 @@ module FarmBot
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
end
config.autoload_paths << Rails.root.join('lib')
config.autoload_paths << Rails.root.join('lib/sequence_migrations')
config.middleware.insert_before ActionDispatch::Static, Rack::Cors do
allow do
origins '*'
@ -39,7 +40,7 @@ module FarmBot
max_age: 0
end
end
Rails.application.routes.default_url_options[:host] = ENV["API_HOST"] || "localhost"
Rails.application.routes.default_url_options[:host] = ENV["API_HOST"] || "localhost"
Rails.application.routes.default_url_options[:port] = ENV["API_PORT"] || 3000
# ¯\_(ツ)_/¯
$API_URL = "//#{ Rails.application.routes.default_url_options[:host] }:#{ Rails.application.routes.default_url_options[:port] }"

View File

@ -6,13 +6,13 @@ describe CeleryScript::Corpus do
it "serializes into JSON" do
result = JSON.parse(corpus.to_json)
expect(result["tag"]).to eq(0)
expect(result["tag"]).to eq(1)
expect(result["args"]).to be_kind_of(Array)
expect(result["nodes"]).to be_kind_of(Array)
expect(result["nodes"].sample.keys.sort).to eq(["allowed_args",
"allowed_body_types",
"name"])
"name"])
expect(result["args"].sample.keys.sort).to eq(["allowed_values",
"name"])
"name"])
end
end

View File

@ -2,6 +2,6 @@ require 'spec_helper'
describe SequenceMigration do
it 'has a latest version' do
expect(SequenceMigration.latest_version).to eq(1)
expect(SequenceMigration::Base.latest_version).to eq(1)
end
end