#1766·loco

Rails 样式迁移

作者: pynixwang创建于 2026年5月13日更新于 2026年7月29日
标签enhancement

pub async fn create_table( m: &SchemaManager<'_>, table: &str, cols: &[Column], timestamps: bool, ) -> Result<(), DbErr> { let mut stmt = Table::create();

stmt.table(DynIden::new(table)).if_not_exists();

for col in cols {
    stmt.col(build_column_def(col));
}

if timestamps {
    stmt.col(build_column_def(&Column::new(
        "created_at",
        ColumnType::Timestamp,
        false,
        false,
        false,
    )));

    stmt.col(build_column_def(&Column::new(
        "updated_at",
        ColumnType::Timestamp,
        false,
        false,
        false,
    )));
}

m.create_table(stmt).await

}

pub async fn create_table_with_timestamps( m: &SchemaManager<'_>, table: &str, cols: &[Column], ) -> Result<(), DbErr> { create_table(m, table, cols, true).await }

fn build_column_def(col: &Column) -> ColumnDef { let mut def = ColumnDef::new(DynIden::new(col.name));

match col.ty {
    ColumnType::Integer => {
        def.integer();
    }
    ColumnType::BigInteger => {
        def.big_integer();
    }
    ColumnType::String => {
        def.string();
    }
    ColumnType::Text => {
        def.text();
    }
    ColumnType::Boolean => {
        def.boolean();
    }
    ColumnType::DateTime => {
        def.date_time();
    }
    ColumnType::Timestamp => {
        def.timestamp();
    }
    ColumnType::Uuid => {