From 1dead8c1a3e190b3e4b9077b331d11b0f7257171 Mon Sep 17 00:00:00 2001 From: Yacine Petitprez Date: Wed, 25 Sep 2019 11:46:03 +0700 Subject: [PATCH 1/5] migration to crystal 0.31 --- shard.lock | 4 ++-- shard.yml | 4 ++-- src/clear/expression/nodes/between.cr | 2 +- src/clear/expression/nodes/double_operator.cr | 2 +- src/clear/expression/nodes/function.cr | 2 +- src/clear/expression/nodes/in_array.cr | 2 +- src/clear/expression/nodes/in_range.cr | 2 +- src/clear/extensions/time/interval.cr | 2 +- src/clear/extensions/time/time_in_day.cr | 8 ++++---- src/clear/sql/connection_pool.cr | 2 +- src/clear/version.cr | 2 +- 11 files changed, 16 insertions(+), 16 deletions(-) diff --git a/shard.lock b/shard.lock index 23747d25b..58e7b48af 100644 --- a/shard.lock +++ b/shard.lock @@ -10,7 +10,7 @@ shards: db: github: crystal-lang/crystal-db - version: 0.5.1 + version: 0.6.0 generate: github: anykeyh/generate.cr @@ -22,5 +22,5 @@ shards: pg: github: will/crystal-pg - version: 0.17.0 + version: 0.18.1 diff --git a/shard.yml b/shard.yml index fe33bfd85..c867c2f9f 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: clear -version: 0.7 +version: 0.7.1 authors: - Yacine Petitprez @@ -30,6 +30,6 @@ development_dependencies: github: veelenga/ameba branch: master -crystal: 0.30 +crystal: 0.31 license: MIT diff --git a/src/clear/expression/nodes/between.cr b/src/clear/expression/nodes/between.cr index aaef90b0d..0db436876 100644 --- a/src/clear/expression/nodes/between.cr +++ b/src/clear/expression/nodes/between.cr @@ -8,7 +8,7 @@ class Clear::Expression::Node::Between < Clear::Expression::Node def initialize(@target : Node, @starts : BetweenType, @ends : BetweenType) end - def resolve + def resolve : String {"(", @target.resolve, " BETWEEN ", diff --git a/src/clear/expression/nodes/double_operator.cr b/src/clear/expression/nodes/double_operator.cr index d4f06159d..7a963b08a 100644 --- a/src/clear/expression/nodes/double_operator.cr +++ b/src/clear/expression/nodes/double_operator.cr @@ -5,7 +5,7 @@ require "./node" class Clear::Expression::Node::DoubleOperator < Clear::Expression::Node def initialize(@a : Node, @b : Node, @op : String); end - def resolve + def resolve : String {"(", @a.resolve, " ", @op, " ", @b.resolve, ")"}.join end end diff --git a/src/clear/expression/nodes/function.cr b/src/clear/expression/nodes/function.cr index 17c2afa40..c90e489e0 100644 --- a/src/clear/expression/nodes/function.cr +++ b/src/clear/expression/nodes/function.cr @@ -4,7 +4,7 @@ require "./node" class Clear::Expression::Node::Function < Clear::Expression::Node def initialize(@name : String, @args : Array(String)); end - def resolve + def resolve : String {@name, "(", @args.join(", "), ")"}.join end end diff --git a/src/clear/expression/nodes/in_array.cr b/src/clear/expression/nodes/in_array.cr index 036031775..c17141305 100644 --- a/src/clear/expression/nodes/in_array.cr +++ b/src/clear/expression/nodes/in_array.cr @@ -6,7 +6,7 @@ require "./node" class Clear::Expression::Node::InArray < Clear::Expression::Node def initialize(@target : Node, @array : Array(String)); end - def resolve + def resolve : String if @array.size == 0 "FALSE" # If array is empty, return "FALSE" expression else diff --git a/src/clear/expression/nodes/in_range.cr b/src/clear/expression/nodes/in_range.cr index b4f62c976..43f9a285a 100644 --- a/src/clear/expression/nodes/in_range.cr +++ b/src/clear/expression/nodes/in_range.cr @@ -19,7 +19,7 @@ require "./node" class Clear::Expression::Node::InRange < Clear::Expression::Node def initialize(@target : Node, @range : Range(String, String), @exclusive = false); end - def resolve + def resolve : String rt = @target.resolve final_op = @exclusive ? " < " : " <= " diff --git a/src/clear/extensions/time/interval.cr b/src/clear/extensions/time/interval.cr index 360a22cc2..39b531c2f 100644 --- a/src/clear/extensions/time/interval.cr +++ b/src/clear/extensions/time/interval.cr @@ -5,7 +5,7 @@ struct Clear::Interval getter months : Int32 = 0 def initialize(span : Time::Span ) - @microseconds = span.total_nanoseconds.to_i64 / 1_000 + @microseconds = span.total_nanoseconds.to_i64 // 1_000 end def initialize(span : Time::MonthSpan) diff --git a/src/clear/extensions/time/time_in_day.cr b/src/clear/extensions/time/time_in_day.cr index 9a96c2fd3..dcbbbd782 100644 --- a/src/clear/extensions/time/time_in_day.cr +++ b/src/clear/extensions/time/time_in_day.cr @@ -37,15 +37,15 @@ struct Clear::TimeInDay @microseconds = (SECOND * seconds) + (MINUTE * minutes) + (HOUR * hours) end - def initialize(@microseconds = 0) + def initialize(@microseconds : UInt64 = 0) end def +(t : Time::Span) - Clear::TimeInDay.new(microseconds: @microseconds + t.total_nanoseconds.to_i64 / 1_000) + Clear::TimeInDay.new(microseconds: @microseconds + t.total_nanoseconds.to_i64 // 1_000) end def -(t : Time::Span) - Clear::TimeInDay.new(microseconds: @microseconds - t.total_nanoseconds.to_i64 / 1_000) + Clear::TimeInDay.new(microseconds: @microseconds - t.total_nanoseconds.to_i64 // 1_000) end def +(x : self) @@ -65,7 +65,7 @@ struct Clear::TimeInDay end def total_seconds - @microseconds / SECOND + @microseconds // SECOND end def to_tuple diff --git a/src/clear/sql/connection_pool.cr b/src/clear/sql/connection_pool.cr index 3103015fa..3db3161f0 100644 --- a/src/clear/sql/connection_pool.cr +++ b/src/clear/sql/connection_pool.cr @@ -1,5 +1,5 @@ class Clear::SQL::ConnectionPool - @@connections = {} of String => Channel::Buffered(DB::Database) + @@connections = {} of String => Channel(DB::Database) @@fiber_connections = {} of {String, Fiber} => { DB::Database, Int32 } diff --git a/src/clear/version.cr b/src/clear/version.cr index e6c5999fb..afe7dd1e3 100644 --- a/src/clear/version.cr +++ b/src/clear/version.cr @@ -1,3 +1,3 @@ module Clear - VERSION = "v0.7" + VERSION = "v0.7.1" end From ca2f1edcff3118a3aebaebc4db887b56139fa337 Mon Sep 17 00:00:00 2001 From: Yacine Petitprez Date: Wed, 25 Sep 2019 12:15:49 +0700 Subject: [PATCH 2/5] Cleaning deprecation warnings --- Makefile | 3 +- manual/CHANGELOG.md | 6 ++-- manual/model/lifecycle/callbacks.md | 2 +- .../filter-the-query-1/filter-the-query.md | 6 ++-- manual_old/model/Hooks.md | 4 +-- shard.lock | 4 +-- shard.yml | 2 +- spec/extensions/bcrypt_spec.cr | 8 ++--- spec/extensions/time_spec.cr | 4 +-- spec/model/model_spec.cr | 2 +- spec/sql/select_spec.cr | 2 +- src/clear/cli/generators/migration.cr | 2 +- src/clear/cli/generators/model.cr | 2 +- src/clear/expression/expression.cr | 12 ++++---- src/clear/expression/nodes/in_select.cr | 2 +- src/clear/expression/nodes/minus.cr | 2 +- src/clear/expression/nodes/not.cr | 2 +- src/clear/expression/nodes/not_between.cr | 2 +- src/clear/expression/nodes/null.cr | 2 +- src/clear/expression/nodes/pg_array.cr | 2 +- src/clear/expression/nodes/raw.cr | 2 +- src/clear/expression/nodes/variable.cr | 2 +- src/clear/extensions/enum/migration.cr | 8 ++--- .../full_text_searchable/migration.cr | 4 +-- src/clear/extensions/jsonb/expression.cr | 4 +-- src/clear/extensions/time/time_in_day.cr | 2 +- src/clear/migration/migration.cr | 4 ++- src/clear/migration/operation/columns.cr | 12 ++++---- src/clear/migration/operation/execute.cr | 18 ++++-------- src/clear/migration/operation/indexes.cr | 4 +-- src/clear/migration/operation/table.cr | 29 ++++++++++++------- src/clear/model/modules/has_timestamps.cr | 4 +-- src/clear/sql/query/fetch.cr | 2 +- .../kemal/src/views/components/footer.ecr | 2 +- 34 files changed, 85 insertions(+), 83 deletions(-) diff --git a/Makefile b/Makefile index 345440f31..5acfa0fba 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ CRYSTAL_BIN ?= $(shell which crystal) test: - $(CRYSTAL_BIN) spec -Dquiet \ No newline at end of file + $(CRYSTAL_BIN) spec -Dquiet --warnings=all + diff --git a/manual/CHANGELOG.md b/manual/CHANGELOG.md index 637654dd8..f511d2f5e 100644 --- a/manual/CHANGELOG.md +++ b/manual/CHANGELOG.md @@ -28,7 +28,7 @@ class MyModel column i : Clear::Interval end -puts "Expected time: #{Time.now + MyModel.first!.i}" +puts "Expected time: #{Time.local + MyModel.first!.i}" ``` - Add `Clear::TimeInDay` columns type, which stands for the `time` object in PostgreSQL. @@ -42,7 +42,7 @@ time = Clear::TimeInDay.parse("12:33") puts time.hour # 12 puts time.minutes # 0 -Time.now.at(time) # Today at 12:33:00 +Time.local.at(time) # Today at 12:33:00 time.to_s # 12:33:00 time.to_s(false) # don't show seconds => 12:33 @@ -252,7 +252,7 @@ Basically a transition version, to support Crystal 0.27. Some of the features of updating the documentation ! - Add range support for `Sql::Node#in?` method: ```crystal - last_week_users = User.query.where{ created_at.in?(7.day.ago..Time.now) }.count + last_week_users = User.query.where{ created_at.in?(7.day.ago..Time.local) }.count ``` - Refactoring of the nodes and clause of the SQL builder, avoiding array instantiation (usage of tuple instead) and starting to use Node as much as possible to build clauses. - Add `Clear::Expression.unsafe` which does exactly what it says: diff --git a/manual/model/lifecycle/callbacks.md b/manual/model/lifecycle/callbacks.md index e734a2856..f635598ee 100644 --- a/manual/model/lifecycle/callbacks.md +++ b/manual/model/lifecycle/callbacks.md @@ -17,7 +17,7 @@ class User after :create, :send_email - before(:update) { |m| m.as(User).updated_at = Time.now } + before(:update) { |m| m.as(User).updated_at = Time.local } def send_email EmailManager.send_email(subject: "welcome #{full_name} !", body: "...") diff --git a/manual/querying/the-collection-object/filter-the-query-1/filter-the-query.md b/manual/querying/the-collection-object/filter-the-query-1/filter-the-query.md index 9e417fb81..0ac74303a 100644 --- a/manual/querying/the-collection-object/filter-the-query-1/filter-the-query.md +++ b/manual/querying/the-collection-object/filter-the-query-1/filter-the-query.md @@ -1,7 +1,7 @@ # Filter the query – The Expression Engine -Because Collection represents SQL SELECT query, they offer way to filter the query. -Clear offer the Expression Engine, which is inspired by Sequel. +Because Collection represents SQL SELECT query, they offer way to filter the query. +Clear offer the Expression Engine, which is inspired by Sequel. It basically helps you to write complex filter conditions without sacrificing on code expressiveness. ## The where clause @@ -91,7 +91,7 @@ Expression engine manage natively range, array and other methods as see below. Range: ```ruby -User.query.where{ created_at.in?(5.days.from_now .. Time.now) } # WHERE created_at > ... AND created_at < ... +User.query.where{ created_at.in?(5.days.from_now .. Time.local) } # WHERE created_at > ... AND created_at < ... ``` #### Array / Tuples: diff --git a/manual_old/model/Hooks.md b/manual_old/model/Hooks.md index 6ad1947ea..4db26e6da 100644 --- a/manual_old/model/Hooks.md +++ b/manual_old/model/Hooks.md @@ -29,7 +29,7 @@ before(:validate) do |model| model = model.as(self) unless model.persisted? - now = Time.now + now = Time.local model.created_at = now model.updated_at = now end @@ -41,7 +41,7 @@ after(:validate) do |model| # In the case the updated_at has been changed, we do not override. # It happens on first insert, in the before validation setup. if model.changed? && !model.updated_at_column.changed? - model.updated_at = Time.now + model.updated_at = Time.local end end ``` diff --git a/shard.lock b/shard.lock index 58e7b48af..3f7ec3ac6 100644 --- a/shard.lock +++ b/shard.lock @@ -10,7 +10,7 @@ shards: db: github: crystal-lang/crystal-db - version: 0.6.0 + version: 0.7.0 generate: github: anykeyh/generate.cr @@ -22,5 +22,5 @@ shards: pg: github: will/crystal-pg - version: 0.18.1 + version: 0.19.0 diff --git a/shard.yml b/shard.yml index c867c2f9f..4eb0beb0e 100644 --- a/shard.yml +++ b/shard.yml @@ -18,7 +18,7 @@ dependencies: branch: master pg: github: will/crystal-pg - version: "~> 0.17" + version: "~> 0.19" inflector: github: phoffer/inflector.cr version: "~> 0.1.8" diff --git a/spec/extensions/bcrypt_spec.cr b/spec/extensions/bcrypt_spec.cr index bef6f2e1a..9cf4b8f16 100644 --- a/spec/extensions/bcrypt_spec.cr +++ b/spec/extensions/bcrypt_spec.cr @@ -37,16 +37,16 @@ module BCryptSpec User.create!({encrypted_password: Crypto::Bcrypt::Password.create("abcd")}) User.query.count.should eq 1 - User.query.first!.encrypted_password.should eq "abcd" - User.query.first!.encrypted_password.should_not eq "abce" + User.query.first!.encrypted_password.verify("abcd").should be_true + User.query.first!.encrypted_password.verify("abce").should be_false usr = User.query.first! usr.encrypted_password = Crypto::Bcrypt::Password.create("lorem.ipsum") usr.save! - User.query.first!.encrypted_password.should_not eq "abcd" - User.query.first!.encrypted_password.should eq "lorem.ipsum" + User.query.first!.encrypted_password.verify("abcd").should be_false + User.query.first!.encrypted_password.verify("lorem.ipsum").should be_true end end end diff --git a/spec/extensions/time_spec.cr b/spec/extensions/time_spec.cr index c00ebdc28..02b636a02 100644 --- a/spec/extensions/time_spec.cr +++ b/spec/extensions/time_spec.cr @@ -57,14 +57,14 @@ module IntervalSpec # TimeSpan [1.hour, 1.day, 1.month].each do |span| i = Clear::Interval.new(span) - now = Time.now + now = Time.local (now + i).to_unix.should eq( (now+span).to_unix) (now - i).to_unix.should eq( (now-span).to_unix ) end i = Clear::Interval.new(months: 1, days: -1, minutes: 12) - now = Time.now + now = Time.local (now + i).to_unix.should eq( (now+1.month-1.day+12.minute).to_unix) (now - i).to_unix.should eq( (now-1.month+1.day-12.minute).to_unix) diff --git a/spec/model/model_spec.cr b/spec/model/model_spec.cr index 1e2fa75d1..0eed5d3c2 100644 --- a/spec/model/model_spec.cr +++ b/spec/model/model_spec.cr @@ -456,7 +456,7 @@ module ModelSpec end it "works on date fields with different timezone" do - now = Time.now + now = Time.local temporary do reinit diff --git a/spec/sql/select_spec.cr b/spec/sql/select_spec.cr index e613f23e8..37f678858 100644 --- a/spec/sql/select_spec.cr +++ b/spec/sql/select_spec.cr @@ -250,7 +250,7 @@ module SelectSpec end it "can stack with `AND` operator" do - now = Time.now + now = Time.local r = select_request.from(:users).where { users.id == nil }.where { var("users", "updated_at") >= now } diff --git a/src/clear/cli/generators/migration.cr b/src/clear/cli/generators/migration.cr index e324cfa6c..e8cc72c27 100644 --- a/src/clear/cli/generators/migration.cr +++ b/src/clear/cli/generators/migration.cr @@ -25,7 +25,7 @@ class Clear::CLI::Generator if name name_underscore = name.underscore class_name = name.camelcase - migration_uid = Time.now.to_unix.to_s.rjust(10, '0') + migration_uid = Time.local.to_unix.to_s.rjust(10, '0') g["migration_uid"] = migration_uid g["class_name"] = class_name diff --git a/src/clear/cli/generators/model.cr b/src/clear/cli/generators/model.cr index ec99a0998..fa34e3e15 100644 --- a/src/clear/cli/generators/model.cr +++ b/src/clear/cli/generators/model.cr @@ -28,7 +28,7 @@ class Clear::CLI::Generator class_name = name.camelcase fields = @argv.join("|") - migration_uid = Time.now.to_unix.to_s.rjust(10, '0') + migration_uid = Time.local.to_unix.to_s.rjust(10, '0') g["model_class"] = class_name g["migration_uid"] = migration_uid diff --git a/src/clear/expression/expression.cr b/src/clear/expression/expression.cr index eddcecffd..11acf5383 100644 --- a/src/clear/expression/expression.cr +++ b/src/clear/expression/expression.cr @@ -9,20 +9,20 @@ # Instead of writing: # # ``` -# model_collection.where("created_at BETWEEN ? AND ?", 1.day.ago, DateTime.now) +# model_collection.where("created_at BETWEEN ? AND ?", 1.day.ago, DateTime.local) # ``` # # You can write: # ``` -# model_collection.where { created_at.between(1.day.ago, DateTime.now) } +# model_collection.where { created_at.between(1.day.ago, DateTime.local) } # ``` # # or even: # ``` -# model_collection.where { created_at.in?(1.day.ago..DateTime.now) } +# model_collection.where { created_at.in?(1.day.ago..DateTime.local) } # ``` # -# (Note for the later, it will generate `created_at > 1.day.ago AND created_at < DateTime.now`) +# (Note for the later, it will generate `created_at > 1.day.ago AND created_at < DateTime.local`) # # ## Limitations # @@ -140,8 +140,8 @@ class Clear::Expression # # ## Example # ``` - # Clear::Expression[Time.now] # < "2017-04-03 23:04:43.234 +08:00" - # Clear::Expression[Time.now, date: true] # < "2017-04-03" + # Clear::Expression[Time.local] # < "2017-04-03 23:04:43.234 +08:00" + # Clear::Expression[Time.local, date: true] # < "2017-04-03" # ``` def self.safe_literal(x : Time, date : Bool = false) : String {"'", x.to_utc.to_s(date ? DATABASE_DATE_FORMAT : DATABASE_DATE_TIME_FORMAT), "'"}.join diff --git a/src/clear/expression/nodes/in_select.cr b/src/clear/expression/nodes/in_select.cr index 89462dd7e..3ae84f778 100644 --- a/src/clear/expression/nodes/in_select.cr +++ b/src/clear/expression/nodes/in_select.cr @@ -4,7 +4,7 @@ require "./node" class Clear::Expression::Node::InSelect < Clear::Expression::Node def initialize(@target : Node, @select : Clear::SQL::SelectBuilder); end - def resolve + def resolve : String {@target.resolve, " IN (", @select.to_sql, ")"}.join end end diff --git a/src/clear/expression/nodes/minus.cr b/src/clear/expression/nodes/minus.cr index 7119f8d20..5fdfb8ec2 100644 --- a/src/clear/expression/nodes/minus.cr +++ b/src/clear/expression/nodes/minus.cr @@ -4,7 +4,7 @@ require "./node" class Clear::Expression::Node::Minus < Clear::Expression::Node def initialize(@a : Node); end - def resolve + def resolve : String {"-", @a.resolve}.join end end diff --git a/src/clear/expression/nodes/not.cr b/src/clear/expression/nodes/not.cr index 29fe8bd95..3b17fb7ba 100644 --- a/src/clear/expression/nodes/not.cr +++ b/src/clear/expression/nodes/not.cr @@ -4,7 +4,7 @@ require "./node" class Clear::Expression::Node::Not < Clear::Expression::Node def initialize(@a : Node); end - def resolve + def resolve : String {"NOT ", @a.resolve}.join end end diff --git a/src/clear/expression/nodes/not_between.cr b/src/clear/expression/nodes/not_between.cr index e43aceaf8..85b9049ac 100644 --- a/src/clear/expression/nodes/not_between.cr +++ b/src/clear/expression/nodes/not_between.cr @@ -8,7 +8,7 @@ class Clear::Expression::Node::NotBetween < Clear::Expression::Node def initialize(@target : Node, @starts : BetweenType, @ends : BetweenType) end - def resolve + def resolve : String { "(", @target.resolve, diff --git a/src/clear/expression/nodes/null.cr b/src/clear/expression/nodes/null.cr index 03d2c3d7b..ef034281c 100644 --- a/src/clear/expression/nodes/null.cr +++ b/src/clear/expression/nodes/null.cr @@ -5,7 +5,7 @@ class Clear::Expression::Node::Null < Clear::Expression::Node def initialize end - def resolve + def resolve : String "NULL" end end diff --git a/src/clear/expression/nodes/pg_array.cr b/src/clear/expression/nodes/pg_array.cr index 0f5eb26aa..51b9d5b45 100644 --- a/src/clear/expression/nodes/pg_array.cr +++ b/src/clear/expression/nodes/pg_array.cr @@ -8,7 +8,7 @@ class Clear::Expression::Node::PGArray(T) < Clear::Expression::Node def initialize(@arr : Array(T)) end - def resolve + def resolve : String {"array[", Clear::Expression[@arr].join(", "), "]"}.join end end diff --git a/src/clear/expression/nodes/raw.cr b/src/clear/expression/nodes/raw.cr index bc298ab40..cffd3581f 100644 --- a/src/clear/expression/nodes/raw.cr +++ b/src/clear/expression/nodes/raw.cr @@ -4,7 +4,7 @@ require "./node" class Clear::Expression::Node::Raw < Clear::Expression::Node def initialize(@raw : String); end - def resolve + def resolve : String @raw end end diff --git a/src/clear/expression/nodes/variable.cr b/src/clear/expression/nodes/variable.cr index e25f11b30..27f200f61 100644 --- a/src/clear/expression/nodes/variable.cr +++ b/src/clear/expression/nodes/variable.cr @@ -26,7 +26,7 @@ class Clear::Expression::Node::Variable < Clear::Expression::Node {% end %} end - def resolve + def resolve : String parent = @parent if parent {parent.resolve, ".", Clear::SQL.escape(@name)}.join diff --git a/src/clear/extensions/enum/migration.cr b/src/clear/extensions/enum/migration.cr index 0f11207e0..07014255e 100644 --- a/src/clear/extensions/enum/migration.cr +++ b/src/clear/extensions/enum/migration.cr @@ -7,11 +7,11 @@ module Clear::Migration def initialize(@name, @values) end - def up + def up : Array(String) ["CREATE TYPE #{@name} AS ENUM (#{Clear::Expression[@values].join(", ")})"] end - def down + def down : Array(String) ["DROP TYPE #{@name}"] end end @@ -23,11 +23,11 @@ module Clear::Migration def initialize(@name, @values) end - def up + def up : Array(String) ["DROP TYPE #{@name}"] end - def down + def down : Array(String) if values = @values ["CREATE TYPE #{@name} AS ENUM (#{Clear::Expression[values].join(", ")})"] else diff --git a/src/clear/extensions/full_text_searchable/migration.cr b/src/clear/extensions/full_text_searchable/migration.cr index 22bfa0df5..095340015 100644 --- a/src/clear/extensions/full_text_searchable/migration.cr +++ b/src/clear/extensions/full_text_searchable/migration.cr @@ -72,11 +72,11 @@ class Clear::Migration::FullTextSearchableOperation < Clear::Migration::Operatio ["DROP FUNCTION #{function_name}()", "DROP TRIGGER #{trigger_name}"] end - def up + def up : Array(String) print_trigger + print_update_current_data end - def down + def down : Array(String) print_delete_trigger end end diff --git a/src/clear/extensions/jsonb/expression.cr b/src/clear/extensions/jsonb/expression.cr index d0f4fa2bf..68015ba1f 100644 --- a/src/clear/extensions/jsonb/expression.cr +++ b/src/clear/extensions/jsonb/expression.cr @@ -10,7 +10,7 @@ class Clear::Expression::Node::JSONB::Field < Clear::Expression::Node def initialize(@field, @key, @cast = nil) end - def resolve + def resolve : String jsonb_resolve(@field.resolve, jsonb_k2a(key), @cast) end @@ -41,7 +41,7 @@ class Clear::Expression::Node::JSONB::Equality < Clear::Expression::Node def initialize(@jsonb_field, @value) end - def resolve + def resolve : String {@jsonb_field, Clear::Expression[@value.to_json]}.join(" @> ") end end diff --git a/src/clear/extensions/time/time_in_day.cr b/src/clear/extensions/time/time_in_day.cr index dcbbbd782..62102ef39 100644 --- a/src/clear/extensions/time/time_in_day.cr +++ b/src/clear/extensions/time/time_in_day.cr @@ -10,7 +10,7 @@ # puts time.hour # 12 # puts time.minutes # 0 # -# Time.now.at(time) # Today at 12:33:00 +# Time.local.at(time) # Today at 12:33:00 # time.to_s # 12:33:00 # time.to_s(false) # don't show seconds => 12:33 # diff --git a/src/clear/migration/migration.cr b/src/clear/migration/migration.cr index a8576ad41..be75238d3 100644 --- a/src/clear/migration/migration.cr +++ b/src/clear/migration/migration.cr @@ -162,9 +162,11 @@ end # and will be removed when the bug is fixed class DummyMigration include Clear::Migration - def uid + + def uid : Int64 -0x01_i64 end + def change(dir) # Nothing end diff --git a/src/clear/migration/operation/columns.cr b/src/clear/migration/operation/columns.cr index 52dc8b959..70a3da805 100644 --- a/src/clear/migration/operation/columns.cr +++ b/src/clear/migration/operation/columns.cr @@ -7,11 +7,11 @@ module Clear::Migration def initialize(@table, @column, @datatype) end - def up + def up : Array(String) ["ALTER TABLE #{@table} ADD #{@column} #{@datatype}"] end - def down + def down : Array(String) ["ALTER TABLE #{@table} DROP #{@column}"] end end @@ -24,11 +24,11 @@ module Clear::Migration def initialize(@table, @column, @datatype = nil) end - def up + def up : Array(String) ["ALTER TABLE #{@table} DROP #{@column}"] end - def down + def down : Array(String) raise IrreversibleMigration.new( "Cannot revert column drop, because datatype is unknown" ) if @datatype.nil? @@ -51,7 +51,7 @@ module Clear::Migration @new_column_type ||= @column_type end - def up + def up : Array(String) o = [] of String if @old_column_name && @new_column_name && @old_column_name != @new_column_name o << "ALTER TABLE #{@table} RENAME COLUMN #{@old_column_name} TO #{@new_column_name};" @@ -64,7 +64,7 @@ module Clear::Migration o end - def down + def down : Array(String) o = [] of String if @old_column_name && @new_column_name && @old_column_name != @new_column_name o << "ALTER TABLE #{@table} RENAME COLUMN #{@new_column_name} TO #{@old_column_name};" diff --git a/src/clear/migration/operation/execute.cr b/src/clear/migration/operation/execute.cr index 258997acf..427559b59 100644 --- a/src/clear/migration/operation/execute.cr +++ b/src/clear/migration/operation/execute.cr @@ -9,21 +9,13 @@ module Clear::Migration def initialize(@up = nil, @down = nil, @irreversible = false) end - def up - if @up - [@up] - else - [] of String - end + def up : Array(String) + [@up].compact end - def down - if @down - [@down] - else - irreversible! if @irreversible - [] of String - end + def down : Array(String) + irreversible! if @irreversible && @down.nil? + [@down].compact end end end diff --git a/src/clear/migration/operation/indexes.cr b/src/clear/migration/operation/indexes.cr index 4200e2fc9..11e1458e5 100644 --- a/src/clear/migration/operation/indexes.cr +++ b/src/clear/migration/operation/indexes.cr @@ -31,11 +31,11 @@ module Clear::Migration {"(", @fields.join(", "), ")"}.join end - def up + def up : Array(String) [["CREATE", print_unique, "INDEX", safe_name(@name), "ON", @table, print_using, print_columns].compact.join(" ")] end - def down + def down : Array(String) # Using of IF EXISTS in the case we have a migration with # column created followed by index. Dropping of the column # will cascade the deletion of the index, therefor the migration will diff --git a/src/clear/migration/operation/table.cr b/src/clear/migration/operation/table.cr index e86f47634..a09e43e3a 100644 --- a/src/clear/migration/operation/table.cr +++ b/src/clear/migration/operation/table.cr @@ -89,20 +89,27 @@ module Clear::Migration str.underscore.gsub(/[^a-zA-Z0-9_]/, "_").gsub(/_+/, "_") end - def up + def up : Array(String) columns_and_fkeys = print_columns + print_fkeys content = "(#{columns_and_fkeys.join(", ")})" unless columns_and_fkeys.empty? - [ - (["CREATE TABLE", @name, content].reject(&.nil?).join(" ") if is_create?), - ] + print_indexes + arr = if is_create? + [ + ["CREATE TABLE", @name, content].compact.join(" "), + ] + else + # To implement later + [] of String + end + + arr + print_indexes end - def down + def down : Array(String) [ - (["DROP TABLE", @name].join(" ") if is_create?), - ] + (["DROP TABLE", @name].join(" ") if is_create?) + ].compact end private def print_fkeys @@ -181,11 +188,11 @@ module Clear::Migration end - def up + def up : Array(String) ["CREATE TABLE #{@table}"] end - def down + def down : Array(String) ["DROP TABLE #{@table}"] end end @@ -196,11 +203,11 @@ module Clear::Migration def initialize(@table) end - def up + def up : Array(String) ["DROP TABLE #{@table}"] end - def down + def down : Array(String) ["CREATE TABLE #{@table}"] end end diff --git a/src/clear/model/modules/has_timestamps.cr b/src/clear/model/modules/has_timestamps.cr index 1ffe963d0..f802f7df8 100644 --- a/src/clear/model/modules/has_timestamps.cr +++ b/src/clear/model/modules/has_timestamps.cr @@ -10,7 +10,7 @@ module Clear::Model::HasTimestamps model = model.as(self) unless model.persisted? - now = Time.now + now = Time.local model.created_at = now unless model.created_at_column.defined? model.updated_at = now unless model.updated_at_column.defined? end @@ -21,7 +21,7 @@ module Clear::Model::HasTimestamps # In the case the updated_at has been changed, we do not override. # It happens on first insert, in the before validation setup. - model.updated_at = Time.now if model.changed? && !model.updated_at_column.changed? + model.updated_at = Time.local if model.changed? && !model.updated_at_column.changed? end end diff --git a/src/clear/sql/query/fetch.cr b/src/clear/sql/query/fetch.cr index 7ed763f43..78d0ba136 100644 --- a/src/clear/sql/query/fetch.cr +++ b/src/clear/sql/query/fetch.cr @@ -25,7 +25,7 @@ module Clear::SQL::Query::Fetch trigger_before_query Clear::SQL.transaction do |cnx| - cursor_name = "__cursor_#{Time.now.to_unix ^ (rand * 0xfffffff).to_i}__" + cursor_name = "__cursor_#{Time.local.to_unix ^ (rand * 0xfffffff).to_i}__" cursor_declaration = "DECLARE #{cursor_name} CURSOR FOR #{to_sql}" diff --git a/templates/kemal/src/views/components/footer.ecr b/templates/kemal/src/views/components/footer.ecr index dee6bc524..aef2f67f7 100644 --- a/templates/kemal/src/views/components/footer.ecr +++ b/templates/kemal/src/views/components/footer.ecr @@ -1,6 +1,6 @@ module TreeTemplate::Components # This is just an example of component - def app_footer(content = "<%= Time.now.year %>") + def app_footer(content = "<%= Time.local.year %>") footer do p(style: "text-align: center") do # test From 4cc09d6f9dbb7eb908d65e9374c19586681842e9 Mon Sep 17 00:00:00 2001 From: Yacine Petitprez Date: Wed, 25 Sep 2019 12:16:51 +0700 Subject: [PATCH 3/5] change version number --- shard.yml | 2 +- src/clear/version.cr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shard.yml b/shard.yml index 4eb0beb0e..4d918e18d 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: clear -version: 0.7.1 +version: 0.7.2 authors: - Yacine Petitprez diff --git a/src/clear/version.cr b/src/clear/version.cr index afe7dd1e3..4d7034c40 100644 --- a/src/clear/version.cr +++ b/src/clear/version.cr @@ -1,3 +1,3 @@ module Clear - VERSION = "v0.7.1" + VERSION = "v0.7.2" end From 45ff0fb94276a568b4b867fcd2a5d569b51b70ce Mon Sep 17 00:00:00 2001 From: Yacine Petitprez Date: Wed, 25 Sep 2019 12:32:38 +0700 Subject: [PATCH 4/5] Fix crashing specs, still pg issue remaining --- shard.lock | 2 +- spec/model/cache_spec.cr | 83 ++++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 39 deletions(-) diff --git a/shard.lock b/shard.lock index 3f7ec3ac6..7c8b10b4e 100644 --- a/shard.lock +++ b/shard.lock @@ -6,7 +6,7 @@ shards: ameba: github: veelenga/ameba - commit: 8843235f5faeeb97b2a834a60825e05ca5798e86 + commit: 8a27a36c49ee45954680931dc7ea457bd08aa2ec db: github: crystal-lang/crystal-db diff --git a/spec/model/cache_spec.cr b/spec/model/cache_spec.cr index 8b868e723..be2553785 100644 --- a/spec/model/cache_spec.cr +++ b/spec/model/cache_spec.cr @@ -1,25 +1,29 @@ require "../spec_helper" require "./cache_schema" + module CacheSpec - describe "Clear::Model" do - temporary do - Clear::Migration::Manager.instance.reinit! - MigrateSpec10.new.apply(Clear::Migration::Direction::UP) + def self.reinit + Clear::Migration::Manager.instance.reinit! + MigrateSpec10.new.apply(Clear::Migration::Direction::UP) - User.create [{id: 101, name: "User 1"}] - User.create [{id: 102, name: "User 2"}] + User.create [{id: 101, name: "User 1"}] + User.create [{id: 102, name: "User 2"}] - Category.create [{id: 201, name: "Test"}] - Category.create [{id: 202, name: "Test 2"}] + Category.create [{id: 201, name: "Test"}] + Category.create [{id: 202, name: "Test 2"}] - Post.create [{id: 301, published: true, user_id: 101, category_id: 201, content: "Lorem ipsum"}] - Post.create [{id: 302, published: true, user_id: 102, category_id: 201, content: "Lorem ipsum"}] - Post.create [{id: 303, published: true, user_id: 102, category_id: 202, content: "Lorem ipsum"}] - Post.create [{id: 304, published: true, user_id: 101, category_id: 202, content: "Lorem ipsum"}] + Post.create [{id: 301, published: true, user_id: 101, category_id: 201, content: "Lorem ipsum"}] + Post.create [{id: 302, published: true, user_id: 102, category_id: 201, content: "Lorem ipsum"}] + Post.create [{id: 303, published: true, user_id: 102, category_id: 202, content: "Lorem ipsum"}] + Post.create [{id: 304, published: true, user_id: 101, category_id: 202, content: "Lorem ipsum"}] + end - context "cache system" do - it "manage has_many relations" do + describe "Clear::Model" do + context "cache system" do + it "manage has_many relations" do + temporary do + reinit Clear::Model::QueryCache.reset_counter # relations has_many User.query.first!.posts.count.should eq(2) @@ -29,41 +33,44 @@ module CacheSpec Clear::Model::QueryCache.cache_hitted.should eq(1) end end + end - it "can be chained" do - temporary do - Clear::Model::QueryCache.reset_counter - User.query.with_posts(&.with_category).each do |user| - user.posts.each do |p| - case p.id - when 301, 302 - (p.category.not_nil!.id == 201).should eq(true) - when 303, 304 - (p.category.not_nil!.id == 202).should eq(true) - end + it "can be chained" do + temporary do + reinit + Clear::Model::QueryCache.reset_counter + User.query.with_posts(&.with_category).each do |user| + user.posts.each do |p| + case p.id + when 301, 302 + (p.category.not_nil!.id == 201).should eq(true) + when 303, 304 + (p.category.not_nil!.id == 202).should eq(true) end end end end + end - it "can be called in chain on the through relations" do - temporary do - # Relation belongs_to - Clear::Model::QueryCache.reset_counter - Post.query.with_user.each do |post| - post.user.not_nil! - end - Clear::Model::QueryCache.cache_hitted.should eq(4) # Number of posts + it "can be called in chain on the through relations" do + temporary do + reinit + # Relation belongs_to + Clear::Model::QueryCache.reset_counter + Post.query.with_user.each do |post| + post.user.not_nil! + end + Clear::Model::QueryCache.cache_hitted.should eq(4) # Number of posts - Category.query.with_users { |q| q.order_by("users.id", "asc") }.order_by("id", "asc").each do |c| - c.users.each do |user| - c.id.not_nil! - user.id.not_nil! - end + Category.query.with_users { |q| q.order_by("users.id", "asc") }.order_by("id", "asc").each do |c| + c.users.each do |user| + c.id.not_nil! + user.id.not_nil! end end end end + end end end From 2e04a76c7a4719e2b3f05fee28c952b9170810e9 Mon Sep 17 00:00:00 2001 From: Yacine Petitprez Date: Thu, 3 Oct 2019 02:53:03 +0700 Subject: [PATCH 5/5] - Fix pg shard version & crystal target --- shard.lock | 2 +- shard.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/shard.lock b/shard.lock index 7c8b10b4e..b4cfe0924 100644 --- a/shard.lock +++ b/shard.lock @@ -22,5 +22,5 @@ shards: pg: github: will/crystal-pg - version: 0.19.0 + commit: cafe7b4de4f6e0b0ef620c96de2b9d03f0157347 diff --git a/shard.yml b/shard.yml index 4d918e18d..f83806deb 100644 --- a/shard.yml +++ b/shard.yml @@ -18,7 +18,7 @@ dependencies: branch: master pg: github: will/crystal-pg - version: "~> 0.19" + branch: master inflector: github: phoffer/inflector.cr version: "~> 0.1.8" @@ -30,6 +30,6 @@ development_dependencies: github: veelenga/ameba branch: master -crystal: 0.31 +crystal: 0.31.1 license: MIT