简体   繁体   English

使用ActiveRecord执行迁移文件而不使用Rails不起作用

[英]Execute migration files using ActiveRecord without rails doesn't work

I'm trying to execute migration files using gem 'active_record' in a local host app that doesn't use rails and use a sqlite database. 我正在尝试在不使用Rails也不使用sqlite数据库的本地主机应用中使用gem'active_record'执行迁移文件。

I have the following rake file: 我有以下rake文件:

require_relative './config/environments.rb'
require 'active_record'

task :default => :migrate

desc "executing migration"
task :migrate do   
    ActiveRecord::Migrator.migrate('/db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
end

inside the /db/migrate path i have a file called 001_create_users.rb with the following code: 在/ db / migrate路径中,我有一个名为001_create_users.rb的文件,其中包含以下代码:

require 'active_record'

class CreateUsers < ActiveRecord::Migration

    def self.up
        create_table: users do |u|
            u.string :username
            u.string :password_digest
        end
     end
end

but whe I execute rake on terminal the table users is not created on the database, just a table called schema_migrations is created. 但是当我在终端上执行rake时,并没有在数据库中创建表users,而是仅创建了一个名为schema_migrations的表。

On the ActiveRecord i get the following records: 在ActiveRecord上,我得到以下记录:

D, [2014-07-10T11:42:21.520400 #2042] DEBUG -- :   [1m[36m (7.2ms)[0m  [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
D, [2014-07-10T11:42:21.520842 #2042] DEBUG -- :   [1m[35m (0.1ms)[0m  select sqlite_version(*)
D, [2014-07-10T11:42:21.522101 #2042] DEBUG -- :   [1m[36m (1.0ms)[0m  [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m

What I need to do in order to create property my Users table? 我需要做些什么来创建用户表的属性?

Have you tried standalone_migrations gem? 您是否尝试过standalone_migrations gem? ( https://github.com/thuss/standalone-migrations ) https://github.com/thuss/standalone-migrations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM