-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support citext column type #550
Comments
I can see how that could be useful, but I'm not sure if this is something we could support directly right now. There's a TON of things that postgres can do, and being able to be 1-1 with postgres while trying to maintain type-safety and all that is a pretty big undertaking. One of my big worries is that crystal itself doesn't have a Instead, I think we should put some focus into #497. This would allow you to roll your own custom type and then it would be up to you on how you handle it. Right now you can make a custom type, but it's just a bit of work. It would look something like this: class Citext
def blank?
false
end
module Lucky
alias ColumnType = String
include Avram::Type
def parse(value : String)
if !value.blank?
SuccessfulCast(Citext).new(parsed_value)
else
FailedCast.new
end
end
def to_db(value : String)
value
end
# Essentially just need to do what String does https://github.com/luckyframework/avram/blob/master/src/avram/charms/string_extensions.cr
# but change whatever needs to change for a citext column
class Criteria(T, V) < Avram::Criteria(T, V)
end
end
end
# only needed if you wanted to use `add field : Citext` in your migrations
# Otherwise you'd use raw SQL
module Avram::Migrator::Columns
class CitextColumn(T) < Base
@default : T | Nil = nil
def initialize(@name, @nilable, @default)
end
def column_type : String
"citext"
end
end
end Then in your model: class User < BaseModel
table do
column username : Citext
end
end What are your thoughts on that? |
Thank you very much for your comprehensive answer and code examples. Before going further in the discussion, I have several assumptions about the Lucky ecosystem which are maybe mistaken :
With these assumptions, it feels more coherent/efficient to solve lucky issues as upstream as possible. The original issue (luckyframework/authentic#9) which led me here is/will be present in all shards/projects that implement email as login. So if we create for example a
I trust you 💯 on this. Designing an ORM is way over my skills.
You're right, it doesn't only involve All I want to say : this kind of decision is technically way over my head, but I can't help thinking that not implementing a part of Citext support upstream in Avram is a missed opportunity. |
You're not far off. Rails is like "do things how we do them, and everything just magically works". Lucky is more like "we recommend you do things our way because it's the most type-safe; however, here's an escape hatch so you can do things your own way". So we do have a convention, but that convention is based on a specific configuration. One thing in rails is that if you break convention, then your code becomes 10x more complicated. We are currently doing the same thing, but I have a goal of not doing that. Breaking from the normal set shouldn't increase your dev time and complexity (like with this citext). I'll have to think about the # just thinking out loud here...
class Migration < V1
def change
create table_for(User) do
add username : String, modifier: CitexModifier
end
end
end
module CitexModifier
include Lucky::ColumnModifier
def column_type : String
"citext"
end
end Maybe you want |
It's tough doing that, but documentation and code examples help tremendously with that (I succeeded doing a Lucky API project with no database thanks to the wizard and lucky website source code). As for your I just need a non-raw SQL way to declare a Citext column as an immediate improvement. it would be great if there was an unsafe but generic way (à la class Migration < V1
def change
enable_extension "citext"
create table_for(User) do
add username : String, column_type: "citext"
end
end
end The
The more I think about it, implementing a novel Citext custom type could be avoided. Most of the outside db logic has to be implemented anyway. I like the discussion too :-) |
A problem with supporting citext right now is will/crystal-pg#43 |
This is blocked until will/crystal-pg#221 is merged and released |
Sometimes a case insensitive text column type might be useful : luckyframework/authentic#51
citext is exactly that.
I was thinking about a
CIString
or Citext crystal type to map a citext column, but maybe an option (case_sensitive: false
) could be better.The text was updated successfully, but these errors were encountered: