-
e.g. just like this: (although, following code actually not work)
|
Beta Was this translation helpful? Give feedback.
Answered by
jwoertink
May 1, 2024
Replies: 2 comments 1 reply
-
Not tested, but you can try this require "pg"
require "avram"
class AppDatabase < Avram::Database
end
AppDatabase.configure do |settings|
settings.credentials = Avram::Credentials.new(database: "test_db", username: "postgres", hostname: "localhost")
end
Avram.configure do |settings|
settings.database_to_migrate = AppDatabase
settings.lazy_load_enabled = true
end
class CreateFoo::V20240420041990 < Avram::Migrator::Migration::V1
def migrate
create table_for(Foo), if_not_exists: true do
primary_key id : Int64
add is_211 : Bool, index: true, default: false
end
end
def rollback
drop table_for(Foo)
end
end
Avram::Migrator::Runner.new.run_next_migration
class Foo < BaseModel
table do
column is_211 : Bool
end
end
Foo::SaveOperation.create!(is_211: true)
Foo::BaseQuery.new.is_211(true).select_count #=> 1 No need for |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
zw963
-
Thank you, following code is working. # NOTICE: It drop db, and recreate it, so, be careful to not loss your's data.
require "pg"
require "avram"
class AppDatabase < Avram::Database
end
AppDatabase.configure do |settings|
settings.credentials = Avram::Credentials.new(database: "test_db", username: "postgres", hostname: "localhost")
end
Avram.configure do |settings|
settings.database_to_migrate = AppDatabase
settings.lazy_load_enabled = true
end
class CreateFoo::V20240420041990 < Avram::Migrator::Migration::V1
def migrate
create table_for(Foo), if_not_exists: true do
primary_key id : Int64
add is_211 : Bool, index: true, default: false
add_timestamps
end
end
def rollback
drop table_for(Foo)
end
end
Avram::Migrator::Runner.drop_db(true)
Avram::Migrator::Runner.create_db(true)
Avram::Migrator::Runner.new.run_next_migration
abstract class BaseModel < Avram::Model
def self.database : Avram::Database.class
AppDatabase
end
end
class Foo < BaseModel
table do
column is_211 : Bool
end
end
Foo::SaveOperation.create!(is_211: true)
pp! Foo::BaseQuery.new.is_211(true).select_count # => 1 But, one concerns, we have to add
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not tested, but you can try this