You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In ORMS like Django you only define a class for the data type and it'll automatically create the migration and the table with the makemigrationand migratecommands. This certainly makes the workflow easier as the user only needs to modify one file. We could do something similar to avoid having to manually edit the migration file. I've taken a first step by creating the migration file from a struct
using Dates, SearchLight
functiongenerate_migration(migration_name, struct_list...)
# Convert Julia types to DB column typesfunctiontype_to_dbcolumn(t)
return t == String ?:string:
t == Float64 ?:float:
t == Int ?:int:error("Unsupported type: $t")
end# Generate migration for a single structfunctionmigration_for_struct(s, table_name)
field_names =fieldnames(s)
field_types =fieldtypes(s)
col_defs = [":$(field_names[i]) => :$(type_to_dbcolumn(field_types[i]))"for i in2:length(field_names)]
return""" function up() create_table(:$table_name) do [ pk() columns([$(join(col_defs, ",\n\t\t")) ]) ] end end function down() drop_table($table_name) end"""end# Process struct_list to separate structs from optional table names
structs_with_processed_names = []
@show struct_list
for item in struct_list
iftypeof(item) == Tuple{DataType, String}
s, name = item
push!(structs_with_processed_names, (s, name))
elseiftypeof(item) == DataType
s = item
push!(structs_with_processed_names, (s, lowercase(string(s))*"s"))
endend# Loop over each struct and generate the migration code
migrations = [migration_for_struct(s, table_name) for (s, table_name) in structs_with_processed_names]
# Bundle the generated migrations into a module
migration_code =""" module $migration_name import SearchLight.Migrations: create_table, column, columns, pk, add_index, drop_table, add_indices$(join(migrations, "\n\n")) end"""# Save to file
filename = SearchLight.Migration.migration_file_name(migration_name)
open(filename, "w") do file
write(file, migration_code)
endprintln("Migration saved to $filename")
return migration_code
end
Example usage
import SearchLight: AbstractModel, DbId
import Base:@kwdef@kwdefmutable struct RandomVector <:AbstractModel
id::DbId=DbId()
x::String=""
m::Float64=0.0
N::Int=0end# Using the function with a mix of explicit table names and just structsprintln(generate_migration("RandomVectors", RandomVector, (RandomVector, "my_table_name")))
The text was updated successfully, but these errors were encountered:
In ORMS like Django you only define a class for the data type and it'll automatically create the migration and the table with the
makemigration
andmigrate
commands. This certainly makes the workflow easier as the user only needs to modify one file. We could do something similar to avoid having to manually edit the migration file. I've taken a first step by creating the migration file from a structExample usage
The text was updated successfully, but these errors were encountered: