Comprobar si existe una tabla en Rails


Tengo una tarea de rake que no funcionará a menos que exista una tabla. Estoy trabajando con más de 20 ingenieros en un sitio web, así que quiero asegurarme de que han migrado la tabla antes de que puedan hacer una tarea de rake que llenará esa tabla respectiva.

¿AR tiene un método como Table.exists? ¿Cómo puedo asegurarme de que han migrado la tabla con éxito?

Author: jackyalcine, 2011-07-06

4 answers

En Rails 5 la API se hizo explícita con respecto a tablas/vistas, colectivamente fuentes de datos.

# Tables and views
ActiveRecord::Base.connection.data_sources
ActiveRecord::Base.connection.data_source_exists? 'kittens'

# Tables
ActiveRecord::Base.connection.tables
ActiveRecord::Base.connection.table_exists? 'kittens'

# Views
ActiveRecord::Base.connection.views
ActiveRecord::Base.connection.view_exists? 'kittens'

En Rails 2, 3 y 4 la API es sobre tablas.

# Listing of all tables and views
ActiveRecord::Base.connection.tables

# Checks for existence of kittens table/view (Kitten model)
ActiveRecord::Base.connection.table_exists? 'kittens'

Obtener el estado de las migraciones:

# Tells you all migrations run
ActiveRecord::Migrator.get_all_versions

# Tells you the current schema version
ActiveRecord::Migrator.current_version

Si necesita más API para migraciones o metadatos, consulte:

 272
Author: captainpete,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-29 02:24:59

Incluso si la tabla no existe:

Modelo Kitten, cuadro esperado kittens carriles 3:

Gatito.tabla_exists? # = > false

 54
Author: alexey_the_cat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-31 14:48:58

Descubrí esto mientras intentaba eliminar una tabla a través de una migración:

drop_table :kittens if (table_exists? :kittens)
ActiveRecord::Migration.drop_table :kittens if (ActiveRecord::Base.connection.table_exists? :kittens)

Funciona para Rails 3.2

Esta forma más simple estará disponible en Rails 5:

drop_table :kittens, if_exists: true

Referencia: https://github.com/rails/rails/pull/16366

Y aquí está el REGISTRO de CAMBIOS de Rails 5 ActiveRecord :

Introduzca la opción :if_exists para drop_table.

Ejemplo:

drop_table(:posts, if_exists: true)

Que ejecutaría:

DROP TABLE IF EXISTS posts

Si el la tabla no existe, if_exists: false (el valor predeterminado) genera una excepción, mientras que if_exists: true no hace nada.

 28
Author: kangkyu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-09 10:39:25

Carriles 5.1

if ActiveRecord::Base.connection.data_source_exists? 'table_name'
   drop_table :table_name
end

O

drop_table :table_name, if_exists: true
 6
Author: Vitor Oliveira,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-16 18:14:20