Apartment gem: How to rename tenant? - ruby-on-rails-4

Can we able to rename the tenant in apartment-gem or we have to drop the tenant and create a new one to achieve this.
Please provide me some suggestions

I don't think this is available through the apartment gem, but it is fairly simple to do with a SQL query. It depends on your setup though.
If you are using Postgresql with a schema for each tenant:
ALTER SCHEMA old RENAME TO new;
If you are using MySql, you should rename the table name prefixes for the tenant. This should work if the databases are on the same file system:
RENAME TABLE current_tenant.table TO new_tenant.table;
Disclaimer: not tested.

You can change name (in my case subdomain) doing something like this:
1) Your schema should be some unique column in Tenant model (schema_id is fine). I am generating it's value from subdomain and Tenant ID.
2) In apartment.rb you require "apartment/elevators/generic". Then
config.tenant_names = -> { Tenant.pluck :schema_id }
so you use schema stuff like tenant name.
Then on the bottom of the file add
Rails.application.config.middleware.use "Apartment::Elevators::Generic", lambda { |request|
Tenant.find_by(subdomain: request.host.split(".").first).schema_id
}
Now after you make proper subdomain requests stuff, you or your tenant user can edit name/subdomain and data in schemas will be safe.
PS: Also see here - https://github.com/influitive/apartment/issues/242

Related

AWS Amplify filter for #searchable annotation

Currently I am using a DynamoDB instance for my social media application. While designing the schema I sticked to the "one table" rule. So I am putting every data in the same table like posts, users, comments etc. Now I want to make flexible queries for my data. Here I found out that I could use the #searchable annotation to create an Elastic Search instance for a table which is annotated with #model
In my GraphQL schema I only have one #model, since I only have one table. My problem now is that I don't want to make everything in the table searchable, since that would be most likely very expensive. There are some data which don't have to be added to the Elastic Search instance (For example comment related data). How could I handle it ? Do I really have to split my schema down into multiple tables to be able to manage the #searchable annotation ? Couldn't I decide If the row should be stored to the Elastic Search with help of the Partitionkey / Primarykey, acting like a filter ?
The current implementation of the amplify-cli uses a predefined python Lambda that are added once we add the #searchable directive to one of our models.
The Lambda code can not be edited and currently, there is no option to define a custom Lambda, you read about it
https://github.com/aws-amplify/amplify-cli/issues/1113
https://github.com/aws-amplify/amplify-cli/issues/1022
If you want a custom Lambda where you can filter what goes to the Elasticsearch Instance, you can follow the steps described here https://github.com/aws-amplify/amplify-cli/issues/1113#issuecomment-476193632
The closest you can get is by creating a template in amplify\backend\api\myapiname\stacks\ where you can manage all the resources related to Elasticsearch. A good start point is to
Add #searchable to one of your model in the schema.grapql
Run amplify api gql-compile
Copy the generated template in the build folder, \amplify\backend\api\myapiname\build\stacks\SearchableStack.json to amplify\backend\api\myapiname\stacks\
Remove the #searchable directive from the model added in step 1
Start editing your new template copied in step 3
Add a Lambda and use it in the template as the resolver for the DynamoDB Stream
Using this approach will give you total control of the resources related to the Elasticsearch service, but, will also require to do it all by your own.
Or, just go by creating a table for each model.
Hope it helps
It is now possible to override the generated streaming function code as well.
thanks to the AWS Support for the information provided
leaved a message on the related github issue as well https://github.com/aws-amplify/amplify-category-api/issues/437#issuecomment-1351556948
All you need is to run
amplify override api
edit the corresponding overrode.ts
change the code with the resources.opensearch.OpenSearchStreamingLambdaFunction.code
resources.opensearch.OpenSearchStreamingLambdaFunction.functionName = 'python_streaming_function';
resources.opensearch.OpenSearchStreamingLambdaFunction.handler = 'index.lambda_handler';
resources.opensearch.OpenSearchStreamingLambdaFunction.code = {
zipFile: `
# python streaming function customized code goes here
`
}
Resources:
[1] https://docs.amplify.aws/cli/graphql/override/#customize-amplify-generated-resources-for-searchable-opensearch-directive
[2]AWS::Lambda::Function Code - Properties - https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html#aws-properties-lambda-function-code-properties

Prevent duplicate data in Webix File Manager?

Can we prevent duplicate names of files and folders in the same folder
to avoid data replication? Is there any built-in
functionality in Webix File Manager?
I solve it by myself. You can prevent duplicate data by controlling response which contain "id" and "value" with a new id and a new file name. You can control it at backend in my case MVC controller.

AWS Beanstalk - Customize app to create dynamodb tables

I'm trying to deploy my app (nodejs) using AWS beanstalk. I want to create DynamoDB tables during the deployment. I'm trying to use the customization feature of beanstalk, which means I'm trying to write a config file (YAML) under .exextensions. I want to create a table something like this:
TestTable with fields:
field 1 (hash key),
field 2 (range key),
field 3,
field 4, ..
.
When googling, I can find only examples of config files with a single field (e.g. http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-environment-resources-dynamodb.html).
Looking for some examples of config files with tables containing multiple fields or a document containing the entire features/keywords of the YAML template of beanstalk.
Dynamo DB is schemaless so you don't need to specify field3, field4 etc. You can specify key schema as shown on the page you linked. The specific example on the page only uses hash key for the dynamo DB table but you can also specify range key (field2) similar to how hash key is specified. Syntax of the config file needs to have RANGE key in addition to HASH key in KeySchema. It follows the cloudformation resource description syntax. See the following links for details:
http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html
http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-dynamodb-keyschema.html
Do you want to create a table on each deployment?
If the table is a resource that your app uses, I would go with CloudFormation to define your template (list of AWS resources that you need together, like - 3 EC2 machines, a load-balancer, an RDS server, a DynamoDB table and an ElasticBeanstalk app and environment). Then, your app will only get a reference to the table's name and use it (can be done using an environment variable).
If you haven't use CloudFormation before, you might find a slight learning-curve at the beginning, but at the end it's relatively simple. A template is a JSON file with declarations and after you upload your template, you can create many instances of it (for example - production and staging).
You can find a snippet of using Elastic Beanstalk in CloudFormation here - http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-elasticbeanstalk.html

How do you rename a security group in Sitecore?

The department I'm in recently changed it's name. However, in Sitecore, I have some groups whose name contains the old department name. I'm having trouble finding any way to rename these groups to use the new department name. There is no edit or rename option in the Role Manager.
Is there any way to rename a group after it has been created? Or is the only option to recreate that group, and All its permissions (quite the task)?
Sitecore stores permissions for items in clear text and they are not linked to the role with anything but the name so if you rename a role that you have used it on items then when you rename it authors will no longer have any right.
If you haven't used the role then you can go and change it in the aspnet_Roles table as #Bryan advises.
There is one more way. You can create a new role and give it the name you want and add the original role to it. This way authors will get the same permissions but have the new role.
The question and the answer in the post above deals with Sitecore 6 I believe.
For people who might stumble on this post:
We have successfully renamed Roles in Sitecore 7.2 by updating database without any issues.
Following are the tables need updated with new RoleName:
- aspnet_roles: update the new RoleName
- RolesInRoles: update MemberRoleName and TargetRoleName
Hope this helps someone!
If you are using the out-of-the-box Role provider, that's just standard ASP.NET Roles. There is some discussion about this here.
If you want to get adventurous, you could just edit the role name in your Core DB, table name is aspnet_Roles. Not sure what repercussions this wold have though. But hey, I like adventure. Let us know how it works. :)

Generate Symfony2 fixtures from DB?

Is it possible to generate fixtures from an existing DB in Symfony2/Doctrine? How could I do that?
Example:
I have defined 15 entities and my symfony2 application is working. Now some people are able to browse to the application and by using it it had inserted about 5000 rows until now. Now I want the stuff inserted as fixtures, but I don’t want to do this by hand. How can I generate them from the DB?
There's no direct manner within Doctrine or Symfony2, but writing a code generator for it (either within or outside of sf2) would be trivial. Just pull each property and generate a line of code to set each property, then put it in your fixture loading method. Example:
<?php
$i = 0;
$entities = $em->getRepository('MyApp:Entity')->findAll();
foreach($entities as $entity)
{
$code .= "$entity_{$i} = new MyApp\Entity();\n";
$code .= "$entity_{$i}->setMyProperty('" . addslashes($entity->getMyProperty()); . "'); \n");
$code .= "$manager->persist($entity_{$i}); \n $manager->flush();";
++$i;
}
// store code somewhere with file_put_contents
As I understand your question, you have two databases: the first is already in production and filled with 5000 rows, the second one is a new database you want to use for new test and development. Is that right ?
If it is, I suggest you to create in you test environment two entity manager: the first will be the 'default' one, which will be used in your project (your controllers, etc.). The second one will be used to connect to your production database. You will find here how to deal with multiple entity manager : http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html
Then, you should create a Fixture class which will have access to your container. There is an "how to" here : http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#using-the-container-in-the-fixtures.
Using the container, you will have access to both entity manager. And this is the 'magic': you will have to retrieve the object from your production database, and persist them in the second entity manager, which will insert them in your test database.
I point your attention to two points:
If there are relationship between object, you will have to take care to those dependencies: owner side, inversed side, ...
If you have 5000 rows, take care on the memory your script will use. Another solution may be use native sql to retrieve all the rows from your production database and insert them in your test database. Or a SQL script...
I do not have any code to suggest to you, but I hope this idea will help you.
I assume that you want to use fixtures (and not just dump the production or staging database in the development database) because a) your schema changes and the dumps would not work if you update your code or b) you don't want to dump the hole database but only want to extend some custom fixtures. An example I can think of is: you have 206 countries in your staging database and users add cities to those countries; to keep the fixtures small you only have 5 countries in your development database, however you want to add the cities that the user added to those 5 countries in the staging database to the development database
The only solution I can think of is to use the mentioned DoctrineFixturesBundle and multiple entity managers.
First of all you should configure two database connections and two entity managers in your config.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
staging:
...
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AcmeDemoBundle: ~
staging:
connection: staging
mappings:
AcmeDemoBundle: ~
As you can see both entity managers map the AcmeDemoBundle (in this bundle I will put the code to load the fixtures). If the second database is not on your development machine, you could just dump the SQL from the other machine to the development machine. That should be possible since we are talking about 500 rows and not about millions of rows.
What you can do next is to implement a fixture loader that uses the service container to retrieve the second entity manager and use Doctrine to query the data from the second database and save it to your development database (the default entity manager):
<?php
namespace Acme\DemoBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Acme\DemoBundle\Entity\City;
use Acme\DemoBundle\Entity\Country;
class LoadData implements FixtureInterface, ContainerAwareInterface
{
private $container;
private $stagingManager;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
$this->stagingManager = $this->container->get('doctrine')->getManager('staging');
}
public function load(ObjectManager $manager)
{
$this->loadCountry($manager, 'Austria');
$this->loadCountry($manager, 'Germany');
$this->loadCountry($manager, 'France');
$this->loadCountry($manager, 'Spain');
$this->loadCountry($manager, 'Great Britain');
$manager->flush();
}
protected function loadCountry(ObjectManager $manager, $countryName)
{
$country = new Country($countryName);
$cities = $this->stagingManager->createQueryBuilder()
->select('c')
->from('AcmeDemoBundle:City', 'c')
->leftJoin('c.country', 'co')
->where('co.name = :country')
->setParameter('country', $countryName)
->getQuery()
->getResult();
foreach ($cities as $city) {
$city->setCountry($country);
$manager->persist($city);
}
$manager->persist($country);
}
}
What I did in the loadCountry method was that I load the objects from the staging entity manager, add a reference to the fixture country (the one that already exists in your current fixtures) and persist it using the default entity manager (your development database).
Sources:
DoctrineFixturesBundle
How to work with Multiple Entity Managers
you could use https://github.com/Webonaute/DoctrineFixturesGeneratorBundle
It add ability to generate fixtures for single entity using commands like
$ php bin/console doctrine:generate:fixture --entity=Blog:BlogPost --ids="12 534 124" --name="bug43" --order="1"
Or you can create full snapshot
php app/console doctrine:generate:fixture --snapshot --overwrite
The Doctrine Fixtures are useful because they allow you to create objects and insert them into the database. This is especially useful when you need to create associations or say, encode a password using one of the password encoders. If you already have the data in a database, you shouldn't really need to bring them out of that format and turn it into PHP code, only to have that PHP code insert the same data back into the database. You could probably just do an SQL dump and then re-insert them into your database again that way.
Using a fixture would make more sense if you were initiating your project but wanted to use user input to create it. If you had in your config file the default user, you could read that and insert the object.
The AliceBundle can help you doing this. Indeed it allows to load fixtures with YAML (or PHP array) files.
For instance you can define your fixtures with:
Nelmio\Entity\Group:
group1:
name: Admins
owner: '#user1->id'
Or with the same structure in a PHP array. It's WAY easier than generating working PHP code.
It also supports references:
Nelmio\Entity\User:
# ...
Nelmio\Entity\Group:
group1:
name: Admins
owner: '#user1'
In the doctrine_fixture cookbook, you can see in the last example how to get the service container in your entity.
With this service container, you can retrieve the doctrine service, then the entity manager. With the entity manager, you will be able to get all the data from your database you need.
Hope this will help you!