How to drop a specific database using Mongoid? - ruby

Mongoid.default_session.database_names gives me an array of database names. I want to delete a specific database. How would I do that?
Mongoid.default_session.drop() always seems to drop the default database even if I override the current database using Mongoid.override_database("test_database")
What am I missing?

In Mongoid v2.0.2
Mongoid.purge!
Rdoc: Mongoid.purge!

Have you tried a combination of Session.use and Session.drop?
Examples: http://mongoid.org/en/moped/docs/driver.html

Related

How to delete the ContentType and related db tables in Strapi?

I am using Strapi v3.0.0-beta.18.7
How to delete the ContentType and the related tables from DB ?
I tried the command below, but it is not deleting the db tables.
DELETE http://localhost:1337/content-type-builder/content-types/application::CONTENT_TYPE_NAME.CONTENT_TYPE_NAME
To delete the content-type and related db-tables in Strapi,
You can delete the folder inside /api folder having same name as your content-type
Suppose if you want to delete the "product" content type, you can delete the product folder inside of /api
The database's tables sync is not managed in the Content Type Builder plugin.
By default, Strapi doesn't delete anything from your database structure.
Strapi is customizable but you will not be able to update this.
Here is an issue that talks about this topic - https://github.com/strapi/strapi/issues/1114
The answers above are really helpful but don't explain, how you would actually go about deleting the table manually.
Suppose you run a local default installation with sqlite, you can find your database at .tmp/data.db. To connect to it, you will need a tool that you can get from sqlite directly:
https://sqlite.org/download.html
I guess you can add it to the PATH, but since I am a beginner and I just wanted it to work, i put the sqlite3.exe directly in the folder of the database and ran it.
To open the database, I used .open data.db and tested it with .tables which showed me all the tables that strapi created for me but didn't delete.
To ensure that I found the right table (recipe-cuisine) i looked at the content using .headers on followed by SELECT * FROM "recipe-cuisine";.
I finally deleted the whole table using DROP TABLE "recipe-cuisine";.
There is an awesome documentation on how to do other operations here: https://www.sqlitetutorial.net/
I hope that helps other beginners who struggle to delete the tables. If anybody has suggestions or helpful links with more information, that would be great!
Lets assume you need remove abc collection.
WARNING
Be sure you created backup and there are not other collections that contains abc substring.
Then you need execute commands:
DELETE FROM `users-permissions_permission` WHERE `controller` LIKE '%abc%';
DELETE FROM strapi_permission WHERE `subject` LIKE '%abc%';
DELETE FROM core_store WHERE `key` LIKE '%abc%';
DELETE FROM upload_file_morph WHERE related_type LIKE '%abc%';
DROP TABLE abc;
Then you need execute also:
rm -rf api/abc
Additional notes:
take care about plural names
be sure that there are no relations with other tables in other case you will see error
TypeError: Cannot read property 'globalId' of undefined

How to do in Ruby on Jets if a new column is added to database table

We need to alter table by adding a new column, in Ruby on Jets, what should be done? on database side, we can add column manually. how about Ruby on Jets files?
Thanks
George
First of all, you should mention your database Name as
If you are using
Dynamodb
You must go through https://rubyonjets.com/docs/database/dynamodb/
or here you will find all the steps to follow for dynomoDb https://www.twilio.com/blog/serverless-ruby-on-aws-lambda-with-the-jets-framework
If You are using MongoDB
You must go through
https://www.youtube.com/watch?v=VwUu1_jMNUQ
https://docs.mongodb.com/mongoid/master/tutorials/getting-started-rails/#create-new-application
https://mongoid.github.io/old/en/mongoid/docs/querying.html
https://mongoid.github.io/old/en/mongoid/docs/querying.html#query_plus
If you are using MySQL you just need to use normal migrations.
Hope this would help you to catch the nearby solutions.
Good Luck!
You should use database migrations: https://rubyonjets.com/docs/database/activerecord/

Postgres ActiveRecord limit table size to one row

I am creating a custom CMS using Sinatra and Postgres with sinatra-activerecord enabled. I am creating a model called SiteInfo which will store information such as the about_description, about_photo, tagline, etc. Is there a way within the migration to create that SiteInfo table to specify that there can only be one row? There's no way through the interface for the admin or user to create an additional one, I'm just wondering.
To answer your question: No, you can't, and there is no need for.
You'll want to do something like SiteInfo.first to load your information anyway.

Generate Propel migration file for data entry alone

Is it possible to generate a Propel migration file without modifying the schema? I only need to insert values.
Thanks.
You can generate a blank migration like this:
propel migration:create
Yes. Just call the diff target in the Propel 1 command, which will create a migration class.
(Propel on MySQL for me always creates spurious differences e.g. between FLOAT and DECIMAL, which I delete manually. I don't know if it would refuse to create a class if no differences were found).
Propel migration system is not able to insert values, only update your database structure when you update your schema.
If you use Symfony2, you can use the command propel:fixture:* to insert data for test purpose.

rails 3:how to generate models for existing database tables

I've configured my database.yml to point to my existing mysql database
how can I generate models from it?
rails generate model existing_table_name
only gives an emty model..
You can try Rmre. It can create models for existing schema and it tries to create all relationships based on foreign keys information.
A Rails model doesn't show your fields, but you can still use them. Try the following. Assuming you have a Model named ModelName and a field called "name", fire up the Rails console and type:
ModelName.find_by_name('foo')
Given a name that exists in the DB, you should see results.
Rails doesn't infer relationships though, but if your database follows Rails conventions they are easily added.
Update
I've noticed this particular lack of explicitness ("magic") is a source of confusion for newbies to Rails. You can always look in schema.rb to see the models and all the fields in one place. Also, if you would prefer to see the schema for each model in the model file, you can use the annotate_models gem, which will put the db schema in a comment at the top of the model file.
Your answer is:
$ rake db:schema:dump
That will set a new db/schema.db to create a schema of your DB.
ActiveRecord doesn't parse a schema definition. It asks the DBM for the table defs and figures out the fields on the fly.
Having the schema is useful if you are going to modify the tables via migrations.
Schema Dumping and You will help you dump it to use as a reference for building migrations.
ActiveRecord makes some suppositions about the table naming and expects an id field to be the primary key with a sequential number as the type. Having the migrations would help you to refactor the tables and/or fieldnames and types, but you can do those same things via your DBM's command-line. You don't really have to follow ActiveRecord's style but doing so helps avoid odd errors and lets AR infer things to make your life easier.
Could try Magic Model Generator
Take a look at rare_map gem.
https://github.com/wnameless/rare_map
It works both on Rail 3 and 4.

Resources